From f7fbf467b0119d1b5e37a297b2c98bee40ba0850 Mon Sep 17 00:00:00 2001 From: Philipp Heckel Date: Sun, 30 Jan 2022 17:38:33 -0500 Subject: [PATCH] Fix back stack; properly update auth user via detail settings; works --- app/src/main/AndroidManifest.xml | 3 - .../main/java/io/heckel/ntfy/db/Database.kt | 3 + .../main/java/io/heckel/ntfy/db/Repository.kt | 6 +- .../java/io/heckel/ntfy/msg/ApiService.kt | 3 + .../java/io/heckel/ntfy/ui/DetailActivity.kt | 6 +- .../heckel/ntfy/ui/DetailSettingsActivity.kt | 78 ++++++++++++------- app/src/main/res/xml/detail_preferences.xml | 2 +- 7 files changed, 65 insertions(+), 36 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 3ed9572..af1a395 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -61,9 +61,6 @@ - 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 e2dcdfc..4f96143 100644 --- a/app/src/main/java/io/heckel/ntfy/db/Database.kt +++ b/app/src/main/java/io/heckel/ntfy/db/Database.kt @@ -263,6 +263,9 @@ interface SubscriptionDao { @Update fun update(subscription: Subscription) + @Query("UPDATE subscription SET authUserId = :authUserId WHERE id = :subscriptionId") + fun updateSubscriptionAuthUserId(subscriptionId: Long, authUserId: Long?) + @Query("DELETE FROM subscription WHERE id = :subscriptionId") fun remove(subscriptionId: Long) diff --git a/app/src/main/java/io/heckel/ntfy/db/Repository.kt b/app/src/main/java/io/heckel/ntfy/db/Repository.kt index 9988f02..b4d1c36 100644 --- a/app/src/main/java/io/heckel/ntfy/db/Repository.kt +++ b/app/src/main/java/io/heckel/ntfy/db/Repository.kt @@ -78,13 +78,17 @@ class Repository(private val sharedPrefs: SharedPreferences, private val databas subscriptionDao.update(subscription) } + fun updateSubscriptionAuthUserId(subscriptionId: Long, authUserId: Long?) { + subscriptionDao.updateSubscriptionAuthUserId(subscriptionId, authUserId) + } + @Suppress("RedundantSuspendModifier") @WorkerThread suspend fun removeSubscription(subscriptionId: Long) { subscriptionDao.remove(subscriptionId) } - suspend fun removeAuthUserFromSubscriptions(authUserId: Long) { + fun removeAuthUserFromSubscriptions(authUserId: Long) { subscriptionDao.removeAuthUserFromSubscriptions(authUserId) } diff --git a/app/src/main/java/io/heckel/ntfy/msg/ApiService.kt b/app/src/main/java/io/heckel/ntfy/msg/ApiService.kt index 6d7baf1..6793c98 100644 --- a/app/src/main/java/io/heckel/ntfy/msg/ApiService.kt +++ b/app/src/main/java/io/heckel/ntfy/msg/ApiService.kt @@ -69,6 +69,9 @@ class ApiService { .url(url) .addHeader("User-Agent", USER_AGENT) .build() + + // XXXXXXXXXXXx + client.newCall(request).execute().use { response -> if (!response.isSuccessful) { throw Exception("Unexpected response ${response.code} when polling topic $url") diff --git a/app/src/main/java/io/heckel/ntfy/ui/DetailActivity.kt b/app/src/main/java/io/heckel/ntfy/ui/DetailActivity.kt index b95eb15..17524ec 100644 --- a/app/src/main/java/io/heckel/ntfy/ui/DetailActivity.kt +++ b/app/src/main/java/io/heckel/ntfy/ui/DetailActivity.kt @@ -448,7 +448,10 @@ class DetailActivity : AppCompatActivity(), ActionMode.Callback, NotificationFra private fun onSettingsClick() { Log.d(TAG, "Opening subscription settings for ${topicShortUrl(subscriptionBaseUrl, subscriptionTopic)}") - startActivity(Intent(this, DetailSettingsActivity::class.java)) + + val intent = Intent(this, DetailSettingsActivity::class.java) + intent.putExtra(EXTRA_SUBSCRIPTION_ID, subscriptionId) + startActivity(intent) } private fun onDeleteClick() { @@ -622,5 +625,6 @@ class DetailActivity : AppCompatActivity(), ActionMode.Callback, NotificationFra companion object { const val TAG = "NtfyDetailActivity" + const val EXTRA_SUBSCRIPTION_ID = "subscriptionId" } } 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 62d674c..665759d 100644 --- a/app/src/main/java/io/heckel/ntfy/ui/DetailSettingsActivity.kt +++ b/app/src/main/java/io/heckel/ntfy/ui/DetailSettingsActivity.kt @@ -1,43 +1,20 @@ package io.heckel.ntfy.ui -import android.Manifest -import android.content.ClipData -import android.content.ClipboardManager -import android.content.Context -import android.content.Intent -import android.content.pm.PackageManager -import android.os.Build import android.os.Bundle -import android.text.TextUtils -import android.widget.Toast -import androidx.annotation.Keep import androidx.appcompat.app.AppCompatActivity -import androidx.appcompat.app.AppCompatDelegate -import androidx.core.app.ActivityCompat -import androidx.core.content.ContextCompat -import androidx.fragment.app.DialogFragment import androidx.lifecycle.lifecycleScope -import androidx.preference.* -import androidx.preference.Preference.OnPreferenceClickListener -import com.google.gson.Gson -import io.heckel.ntfy.BuildConfig +import androidx.preference.ListPreference +import androidx.preference.Preference +import androidx.preference.PreferenceDataStore +import androidx.preference.PreferenceFragmentCompat import io.heckel.ntfy.R import io.heckel.ntfy.db.Repository +import io.heckel.ntfy.db.Subscription import io.heckel.ntfy.db.User import io.heckel.ntfy.log.Log -import io.heckel.ntfy.service.SubscriberService import io.heckel.ntfy.service.SubscriberServiceManager -import io.heckel.ntfy.util.formatBytes -import io.heckel.ntfy.util.formatDateShort -import io.heckel.ntfy.util.shortUrl -import io.heckel.ntfy.util.toPriorityString import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch -import okhttp3.OkHttpClient -import okhttp3.Request -import okhttp3.RequestBody.Companion.toRequestBody -import java.util.* -import java.util.concurrent.TimeUnit /** * Subscription settings @@ -46,6 +23,7 @@ class DetailSettingsActivity : AppCompatActivity() { private lateinit var repository: Repository private lateinit var serviceManager: SubscriberServiceManager private lateinit var settingsFragment: SettingsFragment + private var subscriptionId: Long = 0 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -55,9 +33,13 @@ class DetailSettingsActivity : AppCompatActivity() { repository = Repository.getInstance(this) serviceManager = SubscriberServiceManager(this) + subscriptionId = intent.getLongExtra(DetailActivity.EXTRA_SUBSCRIPTION_ID, 0) if (savedInstanceState == null) { settingsFragment = SettingsFragment() // Empty constructor! + settingsFragment.arguments = Bundle().apply { + this.putLong(DetailActivity.EXTRA_SUBSCRIPTION_ID, subscriptionId) + } supportFragmentManager .beginTransaction() .replace(R.id.settings_layout, settingsFragment) @@ -70,6 +52,11 @@ class DetailSettingsActivity : AppCompatActivity() { supportActionBar?.setDisplayHomeAsUpEnabled(true) } + override fun onSupportNavigateUp(): Boolean { + finish() // Return to previous activity when nav "back" is pressed! + return true + } + class SettingsFragment : PreferenceFragmentCompat() { private lateinit var repository: Repository private lateinit var serviceManager: SubscriberServiceManager @@ -81,10 +68,41 @@ class DetailSettingsActivity : AppCompatActivity() { repository = Repository.getInstance(requireActivity()) serviceManager = SubscriberServiceManager(requireActivity()) - - // xxxxxxxxxxxxxxx + // Load subscription and users + val subscriptionId = arguments?.getLong(DetailActivity.EXTRA_SUBSCRIPTION_ID) ?: return + lifecycleScope.launch(Dispatchers.IO) { + val subscription = repository.getSubscription(subscriptionId) ?: return@launch + val users = repository.getUsers().filter { it.baseUrl == subscription.baseUrl } + activity?.runOnUiThread { + loadView(subscription.id, users) + } + } } + private fun loadView(subscriptionId: Long, 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() { + override fun putString(key: String?, value: String?) { + val authUserId = when (value) { + "" -> null + else -> value?.toLongOrNull() + } + lifecycleScope.launch(Dispatchers.IO) { + Log.d(TAG, "Updating auth user ID to $authUserId for subscription $subscriptionId") + repository.updateSubscriptionAuthUserId(subscriptionId, authUserId) + serviceManager.refresh() + } + } + override fun getString(key: String?, defValue: String?): String? { + Log.d(TAG, "getstring called $key $defValue") + return "xxx" + } + } + } } companion object { diff --git a/app/src/main/res/xml/detail_preferences.xml b/app/src/main/res/xml/detail_preferences.xml index 69d36d3..120c845 100644 --- a/app/src/main/res/xml/detail_preferences.xml +++ b/app/src/main/res/xml/detail_preferences.xml @@ -4,7 +4,7 @@ app:title="@string/detail_settings_auth_header" app:summary="@string/detail_settings_auth_header_summary" app:layout="@layout/preference_category_material_edited"> -