ref: d083d48d694686414b56bdd4327d2756afc85672
app/src/main/java/ml/adamsprogs/bimba/feeds/FeedChooserActivity.kt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
package ml.adamsprogs.bimba.feeds import android.content.Context import android.content.Intent import android.content.SharedPreferences import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.util.Log import android.view.View import android.widget.Toast import androidx.core.content.edit import androidx.core.widget.addTextChangedListener import androidx.recyclerview.widget.LinearLayoutManager import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.MainScope import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import ml.adamsprogs.bimba.R import ml.adamsprogs.bimba.api.FeedsResponse import ml.adamsprogs.bimba.api.FeedsSuccess import ml.adamsprogs.bimba.api.Server import ml.adamsprogs.bimba.api.getFeeds import ml.adamsprogs.bimba.dashboard.MainActivity import ml.adamsprogs.bimba.databinding.ActivityFeedChooserBinding import java.io.InputStream class FeedChooserActivity : AppCompatActivity() { private var _binding: ActivityFeedChooserBinding? = null private val binding get() = _binding!! private lateinit var adapter: BimbaFeedInfoAdapter private lateinit var preferences: SharedPreferences override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) _binding = ActivityFeedChooserBinding.inflate(layoutInflater) setContentView(binding.root) preferences = getSharedPreferences("shp", MODE_PRIVATE) setUpRecycler() binding.button.setOnClickListener { getServer( binding.serverField.editText!!.text.toString(), binding.tokenField.editText!!.text.toString() ) } binding.serverField.editText!!.apply { setText(preferences.getString("host", "bimba.apiote.xyz")) addTextChangedListener { editable -> binding.button.apply { text = context.getString(R.string.cont) isEnabled = !editable.isNullOrBlank() setOnClickListener { getServer( editable!!.toString(), binding.tokenField.editText!!.text.toString() ) } } } } } private fun setUpRecycler() { binding.resultsRecycler.layoutManager = LinearLayoutManager(this) adapter = BimbaFeedInfoAdapter(layoutInflater, listOf(), this) { Log.v("FeedInfo", "clicked: $it") // todo show bottom sheet } binding.resultsRecycler.adapter = adapter } private fun getServer(host: String, token: String) { assert(binding.serverField.editText!!.text.isNotEmpty()) preferences.edit(true) { putString("server", host) putString("token", token) } binding.circularProgressIndicator.visibility = View.VISIBLE binding.resultsRecycler.visibility = View.GONE binding.feedInfo.visibility = View.GONE MainScope().launch { val feedsStream = getFeeds(Server(host, token, "")) if (feedsStream == null) { // todo(error-handling) show empty state Toast.makeText( this@FeedChooserActivity as Context, "Couldn't get response", Toast.LENGTH_SHORT ).show() } else { updateItems(unmarshallFeedsResponse(feedsStream)) binding.button.apply { text = context.getString(R.string.save) setOnClickListener { moveOn() } } } } } private fun moveOn() { val intent = Intent(this, MainActivity::class.java) startActivity(intent) val wasFirstRun = preferences.getBoolean("firstRun", true) preferences.edit(true) { putBoolean("firstRun", false) } if (wasFirstRun) { finish() } } private suspend fun unmarshallFeedsResponse(stream: InputStream): FeedsSuccess { return withContext(Dispatchers.IO) { when (val response = FeedsResponse.unmarshal(stream)) { is FeedsSuccess -> { return@withContext response } else -> { TODO("Error response") } } } } private fun updateItems(response: FeedsSuccess) { Log.v("items", "${response.adminContact} ${response.rateLimited}") response.feeds.forEach { Log.v("items", "$it") } binding.circularProgressIndicator.visibility = View.GONE binding.resultsRecycler.visibility = View.VISIBLE binding.feedInfo.visibility = View.VISIBLE binding.feedInfo.text = // todo(ui) table if (response.rateLimited) { getString(R.string.server_info_rate_limited, response.adminContact) } else { getString(R.string.server_info_not_rate_limited, response.adminContact) } adapter.update(response.feeds) } } |