Simplify settings navigation and copy controls
This commit is contained in:
@@ -104,9 +104,6 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
|
||||
}
|
||||
|
||||
override fun onNavigationItemSelected(item: MenuItem): Boolean {
|
||||
if (item.itemId == R.id.nav_settings_header) {
|
||||
return false
|
||||
}
|
||||
navigateTo(item.itemId)
|
||||
if (!permanentSidebar) {
|
||||
binding.drawerLayout.closeDrawer(GravityCompat.START)
|
||||
@@ -149,13 +146,9 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
|
||||
val menu = navigationView.menu ?: return
|
||||
for (index in 0 until menu.size()) {
|
||||
val item = menu.getItem(index)
|
||||
if (item.itemId == R.id.nav_settings_header) {
|
||||
item.isCheckable = false
|
||||
} else {
|
||||
item.isCheckable = true
|
||||
item.isChecked = item.itemId == itemId
|
||||
}
|
||||
}
|
||||
navigationView.invalidate()
|
||||
}
|
||||
|
||||
|
||||
@@ -5,12 +5,18 @@ import android.content.ClipData
|
||||
import android.net.Uri
|
||||
import android.content.ComponentName
|
||||
import android.content.Intent
|
||||
import android.graphics.Color
|
||||
import android.graphics.drawable.ColorDrawable
|
||||
import android.provider.Settings
|
||||
import android.view.View
|
||||
import android.widget.ArrayAdapter
|
||||
import android.widget.CheckBox
|
||||
import android.widget.LinearLayout
|
||||
import android.widget.PopupWindow
|
||||
import androidx.fragment.app.Fragment
|
||||
import com.google.android.material.switchmaterial.SwitchMaterial
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||
import com.google.android.material.color.MaterialColors
|
||||
import se.ajpanton.notificationlog.databinding.FragmentSettingsBinding
|
||||
import se.ajpanton.notificationlog.settings.LogField
|
||||
import se.ajpanton.notificationlog.settings.LogViewSettingsStore
|
||||
@@ -122,48 +128,80 @@ class SettingsFragment : Fragment(R.layout.fragment_settings) {
|
||||
}
|
||||
|
||||
private fun setupCopyControls() {
|
||||
val labels = listOf("From") + LoggingType.entries.map { it.label() }
|
||||
val labels = listOf("Source") + LoggingType.entries.map { it.label() }
|
||||
binding!!.copyFrom.adapter = ArrayAdapter(requireContext(), android.R.layout.simple_spinner_dropdown_item, labels)
|
||||
binding!!.copyFrom.onItemSelectedListener = object : android.widget.AdapterView.OnItemSelectedListener {
|
||||
override fun onNothingSelected(parent: android.widget.AdapterView<*>?) = Unit
|
||||
override fun onItemSelected(parent: android.widget.AdapterView<*>?, view: View?, position: Int, id: Long) {
|
||||
selectedCopyTargets = emptySet()
|
||||
binding!!.copyTo.isEnabled = position > 0
|
||||
updateCopyButton()
|
||||
binding!!.copyTo.text = "Destination"
|
||||
updateCopyControls()
|
||||
}
|
||||
}
|
||||
binding!!.copyTo.setOnClickListener { chooseCopyTargets(LoggingType.entries[binding!!.copyFrom.selectedItemPosition - 1]) }
|
||||
binding!!.copyTo.setOnClickListener { showDestinationDropdown(LoggingType.entries[binding!!.copyFrom.selectedItemPosition - 1]) }
|
||||
binding!!.copyEventSettings.setOnClickListener {
|
||||
LoggingRuleStore(requireContext()).copy(LoggingType.entries[binding!!.copyFrom.selectedItemPosition - 1], selectedCopyTargets)
|
||||
}
|
||||
}
|
||||
|
||||
private fun chooseCopyTargets(source: LoggingType) {
|
||||
private fun showDestinationDropdown(source: LoggingType) {
|
||||
val targets = LoggingType.entries.filterNot { it == source }
|
||||
val checked = BooleanArray(targets.size) { targets[it] in selectedCopyTargets }
|
||||
val dialog = MaterialAlertDialogBuilder(requireContext())
|
||||
.setTitle("Copy ${source.label()} settings to")
|
||||
.setMultiChoiceItems(targets.map { it.label() }.toTypedArray(), checked) { _, index, selected -> checked[index] = selected }
|
||||
.setNegativeButton("Cancel", null)
|
||||
.setNeutralButton("All", null)
|
||||
.setPositiveButton("Done") { _, _ ->
|
||||
selectedCopyTargets = targets.filterIndexed { index, _ -> checked[index] }.toSet()
|
||||
updateCopyButton()
|
||||
val popupContent = LinearLayout(requireContext()).apply {
|
||||
orientation = LinearLayout.VERTICAL
|
||||
setPadding(dp(8), dp(8), dp(8), dp(8))
|
||||
}
|
||||
.create()
|
||||
dialog.setOnShowListener {
|
||||
dialog.getButton(androidx.appcompat.app.AlertDialog.BUTTON_NEUTRAL).setOnClickListener {
|
||||
val selectAll = checked.any { !it }
|
||||
checked.indices.forEach { index ->
|
||||
checked[index] = selectAll
|
||||
dialog.listView.setItemChecked(index, selectAll)
|
||||
val popup = PopupWindow(popupContent, binding!!.copyTo.width.coerceAtLeast(dp(200)), LinearLayout.LayoutParams.WRAP_CONTENT, true).apply {
|
||||
isOutsideTouchable = true
|
||||
elevation = dp(8).toFloat()
|
||||
setBackgroundDrawable(ColorDrawable(MaterialColors.getColor(requireContext(), com.google.android.material.R.attr.colorSurface, Color.WHITE)))
|
||||
}
|
||||
val boxes = mutableListOf<CheckBox>()
|
||||
popupContent.addView(android.widget.Button(requireContext()).apply {
|
||||
text = "All"
|
||||
setOnClickListener {
|
||||
val selectAll = selectedCopyTargets.size != targets.size
|
||||
selectedCopyTargets = if (selectAll) targets.toSet() else emptySet()
|
||||
boxes.forEachIndexed { index, box -> box.isChecked = targets[index] in selectedCopyTargets }
|
||||
updateCopyControls()
|
||||
}
|
||||
})
|
||||
popupContent.addView(View(requireContext()).apply {
|
||||
layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1).apply {
|
||||
setMargins(0, dp(4), 0, dp(4))
|
||||
}
|
||||
setBackgroundColor(0x33000000)
|
||||
})
|
||||
targets.forEach { target ->
|
||||
val box = CheckBox(requireContext()).apply {
|
||||
text = target.label()
|
||||
isChecked = target in selectedCopyTargets
|
||||
setOnCheckedChangeListener { _, checked ->
|
||||
selectedCopyTargets = selectedCopyTargets.toMutableSet().apply {
|
||||
if (checked) add(target) else remove(target)
|
||||
}
|
||||
updateCopyControls()
|
||||
}
|
||||
}
|
||||
boxes += box
|
||||
popupContent.addView(box)
|
||||
}
|
||||
dialog.show()
|
||||
popup.showAsDropDown(binding!!.copyTo)
|
||||
}
|
||||
|
||||
private fun updateCopyButton() { binding?.copyEventSettings?.isEnabled = selectedCopyTargets.isNotEmpty() }
|
||||
private fun updateCopyControls() {
|
||||
val currentBinding = binding ?: return
|
||||
val sourceChosen = currentBinding.copyFrom.selectedItemPosition > 0
|
||||
currentBinding.copyTo.isEnabled = sourceChosen
|
||||
currentBinding.copyTo.text = destinationLabel()
|
||||
currentBinding.copyEventSettings.isEnabled = sourceChosen && selectedCopyTargets.isNotEmpty()
|
||||
}
|
||||
|
||||
private fun destinationLabel(): String = when (selectedCopyTargets.size) {
|
||||
0 -> "Destination"
|
||||
1 -> selectedCopyTargets.first().label()
|
||||
else -> "${selectedCopyTargets.sortedBy { it.ordinal }.first().label()}..."
|
||||
}
|
||||
|
||||
private fun isListenerEnabled(): Boolean = requireContext().getSystemService(android.app.NotificationManager::class.java)
|
||||
.isNotificationListenerAccessGranted(ComponentName(requireContext(), NotificationCaptureService::class.java))
|
||||
@@ -229,5 +267,7 @@ class SettingsFragment : Fragment(R.layout.fragment_settings) {
|
||||
|
||||
private fun LoggingType.label() = name.lowercase().replace('_', ' ').replaceFirstChar(Char::uppercase)
|
||||
|
||||
private fun dp(value: Int): Int = (value * resources.displayMetrics.density).toInt()
|
||||
|
||||
private enum class ExportFormat(val extension: String) { CSV("csv"), FORMATTED("txt"), HTML_ZIP("zip") }
|
||||
}
|
||||
|
||||
@@ -54,19 +54,48 @@
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="24dp"
|
||||
android:text="Copy event settings" />
|
||||
android:text="Copy settings between pages" />
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="center_vertical"
|
||||
android:orientation="horizontal">
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Copy from" />
|
||||
|
||||
<Spinner
|
||||
android:id="@+id/copy_from"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="12dp"
|
||||
android:layout_weight="1" />
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content" />
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="center_vertical"
|
||||
android:orientation="horizontal">
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="to" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/copy_to"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="12dp"
|
||||
android:layout_weight="1"
|
||||
android:enabled="false"
|
||||
android:text="Choose targets" />
|
||||
android:gravity="start|center_vertical"
|
||||
android:text="Destination" />
|
||||
</LinearLayout>
|
||||
|
||||
<Button
|
||||
android:id="@+id/copy_event_settings"
|
||||
|
||||
@@ -5,15 +5,10 @@
|
||||
android:id="@+id/nav_view_logs"
|
||||
android:title="@string/page_view_logs" />
|
||||
</group>
|
||||
<item
|
||||
android:id="@+id/nav_settings_header"
|
||||
android:checkable="false"
|
||||
android:enabled="false"
|
||||
android:title="@string/navigation_settings_header" />
|
||||
<group android:checkableBehavior="single">
|
||||
<item
|
||||
android:id="@+id/nav_settings"
|
||||
android:title="@string/navigation_indented_settings" />
|
||||
android:title="@string/navigation_settings_header" />
|
||||
<item
|
||||
android:id="@+id/nav_appearing"
|
||||
android:title="@string/navigation_indented_appearing" />
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<string name="notification_listener_label">Notification Log listener</string>
|
||||
<string name="navigation_open">Open navigation</string>
|
||||
<string name="navigation_close">Close navigation</string>
|
||||
<string name="navigation_settings_header">Settings:</string>
|
||||
<string name="navigation_settings_header">Settings</string>
|
||||
<string name="page_view_logs">View logs</string>
|
||||
<string name="page_settings">Settings</string>
|
||||
<string name="page_appearing">Appearing</string>
|
||||
@@ -13,7 +13,6 @@
|
||||
<string name="page_text_content">Text content</string>
|
||||
<string name="page_image_content">Image content</string>
|
||||
<string name="page_edits">Edits</string>
|
||||
<string name="navigation_indented_settings">    Settings</string>
|
||||
<string name="navigation_indented_appearing">    Appearing</string>
|
||||
<string name="navigation_indented_disappearing">    Disappearing</string>
|
||||
<string name="navigation_indented_text_content">    Text content</string>
|
||||
|
||||
Reference in New Issue
Block a user