aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/dev/msfjarvis/aps/ui/settings/SettingsActivity.kt
blob: 7db800231c61381cc6b23d57a26d982e4b070395 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 * Copyright © 2014-2020 The Android Password Store Authors. All Rights Reserved.
 * SPDX-License-Identifier: GPL-3.0-only
 */

package dev.msfjarvis.aps.ui.settings

import de.Maxr1998.modernpreferences.PreferencesAdapter
import de.Maxr1998.modernpreferences.helpers.screen
import de.Maxr1998.modernpreferences.helpers.subScreen
import dev.msfjarvis.aps.R
import dev.msfjarvis.aps.databinding.ActivityPreferenceRecyclerviewBinding
import dev.msfjarvis.aps.util.extensions.viewBinding
import android.os.Bundle
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity

class SettingsActivity : AppCompatActivity() {

    private val miscSettings = MiscSettings(this)
    private val autofillSettings = AutofillSettings(this)
    private val passwordSettings = PasswordSettings(this)
    private val repositorySettings = RepositorySettings(this)
    private val generalSettings = GeneralSettings(this)

    private val binding by viewBinding(ActivityPreferenceRecyclerviewBinding::inflate)
    private val preferencesAdapter: PreferencesAdapter
        get() = binding.preferenceRecyclerView.adapter as PreferencesAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        val screen = screen(this) {
            subScreen {
                titleRes = R.string.pref_category_general_title
                iconRes = R.drawable.app_settings_alt_24px
                generalSettings.provideSettings(this)
            }
            subScreen {
                titleRes = R.string.pref_category_autofill_title
                iconRes = R.drawable.ic_wysiwyg_24px
                autofillSettings.provideSettings(this)
            }
            subScreen {
                titleRes = R.string.pref_category_passwords_title
                iconRes = R.drawable.ic_lock_open_24px
                passwordSettings.provideSettings(this)
            }
            subScreen {
                titleRes = R.string.pref_category_repository_title
                iconRes = R.drawable.ic_call_merge_24px
                repositorySettings.provideSettings(this)
            }
            subScreen {
                titleRes = R.string.pref_category_misc_title
                iconRes = R.drawable.ic_miscellaneous_services_24px
                miscSettings.provideSettings(this)
            }
        }
        val adapter = PreferencesAdapter(screen)
        adapter.onScreenChangeListener = PreferencesAdapter.OnScreenChangeListener { subScreen, entering ->
            supportActionBar?.title = if (!entering) {
                getString(R.string.action_settings)
            } else {
                getString(subScreen.titleRes)
            }
        }
        savedInstanceState?.getParcelable<PreferencesAdapter.SavedState>("adapter")
            ?.let(adapter::loadSavedState)
        binding.preferenceRecyclerView.adapter = adapter
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        outState.putParcelable("adapter", preferencesAdapter.getSavedState())
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return when (item.itemId) {
            android.R.id.home -> if (!preferencesAdapter.goBack()) {
                super.onOptionsItemSelected(item)
            } else {
                true
            }
            else -> super.onOptionsItemSelected(item)
        }
    }

    override fun onBackPressed() {
        if (!preferencesAdapter.goBack())
            super.onBackPressed()
    }
}