ref: ae0b98018aed0a925cda41beada1ec6bd1f27de5
app/src/main/java/xyz/apiote/bimba/czwek/settings/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 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 |
// SPDX-FileCopyrightText: Adam Evyčędo // // SPDX-License-Identifier: GPL-3.0-or-later package xyz.apiote.bimba.czwek.settings.feeds import android.annotation.SuppressLint import android.content.Context import android.content.DialogInterface import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.TextView import androidx.recyclerview.widget.RecyclerView import com.google.android.material.bottomsheet.BottomSheetDialogFragment import com.google.android.material.divider.MaterialDivider import com.google.android.material.materialswitch.MaterialSwitch import com.google.android.material.textview.MaterialTextView import xyz.apiote.bimba.czwek.R import xyz.apiote.bimba.czwek.repo.FeedInfo import java.time.format.DateTimeFormatter class BimbaFeedInfoViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { val root: View = itemView.findViewById(R.id.feed) val switch: MaterialSwitch = itemView.findViewById(R.id.feed_switch) val name: TextView = itemView.findViewById(R.id.feed_name) val divider: MaterialDivider = itemView.findViewById(R.id.divider) companion object { fun bind( feed: FeedInfo, feedSettings: FeedSettings?, holder: BimbaFeedInfoViewHolder?, context: Context, onClickListener: (String) -> Unit, onCheckedChangeListener: (String, Boolean) -> Unit ) { val colorAttr = if (feed.cached) { com.google.android.material.R.attr.colorOnSurfaceVariant } else { com.google.android.material.R.attr.colorOnSurface } holder?.name?.setTextColor( context.theme.obtainStyledAttributes( R.style.Theme_Bimba, intArrayOf(colorAttr) ).getColor(0, 0) ) holder?.root?.setOnClickListener { onClickListener(feed.id) } holder?.name?.text = feed.name if (feedSettings != null) { holder?.divider?.visibility = View.VISIBLE holder?.switch?.apply { visibility = View.VISIBLE isChecked = feedSettings.enabled setOnCheckedChangeListener { _, isChecked -> onCheckedChangeListener(feed.id, isChecked) } } } } } } class BimbaFeedInfoAdapter( private val inflater: LayoutInflater, private var feeds: List<FeedInfo>, private var feedsSettings: FeedsSettings, private val context: Context, private val onClickListener: ((String) -> Unit), private val onEnabledChangedListener: ((String, Boolean) -> 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) { val feed = feeds[position] BimbaFeedInfoViewHolder.bind( feed, feedsSettings.settings[feed.id], holder, context, onClickListener, onEnabledChangedListener ) } override fun getItemCount(): Int = feeds.size fun getItemPosition(feedID: String): Int { return feeds.indexOfFirst { it.id == feedID } } @SuppressLint("NotifyDataSetChanged") // todo [3.2] DiffUtil fun update(items: List<FeedInfo>?, settings: FeedsSettings?, notify: Boolean) { if (items != null) { feeds = items.sortedBy { it.name } } if (settings != null) { feedsSettings = settings } if (notify) { notifyDataSetChanged() } } } class FeedBottomSheet( private var feed: FeedInfo, private var settings: FeedSettings?, private val onDismiss: (FeedSettings?) -> Unit ) : BottomSheetDialogFragment() { companion object { const val TAG = "DepartureBottomSheet" } override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View { val content = inflater.inflate(R.layout.feed_bottom_sheet, container, false) content.findViewById<MaterialTextView>(R.id.title).text = feed.name content.findViewById<MaterialTextView>(R.id.description).text = feed.description content.findViewById<MaterialTextView>(R.id.outdated_info_warning).visibility = if (feed.cached) { View.VISIBLE } else { View.GONE } if (feed.validSince != null && feed.validTill != null) { content.findViewById<MaterialTextView>(R.id.timetable_validity).apply { visibility = View.VISIBLE text = getString( R.string.current_timetable_validity, feed.validSince!!.format(DateTimeFormatter.ISO_LOCAL_DATE), feed.validTill!!.format(DateTimeFormatter.ISO_LOCAL_DATE) ) } } content.findViewById<MaterialTextView>(R.id.attribution).text = feed.attribution content.findViewById<MaterialTextView>(R.id.update_time).text = getString(R.string.last_update, feed.formatDate()) content.findViewById<MaterialSwitch>(R.id.onlineSwitch).apply { isChecked = settings?.useOnline ?: false setOnCheckedChangeListener { _, isChecked -> settings = settings?.copy(useOnline = isChecked) ?: FeedSettings( enabled = true, useOnline = isChecked ) } } return content } override fun onDismiss(dialog: DialogInterface) { onDismiss(settings) super.onDismiss(dialog) } } |