ref: 3877e1e462c33778518e8c48dbf9e53076c00a53
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 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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
// SPDX-FileCopyrightText: Adam Evyčędo // // SPDX-License-Identifier: GPL-3.0-or-later package xyz.apiote.bimba.czwek.settings.feeds import android.content.Intent 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.lifecycle.ViewModelProvider import androidx.recyclerview.widget.LinearLayoutManager import kotlinx.coroutines.MainScope import kotlinx.coroutines.launch import xyz.apiote.bimba.czwek.R import xyz.apiote.bimba.czwek.api.Server import xyz.apiote.bimba.czwek.dashboard.MainActivity import xyz.apiote.bimba.czwek.databinding.ActivityFeedChooserBinding import xyz.apiote.bimba.czwek.repo.FeedInfo import xyz.apiote.bimba.czwek.repo.OfflineRepository import xyz.apiote.bimba.czwek.repo.OnlineRepository import xyz.apiote.bimba.czwek.repo.TrafficResponseException import xyz.apiote.bimba.czwek.repo.join // TODO on internet connection -> getServer // TODO swipe to refresh? class FeedChooserActivity : AppCompatActivity() { private lateinit var viewModel: FeedsViewModel private var _binding: ActivityFeedChooserBinding? = null private val binding get() = _binding!! private lateinit var adapter: BimbaFeedInfoAdapter private val feeds: MutableMap<String, FeedInfo> = mutableMapOf() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) _binding = ActivityFeedChooserBinding.inflate(layoutInflater) setContentView(binding.root) viewModel = ViewModelProvider(this)[FeedsViewModel::class.java] migrateFeedsSettings(this) viewModel.loadSettings(this) setUpRecycler() getServer() binding.button.setOnClickListener { moveOn() } } private fun showBottomSheet(feedID: String) { FeedBottomSheet( feedID, feeds, viewModel.settings.value ?: FeedsSettings(mutableMapOf()) ) { settings -> if (settings != null) { viewModel.setSettings(feedID, settings) updateItems(null, viewModel.settings.value, false) adapter.notifyItemChanged(adapter.getItemPosition(feedID)) } }.show(supportFragmentManager, FeedBottomSheet.TAG) } private fun setUpRecycler() { binding.resultsRecycler.layoutManager = LinearLayoutManager(this) adapter = BimbaFeedInfoAdapter( layoutInflater, feeds.map { it.value }.sortedBy { it.name }, viewModel.settings.value!!, this, { showBottomSheet(it) }, { feedID, isEnabled -> viewModel.setEnabled(feedID, isEnabled) } ) binding.resultsRecycler.adapter = adapter } private fun getServer() { binding.progress.visibility = View.VISIBLE binding.resultsRecycler.visibility = View.GONE MainScope().launch { val offlineRepository = OfflineRepository() val offlineFeeds = offlineRepository.getFeeds(this@FeedChooserActivity) if (!offlineFeeds.isNullOrEmpty()) { feeds.putAll(offlineFeeds) updateItems(offlineFeeds.map { it.value }, null) } try { val repository = OnlineRepository() val onlineFeeds = repository.getFeeds(this@FeedChooserActivity) feeds.clear() joinFeeds(offlineFeeds, onlineFeeds).let { joinedFeeds -> feeds.putAll(joinedFeeds) updateItems(joinedFeeds.map { it.value }, null) } } catch (e: TrafficResponseException) { if (offlineFeeds.isNullOrEmpty()) { showError(e.error.imageResource, e.error.stringResource) } Log.e("Feeds", "$e") } } } private fun moveOn() { viewModel.settings.value?.save(this, Server.get(this)) 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 joinFeeds( feeds1: Map<String, FeedInfo>?, feeds2: Map<String, FeedInfo>? ): Map<String, FeedInfo> { if (feeds1.isNullOrEmpty() && feeds2.isNullOrEmpty()) { return emptyMap() } if (feeds1.isNullOrEmpty()) { return feeds2!! } if (feeds2.isNullOrEmpty()) { return feeds1 } return feeds1.keys.union(feeds2.keys).associateWith { feeds1[it].join(feeds2[it]) } } private fun updateItems( feeds: List<FeedInfo>?, feedsSettings: FeedsSettings?, notify: Boolean = true ) { binding.feedsOverlay.visibility = View.GONE binding.resultsRecycler.visibility = View.VISIBLE binding.button.visibility = View.VISIBLE adapter.update(feeds, feedsSettings, notify) if (feeds?.isEmpty() == true) { showError(R.drawable.error_search, R.string.error_404) } } } |