Convert SettingsFragment to Kotlin
This commit is contained in:
parent
ec3663671e
commit
6a242cbc46
2 changed files with 63 additions and 69 deletions
|
@ -1,69 +0,0 @@
|
|||
package com.wbrawner.simplemarkdown.view.fragment;
|
||||
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Bundle;
|
||||
import android.preference.ListPreference;
|
||||
import android.preference.Preference;
|
||||
import android.preference.PreferenceFragment;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.wbrawner.simplemarkdown.BuildConfig;
|
||||
import com.wbrawner.simplemarkdown.R;
|
||||
|
||||
public class SettingsFragment extends PreferenceFragment
|
||||
implements SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
@Override
|
||||
public void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
addPreferencesFromResource(R.xml.pref_general);
|
||||
SharedPreferences sharedPreferences =
|
||||
PreferenceManager.getDefaultSharedPreferences(getActivity());
|
||||
sharedPreferences.registerOnSharedPreferenceChangeListener(this);
|
||||
setListPreferenceSummary(
|
||||
sharedPreferences,
|
||||
findPreference(getString(R.string.key_default_view))
|
||||
);
|
||||
if (!BuildConfig.ENABLE_CUSTOM_CSS) {
|
||||
getPreferenceScreen().removePreference(findPreference(getString(R.string.pref_custom_css)));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
|
||||
@Nullable Bundle savedInstanceState) {
|
||||
|
||||
return inflater.inflate(R.layout.preference_list_fragment_safe, container, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
||||
Preference preference = findPreference(key);
|
||||
|
||||
if (preference instanceof ListPreference) {
|
||||
setListPreferenceSummary(sharedPreferences, preference);
|
||||
}
|
||||
}
|
||||
|
||||
private void setListPreferenceSummary(SharedPreferences sharedPreferences, Preference preference) {
|
||||
ListPreference listPreference = (ListPreference) preference;
|
||||
String storedValue = sharedPreferences.getString(preference.getKey(), "");
|
||||
if (storedValue.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
int index = 0;
|
||||
try {
|
||||
index = Integer.valueOf(storedValue);
|
||||
} catch (NumberFormatException e) {
|
||||
// TODO: Report this?
|
||||
Log.e("SimpleMarkdown", "Unable to parse " + storedValue + " to integer");
|
||||
}
|
||||
String summary = listPreference.getEntries()[index].toString();
|
||||
preference.setSummary(summary);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
package com.wbrawner.simplemarkdown.view.fragment
|
||||
|
||||
import android.content.SharedPreferences
|
||||
import android.os.Bundle
|
||||
import android.preference.ListPreference
|
||||
import android.preference.Preference
|
||||
import android.preference.PreferenceFragment
|
||||
import android.preference.PreferenceManager
|
||||
import android.util.Log
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
|
||||
import com.wbrawner.simplemarkdown.BuildConfig
|
||||
import com.wbrawner.simplemarkdown.R
|
||||
|
||||
class SettingsFragment : PreferenceFragment(), SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
addPreferencesFromResource(R.xml.pref_general)
|
||||
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(activity)
|
||||
sharedPreferences.registerOnSharedPreferenceChangeListener(this)
|
||||
setListPreferenceSummary(
|
||||
sharedPreferences,
|
||||
findPreference(getString(R.string.key_default_view))
|
||||
)
|
||||
if (!BuildConfig.ENABLE_CUSTOM_CSS) {
|
||||
preferenceScreen.removePreference(findPreference(getString(R.string.pref_custom_css)))
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
|
||||
savedInstanceState: Bundle?): View? {
|
||||
|
||||
return inflater.inflate(R.layout.preference_list_fragment_safe, container, false)
|
||||
}
|
||||
|
||||
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String) {
|
||||
val preference = findPreference(key)
|
||||
|
||||
if (preference is ListPreference) {
|
||||
setListPreferenceSummary(sharedPreferences, preference)
|
||||
}
|
||||
}
|
||||
|
||||
private fun setListPreferenceSummary(sharedPreferences: SharedPreferences, preference: Preference) {
|
||||
val listPreference = preference as ListPreference
|
||||
val storedValue = sharedPreferences.getString(preference.getKey(), "")
|
||||
if (storedValue!!.isEmpty()) {
|
||||
return
|
||||
}
|
||||
var index = 0
|
||||
try {
|
||||
index = Integer.valueOf(storedValue)
|
||||
} catch (e: NumberFormatException) {
|
||||
// TODO: Report this?
|
||||
Log.e("SimpleMarkdown", "Unable to parse $storedValue to integer")
|
||||
}
|
||||
|
||||
val summary = listPreference.entries[index].toString()
|
||||
preference.setSummary(summary)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue