Bimba.git

ref: 83525b8c15e6d18f23e32ee2ba83f013a92368c3

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

import android.content.Context
import android.content.Intent
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.recyclerview.widget.LinearLayoutManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
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

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
			val feedsResult = getFeeds(cm, Server.get(this@FeedChooserActivity))
			if (feedsResult.error != null) {
				Log.w("FeedChooser", "$feedsResult")
				return@launch
			}
			val response = withContext(Dispatchers.IO) {
				FeedsResponse.unmarshal(feedsResult.stream!!)
			}
			if (response is FeedsSuccess) {
				updateItems(response)
			} else {
				// todo error handling
			}
		}
	}

	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 updateItems(response: FeedsSuccess) {
		binding.progress.visibility = View.GONE
		binding.resultsRecycler.visibility = View.VISIBLE
		adapter.update(response.feeds)
	}
}