WIP User edit dialog
This commit is contained in:
parent
678e5625dd
commit
a8dd3bebc0
4 changed files with 141 additions and 6 deletions
|
@ -67,9 +67,6 @@ class SettingsActivity : AppCompatActivity(), PreferenceFragmentCompat.OnPrefere
|
|||
}
|
||||
}
|
||||
|
||||
// Action bar
|
||||
//title = getString(R.string.settings_title)
|
||||
|
||||
// Show 'Back' button
|
||||
supportActionBar?.setDisplayHomeAsUpEnabled(true)
|
||||
}
|
||||
|
@ -524,14 +521,28 @@ class SettingsActivity : AppCompatActivity(), PreferenceFragmentCompat.OnPrefere
|
|||
repository = Repository.getInstance(requireActivity())
|
||||
|
||||
lifecycleScope.launch(Dispatchers.IO) {
|
||||
val usersByBaseUrl = repository.getUsers().groupBy { it.baseUrl }
|
||||
val userIdsWithTopics = repository.getSubscriptions()
|
||||
.groupBy { it.authUserId }
|
||||
.mapValues { e -> e.value.map { it.topic } }
|
||||
val usersByBaseUrl = repository.getUsers()
|
||||
.map { user ->
|
||||
val topics = userIdsWithTopics[user.id] ?: emptyList()
|
||||
UserWithMetadata(user, topics)
|
||||
}
|
||||
.groupBy { it.user.baseUrl }
|
||||
|
||||
activity?.runOnUiThread {
|
||||
addUserPreferences(usersByBaseUrl)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun addUserPreferences(usersByBaseUrl: Map<String, List<User>>) {
|
||||
data class UserWithMetadata(
|
||||
val user: User,
|
||||
val topics: List<String>
|
||||
)
|
||||
|
||||
private fun addUserPreferences(usersByBaseUrl: Map<String, List<UserWithMetadata>>) {
|
||||
usersByBaseUrl.forEach { entry ->
|
||||
val baseUrl = entry.key
|
||||
val users = entry.value
|
||||
|
@ -542,7 +553,20 @@ class SettingsActivity : AppCompatActivity(), PreferenceFragmentCompat.OnPrefere
|
|||
|
||||
users.forEach { user ->
|
||||
val preference = Preference(preferenceScreen.context)
|
||||
preference.title = user.username
|
||||
preference.title = user.user.username
|
||||
preference.summary = if (user.topics.isEmpty()) {
|
||||
getString(R.string.settings_users_prefs_user_not_used)
|
||||
} else if (user.topics.size == 1) {
|
||||
getString(R.string.settings_users_prefs_user_used_by_one, user.topics[0])
|
||||
} else {
|
||||
getString(R.string.settings_users_prefs_user_used_by_many, user.topics.joinToString(", "))
|
||||
}
|
||||
preference.onPreferenceClickListener = OnPreferenceClickListener { _ ->
|
||||
activity?.let {
|
||||
UserFragment().show(it.supportFragmentManager, UserFragment.TAG)
|
||||
}
|
||||
true
|
||||
}
|
||||
preferenceCategory.addPreference(preference)
|
||||
}
|
||||
}
|
||||
|
|
57
app/src/main/java/io/heckel/ntfy/ui/UserFragment.kt
Normal file
57
app/src/main/java/io/heckel/ntfy/ui/UserFragment.kt
Normal file
|
@ -0,0 +1,57 @@
|
|||
package io.heckel.ntfy.ui
|
||||
|
||||
import android.app.AlertDialog
|
||||
import android.app.Dialog
|
||||
import android.os.Bundle
|
||||
import android.view.WindowManager
|
||||
import android.widget.TextView
|
||||
import androidx.fragment.app.DialogFragment
|
||||
import io.heckel.ntfy.R
|
||||
|
||||
class UserFragment : DialogFragment() {
|
||||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
||||
if (activity == null) {
|
||||
throw IllegalStateException("Activity cannot be null")
|
||||
}
|
||||
|
||||
// Build root view
|
||||
val view = requireActivity().layoutInflater.inflate(R.layout.fragment_user_dialog, null)
|
||||
|
||||
val addMode = false // FIXME
|
||||
val positiveButtonTextResId = if (addMode) R.string.user_dialog_button_add else R.string.user_dialog_button_save
|
||||
val titleText = view.findViewById(R.id.user_dialog_title) as TextView
|
||||
titleText.text = if (addMode) {
|
||||
getString(R.string.user_dialog_title_add)
|
||||
} else {
|
||||
getString(R.string.user_dialog_title_edit)
|
||||
}
|
||||
|
||||
// Build dialog
|
||||
val dialog = AlertDialog.Builder(activity)
|
||||
.setView(view)
|
||||
.setPositiveButton(positiveButtonTextResId) { _, _ ->
|
||||
// This will be overridden below to avoid closing the dialog immediately
|
||||
}
|
||||
.setNegativeButton(R.string.user_dialog_button_cancel) { _, _ ->
|
||||
// This will be overridden below
|
||||
}
|
||||
.setNeutralButton(R.string.user_dialog_button_delete) { _, _ ->
|
||||
// This will be overridden below
|
||||
}
|
||||
.create()
|
||||
|
||||
// Show keyboard when the dialog is shown (see https://stackoverflow.com/a/19573049/1440785)
|
||||
dialog.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
|
||||
|
||||
// Add logic to disable "Subscribe" button on invalid input
|
||||
dialog.setOnShowListener {
|
||||
|
||||
}
|
||||
|
||||
return dialog
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val TAG = "NtfyUserFragment"
|
||||
}
|
||||
}
|
41
app/src/main/res/layout/fragment_user_dialog.xml
Normal file
41
app/src/main/res/layout/fragment_user_dialog.xml
Normal file
|
@ -0,0 +1,41 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="horizontal"
|
||||
android:paddingLeft="16dp"
|
||||
android:paddingRight="16dp"
|
||||
android:visibility="visible">
|
||||
<TextView
|
||||
android:id="@+id/user_dialog_title"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingTop="16dp"
|
||||
android:paddingBottom="3dp"
|
||||
android:text="@string/user_dialog_title_add"
|
||||
android:textAlignment="viewStart"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Large" android:paddingStart="4dp"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"/>
|
||||
|
||||
<com.google.android.material.textfield.TextInputEditText
|
||||
android:id="@+id/user_dialog_username"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content" android:hint="@string/user_dialog_username_hint"
|
||||
android:importantForAutofill="no"
|
||||
android:maxLines="1" android:inputType="text"
|
||||
app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"
|
||||
android:layout_marginTop="6dp" app:layout_constraintTop_toBottomOf="@+id/user_dialog_title"/>
|
||||
<com.google.android.material.textfield.TextInputEditText
|
||||
android:id="@+id/user_dialog_password"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content" android:hint="@string/user_dialog_password_hint"
|
||||
android:importantForAutofill="no"
|
||||
android:maxLines="1" android:inputType="textPassword"
|
||||
app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"
|
||||
android:layout_marginTop="6dp" app:layout_constraintTop_toBottomOf="@id/user_dialog_username"/>
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
|
@ -243,6 +243,9 @@
|
|||
<string name="settings_users_title">Manage users</string>
|
||||
<string name="settings_users_summary">Add or remove users used for protected topics</string>
|
||||
<string name="settings_users_prefs_title">Users</string>
|
||||
<string name="settings_users_prefs_user_not_used">Not used by any topics</string>
|
||||
<string name="settings_users_prefs_user_used_by_one">Used by topic %1$s</string>
|
||||
<string name="settings_users_prefs_user_used_by_many">Used by topics %1$s</string>
|
||||
<string name="settings_unified_push_header">UnifiedPush</string>
|
||||
<string name="settings_unified_push_header_summary">Allows other apps to use ntfy as a message distributor. Find out more at unifiedpush.org.</string>
|
||||
<string name="settings_unified_push_enabled_key">UnifiedPushEnabled</string>
|
||||
|
@ -290,4 +293,14 @@
|
|||
<string name="settings_about_version_title">Version</string>
|
||||
<string name="settings_about_version_format">ntfy %1$s (%2$s)</string>
|
||||
<string name="settings_about_version_copied_to_clipboard_message">Copied to clipboard</string>
|
||||
|
||||
<!-- User add/edit dialog -->
|
||||
<string name="user_dialog_title_add">Add user</string>
|
||||
<string name="user_dialog_title_edit">Edit user</string>
|
||||
<string name="user_dialog_username_hint">Username</string>
|
||||
<string name="user_dialog_password_hint">Password</string>
|
||||
<string name="user_dialog_button_add">Add user</string>
|
||||
<string name="user_dialog_button_cancel">Cancel</string>
|
||||
<string name="user_dialog_button_delete">Delete user</string>
|
||||
<string name="user_dialog_button_save">Save</string>
|
||||
</resources>
|
||||
|
|
Loading…
Reference in a new issue