Fix back stack; properly update auth user via detail settings; works

This commit is contained in:
Philipp Heckel 2022-01-30 17:38:33 -05:00
parent 80570eb323
commit f7fbf467b0
7 changed files with 65 additions and 36 deletions

View file

@ -61,9 +61,6 @@
<activity
android:name=".ui.DetailSettingsActivity"
android:parentActivityName=".ui.DetailActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".ui.DetailActivity"/>
</activity>
<!-- Subscriber foreground service for hosts other than ntfy.sh -->

View file

@ -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)

View file

@ -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)
}

View file

@ -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")

View file

@ -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"
}
}

View file

@ -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<User>) {
// 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 {

View file

@ -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">
<Preference
<ListPreference
app:key="@string/detail_settings_auth_user_key"
app:title="@string/detail_settings_auth_user_title"
app:summary="@string/detail_settings_auth_user_summary_none"/>