Bimba.git

ref: e200ec2c2898c340acd1e8aca5452e0dd4acf877

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
package ml.adamsprogs.bimba.feeds

import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.net.ConnectivityManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
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 cm = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
			val feedsResult = getFeeds(cm, Server(host, token, ""))
			val response = if (feedsResult.stream != null) {
				unmarshallFeedsResponse(feedsResult.stream)
			} else {
				null
			}
			if (feedsResult.error != null) {
				binding.feedInfo.text = getString(feedsResult.error.stringResource)
				Log.w("FeedChooser", "$feedsResult")
				Log.w("FeedChooser", "$response")
			} else {
				updateItems(response as FeedsSuccess)  // todo(error-handling) handle parsing error (not bimba server, wrong API version)
				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)
			putString("server", binding.serverField.editText!!.text.toString())
			putString("token", binding.tokenField.editText!!.text.toString())
		}
		if (wasFirstRun) {
			finish()
		}
	}

	private suspend fun unmarshallFeedsResponse(stream: InputStream): FeedsResponse {
		return withContext(Dispatchers.IO) {
			FeedsResponse.unmarshal(stream)
		}
	}

	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)
	}
}