ref: 68ae37f62658ef2b5dfe364bfa283d03b4f35951
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 |
// 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.view.View import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.content.res.AppCompatResources import androidx.core.content.edit import androidx.core.widget.doAfterTextChanged import androidx.lifecycle.ViewModelProvider import androidx.recyclerview.widget.LinearLayoutManager import androidx.transition.TransitionManager import com.google.android.material.transition.MaterialFade 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 // 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 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.searchBar.editText?.doAfterTextChanged { editable -> if (editable != null) { updateItems((viewModel.feeds.value ?: emptyMap()).values.filter { it.name.contains(editable, true) }, null) } } binding.button.setOnClickListener { moveOn() } } private fun showBottomSheet(feedID: String) { FeedBottomSheet( feedID, viewModel.feeds.value!!, viewModel.settings.value ?: FeedsSettings(mutableMapOf()) ) { settings -> if (settings != null) { val fade = MaterialFade().apply { addTarget(R.id.feed_switch_container) duration = 500L } TransitionManager.beginDelayedTransition(binding.root, fade) 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, (viewModel.feeds.value ?: emptyMap()).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 viewModel.loadFeeds(this) viewModel.feeds.observe(this) { feeds -> updateItems(feeds.map { it.value }, null) } viewModel.error.observe(this) { showError(it.imageResource, it.stringResource) } } 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 updateItems( feeds: List<FeedInfo>?, feedsSettings: FeedsSettings?, notify: Boolean = true ) { binding.feedsOverlay.visibility = View.GONE binding.resultsRecycler.visibility = View.VISIBLE binding.searchBar.visibility = if ((viewModel.feeds.value?.size ?: 0) > 12) { View.VISIBLE } else { View.GONE } binding.button.visibility = View.VISIBLE adapter.update(feeds, feedsSettings, notify) if (feeds?.isEmpty() == true) { showError(R.drawable.error_search, R.string.error_404) } } } |