Bimba.git

ref: 04d9b254197db70e7500b745061c9b0818405b21

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
package ml.adamsprogs.bimba.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.core.content.edit
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
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 = FeedsResponse.unmarshal(feedsResult.stream!!)
			updateItems(response as FeedsSuccess)
		}
	}

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