Bimba.git

ref: a9046e4c5bd51282cf4b666e3bb8620ffcf637f5

app/src/main/java/ml/adamsprogs/bimba/settings/ServerChooserActivity.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
package ml.adamsprogs.bimba.settings

import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.net.ConnectivityManager
import android.os.Bundle
import android.util.Log
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.content.res.AppCompatResources
import androidx.core.content.edit
import androidx.core.widget.addTextChangedListener
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
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.databinding.ActivityServerChooserBinding
import ml.adamsprogs.bimba.settings.feeds.FeedChooserActivity

class ServerChooserActivity : AppCompatActivity() {
	private var _binding: ActivityServerChooserBinding? = null
	private val binding get() = _binding!!

	private val activityLauncher =
		registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
			if (!preferences.getBoolean("inFeedsTransaction", true)) {
				finish()
			}
		}

	private lateinit var preferences: SharedPreferences

	override fun onCreate(savedInstanceState: Bundle?) {
		super.onCreate(savedInstanceState)
		preferences = getSharedPreferences("shp", MODE_PRIVATE)

		if (intent.getBooleanExtra("simple", false)) {
			setServer("bimba.apiote.xyz", "")
			runFeedsActivity()
			finish()
		}

		_binding = ActivityServerChooserBinding.inflate(layoutInflater)
		setContentView(binding.root)

		preferences.edit(true) {
			putBoolean("inFeedsTransaction", true)
		}

		binding.button.isEnabled = false
		binding.serverField.editText!!.addTextChangedListener { editable ->
			binding.button.isEnabled = !editable.isNullOrBlank()
		}

		if (!preferences.getBoolean("firstRun", true)) {
			Server.get(this).let { server ->
				binding.serverField.editText!!.setText(server.host)
				binding.tokenField.editText!!.setText(server.token)
			}
		}

		binding.button.setOnClickListener {
			setServer(
				binding.serverField.editText!!.text.toString(),
				binding.tokenField.editText!!.text.toString()
			)
			checkServer()
		}
	}

	private fun showDialog(
		title: Int,
		description: Int,
		icon: Int,
		onPositive: (() -> Unit)?
	) {
		MaterialAlertDialogBuilder(this)
			.setIcon(AppCompatResources.getDrawable(this, icon))
			.setTitle(getString(title))
			.setMessage(getString(description))
			.setNegativeButton(resources.getString(R.string.cancel)) { _, _ -> }.apply {
				if (onPositive != null) {
					setPositiveButton(resources.getString(R.string.cont)) { _, _ ->
						onPositive()
					}
				}
			}
			.show()
	}

	private fun checkServer() {
		// todo [api-freeze] check is really bimba (/.well-known/bimba)
		MainScope().launch {
			val result = getFeeds(
				getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager,
				Server.get(this@ServerChooserActivity)
			)
			if (result.error != null) {
				Log.w("ServerChooser", "$result")
				showDialog(R.string.error, result.error.stringResource, result.error.imageResource, null)
				return@launch
			}

			val response = FeedsResponse.unmarshal(result.stream!!) as FeedsSuccess
			val token = preferences.getString("token", "")
			if (response.private && token == "") {
				showDialog(R.string.error, R.string.server_private_question, R.drawable.error_sec, null)
				return@launch
			}
			if (response.rateLimited && token == "") {
				showDialog(
					R.string.rate_limit,
					R.string.server_rate_limited_question,
					R.drawable.error_limit
				) {
					runFeedsActivity()
				}
				return@launch
			}
			runFeedsActivity()
		}
	}

	private fun setServer(hostname: String, token: String) {
		preferences.edit(true) {
			putString("host", hostname)
			putString("token", token)
		}
	}

	private fun runFeedsActivity() {
		activityLauncher.launch(Intent(this, FeedChooserActivity::class.java))
	}
}