From 442efff0001615590e151346989bf97832ff9299 Mon Sep 17 00:00:00 2001 From: Philipp Heckel Date: Sun, 30 Jan 2022 20:18:09 -0500 Subject: [PATCH] Feature complete; still WIP --- .../main/java/io/heckel/ntfy/db/Database.kt | 4 +- .../heckel/ntfy/ui/DetailSettingsActivity.kt | 43 +++++++++++++------ app/src/main/res/values/strings.xml | 1 + 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/io/heckel/ntfy/db/Database.kt b/app/src/main/java/io/heckel/ntfy/db/Database.kt index 4f96143..28b0aff 100644 --- a/app/src/main/java/io/heckel/ntfy/db/Database.kt +++ b/app/src/main/java/io/heckel/ntfy/db/Database.kt @@ -233,7 +233,7 @@ interface SubscriptionDao { @Query(""" SELECT - s.id, s.baseUrl, s.topic, s.instant, s.mutedUntil, s.upAppId, s.upConnectorToken, + s.id, s.baseUrl, s.topic, s.instant, s.mutedUntil, s.authUserId, s.upAppId, s.upConnectorToken, COUNT(n.id) totalCount, COUNT(CASE n.notificationId WHEN 0 THEN NULL ELSE n.id END) newCount, IFNULL(MAX(n.timestamp),0) AS lastActive @@ -246,7 +246,7 @@ interface SubscriptionDao { @Query(""" SELECT - s.id, s.baseUrl, s.topic, s.instant, s.mutedUntil, s.upAppId, s.upConnectorToken, + s.id, s.baseUrl, s.topic, s.instant, s.mutedUntil, s.authUserId, s.upAppId, s.upConnectorToken, COUNT(n.id) totalCount, COUNT(CASE n.notificationId WHEN 0 THEN NULL ELSE n.id END) newCount, IFNULL(MAX(n.timestamp),0) AS lastActive diff --git a/app/src/main/java/io/heckel/ntfy/ui/DetailSettingsActivity.kt b/app/src/main/java/io/heckel/ntfy/ui/DetailSettingsActivity.kt index 665759d..e12d8e0 100644 --- a/app/src/main/java/io/heckel/ntfy/ui/DetailSettingsActivity.kt +++ b/app/src/main/java/io/heckel/ntfy/ui/DetailSettingsActivity.kt @@ -73,33 +73,50 @@ class DetailSettingsActivity : AppCompatActivity() { lifecycleScope.launch(Dispatchers.IO) { val subscription = repository.getSubscription(subscriptionId) ?: return@launch val users = repository.getUsers().filter { it.baseUrl == subscription.baseUrl } + val authUser = users.firstOrNull { it.id == subscription.authUserId } + Log.d(TAG, "subscription: $subscription") activity?.runOnUiThread { - loadView(subscription.id, users) + loadView(subscription, authUser, users) } } } - private fun loadView(subscriptionId: Long, users: List) { + private fun loadView(subscription: Subscription, authUser: User?, users: List) { // Login user - val authUserPrefId = context?.getString(R.string.detail_settings_auth_user_key) ?: return - val authUser: ListPreference? = findPreference(authUserPrefId) - authUser?.entries = users.map { it.username }.toTypedArray() - authUser?.entryValues = users.map { it.id.toString() }.toTypedArray() - authUser?.preferenceDataStore = object : PreferenceDataStore() { + val anonUser = User(0, "", getString(R.string.detail_settings_auth_user_entry_anon), "") + val usersWithAnon = users.toMutableList() + usersWithAnon.add(0, anonUser) + val authUserPrefId = getString(R.string.detail_settings_auth_user_key) + val authUserPref: ListPreference? = findPreference(authUserPrefId) + authUserPref?.entries = usersWithAnon.map { it.username }.toTypedArray() + authUserPref?.entryValues = usersWithAnon.map { it.id.toString() }.toTypedArray() + authUserPref?.value = authUser?.id?.toString() ?: anonUser.id.toString() + Log.d(TAG, "--> ${authUser?.id?.toString() ?: anonUser.id.toString()}") + authUserPref?.summaryProvider = Preference.SummaryProvider { pref -> + when (pref.value) { + anonUser.id.toString() -> getString(R.string.detail_settings_auth_user_summary_none) + else -> { + val username = users.firstOrNull { it.id.toString() == pref.value } ?: "?" + getString(R.string.detail_settings_auth_user_summary_user_x, username) + } + } + } + authUserPref?.preferenceDataStore = object : PreferenceDataStore() { override fun putString(key: String?, value: String?) { - val authUserId = when (value) { - "" -> null + val newAuthUserId = when (value) { + anonUser.id.toString() -> null else -> value?.toLongOrNull() } lifecycleScope.launch(Dispatchers.IO) { - Log.d(TAG, "Updating auth user ID to $authUserId for subscription $subscriptionId") - repository.updateSubscriptionAuthUserId(subscriptionId, authUserId) + Log.d(TAG, "Updating subscription ${subscription.id} with new auth user ID $newAuthUserId") + repository.updateSubscriptionAuthUserId(subscription.id, newAuthUserId) + Log.d(TAG, "after save: ${repository.getSubscription(subscription.id)}") serviceManager.refresh() } } override fun getString(key: String?, defValue: String?): String? { - Log.d(TAG, "getstring called $key $defValue") - return "xxx" + Log.d(TAG, "getstring called $key $defValue -> ${authUser?.id?.toString() ?: anonUser.id.toString()}") + return authUser?.id?.toString() ?: anonUser.id.toString() } } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c326d17..b31633f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -180,6 +180,7 @@ For topics that require a login, you may pick the login user here. You can add/edit users in the main settings. SubscriptionAuthUserKey Login user + Anonymous login No user selected to log in to the topic User %1$s selected as a login user