Bimba.git

ref: 2c61d7b87d057c7833f0974dfe17e2e70e12443b

app/src/main/java/ml/adamsprogs/bimba/activities/SettingsActivity.kt


 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
package ml.adamsprogs.bimba.activities

import android.preference.*
import android.os.Bundle

import ml.adamsprogs.bimba.*

// todo create layout with toolbar and fragment; and put fragment here
class SettingsActivity: AppCompatPreferenceActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar.setDisplayHomeAsUpEnabled(true)

        fragmentManager.beginTransaction().replace(android.R.id.content, MainPreferenceFragment()).commit()
    }


    class MainPreferenceFragment : PreferenceFragment() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            addPreferencesFromResource(R.xml.pref_main)

            bindPreferenceSummaryToValue(findPreference(getString(R.string.key_timetable_source_url)))
        }

        private fun bindPreferenceSummaryToValue(preference: Preference) {
            preference.onPreferenceChangeListener = bindPreferenceSummaryToValueListener

            bindPreferenceSummaryToValueListener.onPreferenceChange(preference,
                    PreferenceManager
                            .getDefaultSharedPreferences(preference.context)
                            .getString(preference.key, ""))
        }

        private val bindPreferenceSummaryToValueListener = Preference.OnPreferenceChangeListener { preference, newValue ->
            val stringValue = newValue.toString()
            if (preference is EditTextPreference) {
                if (preference.getKey() == getString(R.string.key_timetable_source_url)) {
                    preference.summary = stringValue
                }
            } else {
                preference.summary = stringValue
            }
            true
        }
    }
}