Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sort to shared prefs tool #72

Merged
merged 5 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@ package com.infinum.sentinel.data.models.raw

internal data class PreferencesData(
val name: String,
val values: List<Triple<PreferenceType, String, Any>>
val values: List<Triple<PreferenceType, String, Any>>,
val isSortedAscending: Boolean = false,
val isExpanded: Boolean = true,
)
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ internal class PreferencesCollector(
prefsDirectory.list().orEmpty().toList().map { it.removeSuffix(PREFS_SUFFIX) }
} else {
listOf()
}.sortedBy { name ->
name
}.map { name ->
val allPrefs = getSharedPreferences(name, MODE_PRIVATE).all
val tuples = allPrefs.keys.toSet().mapNotNull {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,32 +64,51 @@ internal class PreferencesFragment :
}
}

@Suppress("UNCHECKED_CAST")
private fun createItemView(data: PreferencesData): View =
SentinelViewItemPreferenceBinding.inflate(layoutInflater, binding.contentLayout, false)
.apply {
nameView.text = data.name
data.values.forEach { tuple ->
prefsLayout.addView(
SentinelViewItemTextBinding.inflate(layoutInflater, prefsLayout, false)
.apply {
labelView.isAllCaps = false
labelView.text = tuple.second
valueView.text = tuple.third.toString()
root.setOnClickListener { _ ->
viewModel.cache(
data.name,
tuple
)
}
root.setOnLongClickListener {
it.context.copyToClipboard(
key = tuple.second,
value = tuple.third.toString()
)
}
}.root
)
sortImageView.setOnClickListener {
viewModel.onSortClicked(data)
}
hideExpandImageView.setOnClickListener {
viewModel.onHideExpandClicked(data)
}

if (data.isExpanded) {
prefsLayout.visibility = View.VISIBLE
sortImageView.visibility = View.VISIBLE
hideExpandImageView.setImageResource(R.drawable.sentinel_ic_minus)
showPreferenceData(data)
} else {
prefsLayout.visibility = View.GONE
sortImageView.visibility = View.GONE
hideExpandImageView.setImageResource(R.drawable.sentinel_ic_plus)
}
}.root

private fun SentinelViewItemPreferenceBinding.showPreferenceData(data: PreferencesData) {
data.values.forEach { (preferenceType, label, value) ->
prefsLayout.addView(
SentinelViewItemTextBinding.inflate(layoutInflater, prefsLayout, false)
.apply {
labelView.isAllCaps = false
labelView.text = label
valueView.text = value.toString()
root.setOnClickListener { _ ->
viewModel.cache(
name = data.name,
tuple = Triple(preferenceType, label, value)
)
}
root.setOnLongClickListener {
it.context.copyToClipboard(
key = label,
value = value.toString()
)
}
}.root
)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.infinum.sentinel.ui.main.preferences

import com.infinum.sentinel.data.models.raw.PreferenceType
import com.infinum.sentinel.data.models.raw.PreferencesData
import com.infinum.sentinel.domain.Factories
import com.infinum.sentinel.domain.Repositories
import com.infinum.sentinel.domain.preference.models.PreferenceParameters
Expand Down Expand Up @@ -38,4 +39,40 @@ internal class PreferencesViewModel(
}
emitEvent(PreferencesEvent.Cached())
}

fun onSortClicked(data: PreferencesData) {
val currentValues = (stateFlow.value as? PreferencesState.Data)?.value.orEmpty()
val sortedData = if (data.isSortedAscending) {
data.values.sortedByDescending { it.second }
} else {
data.values.sortedBy { it.second }
}
val changedValues = currentValues.map { preferencesData ->
if (preferencesData.name == data.name) {
preferencesData.copy(
values = sortedData,
isSortedAscending = !preferencesData.isSortedAscending
)
} else {
preferencesData
}
}
setState(PreferencesState.Data(value = changedValues))
}

fun onHideExpandClicked(data: PreferencesData) {
(stateFlow.value as? PreferencesState.Data)?.let { state ->
val currentValues = state.value
val changedValues = currentValues.map { preferencesData ->
if (preferencesData.name == data.name) {
preferencesData.copy(
isExpanded = !preferencesData.isExpanded
)
} else {
preferencesData
}
}
setState(PreferencesState.Data(value = changedValues))
}
}
}
11 changes: 11 additions & 0 deletions sentinel/src/main/res/drawable/sentinel_ic_sort.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">

<path
android:fillColor="?android:textColorSecondary"
android:pathData="M14.94,4.66h-4.72l2.36,-2.36zM10.25,19.37h4.66l-2.33,2.33zM6.1,6.27L1.6,17.73h1.84l0.92,-2.45h5.11l0.92,2.45h1.84L7.74,6.27L6.1,6.27zM4.97,13.64l1.94,-5.18 1.94,5.18L4.97,13.64zM15.73,16.14h6.12v1.59h-8.53v-1.29l5.92,-8.56h-5.88v-1.6h8.3v1.26l-5.93,8.6z" />

</vector>
41 changes: 36 additions & 5 deletions sentinel/src/main/res/layout/sentinel_view_item_preference.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,49 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingStart="16dp"
android:paddingEnd="16dp">

<com.google.android.material.textview.MaterialTextView
android:id="@+id/nameView"
style="@style/TextAppearance.Material3.BodySmall"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:textColor="@color/sentinel_primary" />
android:orientation="horizontal"
android:paddingBottom="8dp">

<com.google.android.material.textview.MaterialTextView
android:id="@+id/nameView"
style="@style/TextAppearance.Material3.BodySmall"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_weight="1"
android:textColor="@color/sentinel_primary"
tools:text="Preferences" />

<ImageView
android:id="@+id/sortImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:contentDescription="@string/sentinel_sort_preference_by_name"
android:src="@drawable/sentinel_ic_sort" />

<View
android:layout_width="16dp"
android:layout_height="match_parent" />

<ImageView
android:id="@+id/hideExpandImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:contentDescription="@string/sentinel_hide_expands_the_preferences"
android:src="@drawable/sentinel_ic_plus" />

</LinearLayout>

<LinearLayout
android:id="@+id/prefsLayout"
Expand Down
2 changes: 2 additions & 0 deletions sentinel/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@
<string name="sentinel_fragment_arguments">Fragment arguments</string>
<string name="sentinel_fragment_saved_state">Fragment Saved State</string>

<string name="sentinel_sort_preference_by_name">Sort preferences by name</string>
<string name="sentinel_hide_expands_the_preferences">Hide/expand the preferences</string>
<string name="sentinel_preferences_editor">Preferences editor</string>
<string name="sentinel_key">Key</string>
<string name="sentinel_current_value">Current value</string>
Expand Down
Loading