Bimba.git

ref: 2dc257c17c5088dd00af6ab6c549b76a0b8bb617

app/src/main/java/ml/adamsprogs/bimba/feeds/FeedInfos.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
package ml.adamsprogs.bimba.feeds
// git:fixup feeds
import android.content.Context
import android.content.Context.MODE_PRIVATE
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.content.edit
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.materialswitch.MaterialSwitch
import ml.adamsprogs.bimba.R
import ml.adamsprogs.bimba.api.FeedInfo


class BimbaFeedInfoViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
	val root: View = itemView.findViewById(R.id.feedinfo)
	val switch: MaterialSwitch = itemView.findViewById(R.id.feed_switch)
	val name: TextView = itemView.findViewById(R.id.feed_name)

	companion object {
		fun bind(
			feed: FeedInfo,
			context: Context,
			holder: BimbaFeedInfoViewHolder?,
			onClickListener: (FeedInfo) -> Unit
		) {
			val shp = context.getSharedPreferences("shp", MODE_PRIVATE)
			val host = shp.getString("host", "bimba.apiote.xyz")!!
			val enabledFeeds =
				shp.getString("${host}_feeds", "")!!.split(",").associateWith { }.toMutableMap()

			holder?.root?.setOnClickListener {
				onClickListener(feed)
			}
			holder?.name?.text = feed.name
			holder?.switch?.apply {
				isChecked = feed.id in enabledFeeds
				setOnCheckedChangeListener { _, isChecked ->
					if (isChecked) {
						enabledFeeds[feed.id] = Unit
					} else {
						enabledFeeds.remove(feed.id)
					}
					shp.edit(true) {
						putString("${host}_feeds", enabledFeeds.map { it.key }.filter { it != "" }.joinToString(separator = ","))
					}
				}
			}
		}
	}
}

class BimbaFeedInfoAdapter(
	private val inflater: LayoutInflater,
	private var feeds: List<FeedInfo>,
	private val context: Context,
	private val onClickListener: ((FeedInfo) -> Unit)
) :
	RecyclerView.Adapter<BimbaFeedInfoViewHolder>() {
	override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BimbaFeedInfoViewHolder {
		val rowView = inflater.inflate(R.layout.feedinfo, parent, false)
		return BimbaFeedInfoViewHolder(rowView)
	}

	override fun onBindViewHolder(holder: BimbaFeedInfoViewHolder, position: Int) {
		BimbaFeedInfoViewHolder.bind(feeds[position], context, holder, onClickListener)
	}

	override fun getItemCount(): Int = feeds.size

	fun update(items: List<FeedInfo>) {
		feeds = items
		notifyDataSetChanged()
	}

}