Skip to content

Commit

Permalink
Use Android library for settings activity
Browse files Browse the repository at this point in the history
Use external library for lead and follow colorization.
  • Loading branch information
chrgernoe committed Feb 18, 2024
1 parent c9eec4b commit 51f09d7
Show file tree
Hide file tree
Showing 9 changed files with 260 additions and 261 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ dependencies {
* https://developer.chrome.com/docs/android/custom-tabs/
*/
implementation 'androidx.browser:browser:1.7.0'

implementation "androidx.preference:preference-ktx:1.2.1"
implementation "com.github.skydoves:colorpickerpreference:2.0.6"
}
repositories {
mavenCentral()
Expand Down
41 changes: 41 additions & 0 deletions app/src/main/java/com/yacgroup/yacguide/PreferenceFragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (C) 2024 Christian Sommer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.yacgroup.yacguide

import android.content.Context
import android.os.Bundle
import androidx.preference.PreferenceFragmentCompat
import com.skydoves.colorpickerpreference.ColorPickerPreference
import com.yacgroup.yacguide.extensions.getSharedPreferenceManager
import com.yacgroup.yacguide.extensions.setSharedPreferences

class PreferenceFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
preferenceManager.sharedPreferencesName = getString(R.string.preferences_filename)
setPreferencesFromResource(R.xml.preferences, rootKey)
for (stringResource in listOf(R.string.lead, R.string.follow)) {
findPreference<ColorPickerPreference>(getString(stringResource))?.getColorPickerView()?.let { view ->
context?.let { context ->
view.getSharedPreferenceManager().setSharedPreferences(
context.getSharedPreferences(getString(R.string.preferences_filename), Context.MODE_PRIVATE)
)
}
}
}
}
}
99 changes: 17 additions & 82 deletions app/src/main/java/com/yacgroup/yacguide/PreferencesActivity.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2020, 2022, 2023 Axel Paetzold
* 2024 Christian Sommer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -19,13 +20,9 @@ package com.yacgroup.yacguide

import android.content.Context
import android.content.SharedPreferences
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import androidx.core.content.ContextCompat
import android.view.View
import android.widget.Button
import android.widget.CheckBox
import android.widget.Toast
import androidx.preference.Preference
import com.yacgroup.yacguide.database.DatabaseWrapper
import com.yacgroup.yacguide.utils.DialogWidgetBuilder

Expand All @@ -34,29 +31,6 @@ class PreferencesActivity : BaseNavigationActivity() {
private lateinit var _db: DatabaseWrapper
private lateinit var _customSettings: SharedPreferences

private val _settingKeysMap = mapOf(R.id.tourbookOrderingCheckbox to Pair(R.string.order_tourbook_chronologically,
R.bool.order_tourbook_chronologically),
R.id.countSummitsCheckbox to Pair(R.string.count_summits,
R.bool.count_summits),
R.id.countMassifsCheckbox to Pair(R.string.count_massifs,
R.bool.count_massifs),
R.id.countBouldersCheckbox to Pair(R.string.count_boulders,
R.bool.count_boulders),
R.id.countCavesCheckbox to Pair(R.string.count_caves,
R.bool.count_caves),
R.id.countUnofficialRocks to Pair(R.string.count_unofficial_rocks,
R.bool.count_unofficial_rocks),
R.id.countProhibitedRocks to Pair(R.string.count_prohibited_rocks,
R.bool.count_prohibited_rocks),
R.id.countCollapsedRocks to Pair(R.string.count_collapsed_rocks,
R.bool.count_collapsed_rocks),
R.id.countOnlyLeadsCheckbox to Pair(R.string.count_only_leads,
R.bool.count_only_leads),
R.id.colorizeTourbookEntriesCheckbox to Pair(R.string.colorize_tourbook_entries,
R.bool.colorize_tourbook_entries))

private lateinit var _ascendColorsList: List<Int>

override fun getLayoutId() = R.layout.activity_preferences

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -65,31 +39,11 @@ class PreferencesActivity : BaseNavigationActivity() {

_db = DatabaseWrapper(this)
_customSettings = getSharedPreferences(getString(R.string.preferences_filename), Context.MODE_PRIVATE)
_ascendColorsList = listOf(
ContextCompat.getColor(this, R.color.greenblue),
ContextCompat.getColor(this, R.color.green),
ContextCompat.getColor(this, R.color.yellow),
ContextCompat.getColor(this, R.color.red),
ContextCompat.getColor(this, R.color.purple),
ContextCompat.getColor(this, R.color.blue),
ContextCompat.getColor(this, R.color.white))

_displayContent()
}

override fun onStop() {
_storeSettings()
super.onStop()
}

fun changeColor(view: View) {
val currColor = (view.background as ColorDrawable).color
val nextColor = _ascendColorsList[(_ascendColorsList.indexOf(currColor) + 1) % _ascendColorsList.size]
view.setBackgroundColor(nextColor)
}

@Suppress("UNUSED_PARAMETER")
fun resetDatabase(view: View) {
private fun _resetDatabase() {
DialogWidgetBuilder(this, R.string.reset_database).apply {
setMessage(R.string.reset_database_confirm)
setNegativeButton()
Expand All @@ -98,22 +52,13 @@ class PreferencesActivity : BaseNavigationActivity() {
_db.deleteAscends()
_resetCustomSettings()
Toast.makeText(this.context, R.string.reset_database_done, Toast.LENGTH_SHORT).show()
}
}
}.show()
}

private fun _resetCustomSettings() {
val context = this
_customSettings.edit().apply {
for ((_, keyPair) in _settingKeysMap) {
putBoolean(getString(keyPair.first), resources.getBoolean(keyPair.second))
}
putInt(
getString(R.string.lead),
ContextCompat.getColor(context, R.color.color_lead))
putInt(
getString(R.string.follow),
ContextCompat.getColor(context, R.color.color_follow))
clear().apply()
putInt(
getString(R.string.default_region_key),
resources.getInteger(R.integer.default_region_id))
Expand All @@ -125,29 +70,19 @@ class PreferencesActivity : BaseNavigationActivity() {
_displayContent()
}

private fun _storeSettings() {
_customSettings.edit().apply {
for ((checkboxId, keyPair) in _settingKeysMap) {
putBoolean(getString(keyPair.first), findViewById<CheckBox>(checkboxId).isChecked)
}
putInt(
getString(R.string.lead),
(findViewById<Button>(R.id.leadColorButton).background as ColorDrawable).color)
putInt(
getString(R.string.follow),
(findViewById<Button>(R.id.followColorButton).background as ColorDrawable).color)
}.apply()
}

private fun _displayContent() {
for ((checkboxId, keyPair) in _settingKeysMap) {
findViewById<CheckBox>(checkboxId).isChecked = _customSettings.getBoolean(
getString(keyPair.first),
resources.getBoolean(keyPair.second))
PreferenceFragment().let { it ->
supportFragmentManager.apply {
beginTransaction()
.replace(R.id.preference_container, it)
.commit()
// Necessary otherwise the method findPreference below will not find anything.
executePendingTransactions()
}
it.findPreference<Preference>("reset_database")?.setOnPreferenceClickListener { _ ->
_resetDatabase()
true
}
}
findViewById<Button>(R.id.leadColorButton).setBackgroundColor(_customSettings.getInt(getString(R.string.lead),
ContextCompat.getColor(this, R.color.color_lead)))
findViewById<Button>(R.id.followColorButton).setBackgroundColor(_customSettings.getInt(getString(R.string.follow),
ContextCompat.getColor(this, R.color.color_follow)))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (C) 2024 Christian Sommer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.yacgroup.yacguide.extensions

import android.content.SharedPreferences
import com.skydoves.colorpickerview.preference.ColorPickerPreferenceManager

/*
* See https://stackoverflow.com/questions/45131683/kotlin-extension-function-access-java-private-field
*
* TODO: Remove, if the following feature request is implemented: https://github.com/skydoves/ColorPickerView/issues/145
*/
fun ColorPickerPreferenceManager.setSharedPreferences(sharedPreferences: SharedPreferences) {
ColorPickerPreferenceManager::class.java.getDeclaredField("sharedPreferences").let { field ->
field.isAccessible = true
field.set(this, sharedPreferences)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (C) 2024 Christian Sommer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.yacgroup.yacguide.extensions

import com.skydoves.colorpickerview.ColorPickerView
import com.skydoves.colorpickerview.preference.ColorPickerPreferenceManager

/*
* See https://stackoverflow.com/questions/45131683/kotlin-extension-function-access-java-private-field
*
* TODO: Remove, if the following feature request is implemented: https://github.com/skydoves/ColorPickerView/issues/145
*/
fun ColorPickerView.getSharedPreferenceManager(): ColorPickerPreferenceManager {
return ColorPickerView::class.java.getDeclaredField("preferenceManager").let { field ->
field.isAccessible = true
return@let field.get(this) as ColorPickerPreferenceManager
}
}
5 changes: 4 additions & 1 deletion app/src/main/res/layout/activity_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@

<include layout="@layout/appbar_empty"/>

<include layout="@layout/content_preferences"/>
<androidx.fragment.app.FragmentContainerView
android:id="@+id/preference_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

</LinearLayout>

Expand Down
Loading

0 comments on commit 51f09d7

Please sign in to comment.