Bimba.git

commit cef1d5a9ef61fb9d7835b06f87bdea3e18e03867

Author: Adam <git@apiote.xyz>

fix saving localities

%!v(PANIC=String method: strings: negative Repeat count)


diff --git a/app/src/main/java/xyz/apiote/bimba/czwek/api/Api.kt b/app/src/main/java/xyz/apiote/bimba/czwek/api/Api.kt
index 3110ad3f67d1911ae1dd48e710cd3d5682e47de1..ab41284587c6f57fb0ba39a013e76f8ccf190389 100644
--- a/app/src/main/java/xyz/apiote/bimba/czwek/api/Api.kt
+++ b/app/src/main/java/xyz/apiote/bimba/czwek/api/Api.kt
@@ -20,11 +20,15 @@ data class Server(val host: String, val token: String, val feeds: String, val apiPath: String) {
 	companion object {
 		fun get(context: Context): Server {
 			val preferences = context.getSharedPreferences("shp", MODE_PRIVATE)
+			val apiPath = preferences.getString("apiPath", "")!!
+			val feeds = context.getSharedPreferences(
+				URLEncoder.encode(apiPath, "utf-8"),
+				MODE_PRIVATE
+			).all.filter { it.value as Boolean }.keys.joinToString(",")
 			val host = preferences.getString("host", "bimba.apiote.xyz")!!
 			return Server(
 				host, preferences.getString("token", "")!!,
-				preferences.getString("${host}_feeds", "")!!,
-				preferences.getString("apiPath", "")!!,
+				feeds, apiPath
 			)
 		}
 	}




diff --git a/app/src/main/java/xyz/apiote/bimba/czwek/settings/feeds/FeedInfos.kt b/app/src/main/java/xyz/apiote/bimba/czwek/settings/feeds/FeedInfos.kt
index 1ee8463c75f00bda76be726c3e4ffeb7a62cb989..dd00f08cf5b25aaa579b89960915a34a4870dc62 100644
--- a/app/src/main/java/xyz/apiote/bimba/czwek/settings/feeds/FeedInfos.kt
+++ b/app/src/main/java/xyz/apiote/bimba/czwek/settings/feeds/FeedInfos.kt
@@ -14,6 +14,8 @@ import com.google.android.material.bottomsheet.BottomSheetDialogFragment
 import com.google.android.material.materialswitch.MaterialSwitch
 import xyz.apiote.bimba.czwek.R
 import xyz.apiote.bimba.czwek.api.FeedInfoV1
+import xyz.apiote.bimba.czwek.api.Server
+import java.net.URLEncoder
 
 
 class BimbaFeedInfoViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
@@ -28,28 +30,16 @@ 			context: Context,
 			holder: BimbaFeedInfoViewHolder?,
 			onClickListener: (FeedInfoV1) -> 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()
-
+			val shp = context.getSharedPreferences(URLEncoder.encode(Server.get(context).apiPath, "utf-8"), MODE_PRIVATE)
 			holder?.root?.setOnClickListener {
 				onClickListener(feed)
 			}
 			holder?.name?.text = feed.name
 			holder?.switch?.apply {
-				isChecked = feed.id in enabledFeeds
+				isChecked = shp.getBoolean(feed.id, false)
 				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 = ",")
-						)
+						putBoolean(feed.id, isChecked)
 					}
 				}
 			}
@@ -77,7 +67,7 @@ 	override fun getItemCount(): Int = feeds.size
 
 	@SuppressLint("NotifyDataSetChanged") // todo [3.1] DiffUtil
 	fun update(items: List<FeedInfoV1>) {
-		feeds = items
+		feeds = items.sortedBy { it.name }
 		notifyDataSetChanged()
 	}
 }
@@ -96,7 +86,8 @@ 		val content = inflater.inflate(R.layout.feed_bottom_sheet, container, false)
 		content.findViewById<TextView>(R.id.title).text = feed.name
 		content.findViewById<TextView>(R.id.description).text = feed.description
 		content.findViewById<TextView>(R.id.attribution).text = feed.attribution
-		content.findViewById<TextView>(R.id.update_time).text = getString(R.string.last_update, feed.formatDate())
+		content.findViewById<TextView>(R.id.update_time).text =
+			getString(R.string.last_update, feed.formatDate())
 		return content
 	}
 }
\ No newline at end of file