Bimba.git

ref: 009e40933320a46978bb4175a788e8a77e8390c9

app/src/main/java/xyz/apiote/bimba/czwek/settings/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
package xyz.apiote.bimba.czwek.settings.feeds

import android.content.Context
import android.content.Intent
import android.net.ConnectivityManager
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.content.res.AppCompatResources
import androidx.core.content.edit
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import xyz.apiote.bimba.czwek.api.ErrorResponse
import xyz.apiote.bimba.czwek.api.FeedInfoV1
import xyz.apiote.bimba.czwek.api.FeedsResponse
import xyz.apiote.bimba.czwek.api.FeedsResponseDev
import xyz.apiote.bimba.czwek.api.FeedsResponseV1
import xyz.apiote.bimba.czwek.api.Server
import xyz.apiote.bimba.czwek.api.getFeeds
import xyz.apiote.bimba.czwek.dashboard.MainActivity
import xyz.apiote.bimba.czwek.databinding.ActivityFeedChooserBinding

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

	private lateinit var adapter: BimbaFeedInfoAdapter

	override fun onCreate(savedInstanceState: Bundle?) {
		super.onCreate(savedInstanceState)
		_binding = ActivityFeedChooserBinding.inflate(layoutInflater)
		setContentView(binding.root)

		setUpRecycler()
		getServer()

		binding.button.setOnClickListener {
			moveOn()
		}
	}

	private fun setUpRecycler() {
		binding.resultsRecycler.layoutManager = LinearLayoutManager(this)
		adapter = BimbaFeedInfoAdapter(layoutInflater, listOf(), this) {
			FeedBottomSheet(it).show(supportFragmentManager, FeedBottomSheet.TAG)
		}
		binding.resultsRecycler.adapter = adapter
	}

	private fun getServer() {
		binding.progress.visibility = View.VISIBLE
		binding.resultsRecycler.visibility = View.GONE

		MainScope().launch {
			val cm = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
			Log.i("FeedChooser", "${Server.get(this@FeedChooserActivity)}")
			val result = getFeeds(cm, Server.get(this@FeedChooserActivity))
			if (result.error != null) {
				showError(result.error.imageResource, result.error.stringResource)
				if (result.stream != null) {
					val response = withContext(Dispatchers.IO) {ErrorResponse.unmarshal(result.stream)}
					Log.w("FeedChooser", "${result.error.statusCode}, ${response.message}")
				} else {
					Log.w(
						"FeedChooser",
						"${result.error.statusCode}, ${getString(result.error.stringResource)}"
					)
				}
				return@launch
			}
			val feeds = when (val response = withContext(Dispatchers.IO) {FeedsResponse.unmarshal(result.stream!!)}) {
				is FeedsResponseDev -> response.feeds
				is FeedsResponseV1 -> response.feeds
				else -> null
			}
			updateItems(feeds!!)
		}
	}

	private fun moveOn() {
		val preferences = getSharedPreferences("shp", MODE_PRIVATE)
		preferences.edit(true) {
			putBoolean("inFeedsTransaction", false)
		}
		if (preferences.getBoolean("firstRun", true)) {
			val intent = Intent(this, MainActivity::class.java)
			startActivity(intent)
		}
		finish()
	}

	private fun showError(image: Int, text: Int) {
		binding.progress.visibility = View.GONE
		binding.resultsRecycler.visibility = View.GONE
		binding.errorImage.apply {
			visibility = View.VISIBLE
			setImageDrawable(AppCompatResources.getDrawable(this@FeedChooserActivity, image))
		}
		binding.errorText.apply {
			visibility = View.VISIBLE
			setText(text)
		}

	}

	private fun updateItems(feeds: List<FeedInfoV1>) {
		binding.feedsOverlay.visibility = View.GONE
		binding.resultsRecycler.visibility = View.VISIBLE
		binding.button.visibility = View.VISIBLE
		adapter.update(feeds)
	}
}