From f9a831eb5d805b60d603b919c54062277553f795 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Mon, 1 Dec 2014 15:48:19 -0500 Subject: [PATCH] Settings menu shows the font size --- app/src/main/AndroidManifest.xml | 4 +- .../samples/SeekBarDialogPreference.java | 133 ++++++++++++++++++ .../activity/downloader/DownloadPrefs.java | 5 +- .../activity/viewer/BibleViewer.java | 6 +- .../viewer/BibleViewerPreferences.java | 2 +- .../activity/settings/MinimalBibleSettings.kt | 16 +++ .../activity/viewer/BookAdapter.kt | 4 +- .../layout/seekbardialogpreference_layout.xml | 21 +++ app/src/main/res/values/attrs.xml | 10 +- app/src/main/res/xml/preferences.xml | 17 +++ 10 files changed, 210 insertions(+), 8 deletions(-) create mode 100644 app/src/main/java/com/cmwmobile/android/samples/SeekBarDialogPreference.java create mode 100644 app/src/main/kotlin/org/bspeice/minimalbible/activity/settings/MinimalBibleSettings.kt create mode 100644 app/src/main/res/layout/seekbardialogpreference_layout.xml create mode 100644 app/src/main/res/xml/preferences.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index d0ea743..8318440 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -8,13 +8,15 @@ + diff --git a/app/src/main/java/com/cmwmobile/android/samples/SeekBarDialogPreference.java b/app/src/main/java/com/cmwmobile/android/samples/SeekBarDialogPreference.java new file mode 100644 index 0000000..0aea692 --- /dev/null +++ b/app/src/main/java/com/cmwmobile/android/samples/SeekBarDialogPreference.java @@ -0,0 +1,133 @@ +/** + * Copyright CMW Mobile.com, 2010. + */ +package com.cmwmobile.android.samples; + +import android.content.Context; +import android.content.res.TypedArray; +import android.preference.DialogPreference; +import android.util.AttributeSet; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.SeekBar; +import android.widget.TextView; + +import org.bspeice.minimalbible.R; +import org.jetbrains.annotations.NotNull; + +/** + * The SeekBarDialogPreference class is a DialogPreference based and provides a + * seekbar preference. + * + * @author Casper Wakkers + */ +public class SeekBarDialogPreference extends + DialogPreference implements SeekBar.OnSeekBarChangeListener { + + // Layout widgets. + private SeekBar seekBar = null; + private TextView valueText = null; + + // Custom xml attributes. + private int maximumValue = 0; + private int minimumValue = 0; + private int stepSize = 0; + private String units = null; + + private int value = 0; + + /** + * The SeekBarDialogPreference constructor. + * + * @param context of this preference. + * @param attrs custom xml attributes. + */ + public SeekBarDialogPreference(Context context, AttributeSet attrs) { + super(context, attrs); + + TypedArray typedArray = context.obtainStyledAttributes(attrs, + R.styleable.SeekBarDialogPreference); + + maximumValue = typedArray.getInteger( + R.styleable.SeekBarDialogPreference_maximumValue, 0); + minimumValue = typedArray.getInteger( + R.styleable.SeekBarDialogPreference_minimumValue, 0); + stepSize = typedArray.getInteger( + R.styleable.SeekBarDialogPreference_stepSize, 1); + units = typedArray.getString( + R.styleable.SeekBarDialogPreference_units); + + typedArray.recycle(); + } + + /** + * {@inheritDoc} + */ + @Override + protected View onCreateDialogView() { + LayoutInflater layoutInflater = LayoutInflater.from(getContext()); + + View view = layoutInflater.inflate( + R.layout.seekbardialogpreference_layout, null); + + seekBar = (SeekBar) view.findViewById(R.id.preference_seekbar); + valueText = (TextView) view.findViewById(R.id.preference_text); + + // Get the persistent value and correct it for the minimum value. + value = getPersistedInt(minimumValue) - minimumValue; + + // You're never know... + if (value < 0) { + value = 0; + } + + seekBar.setKeyProgressIncrement(stepSize); + seekBar.setMax(maximumValue - minimumValue); + + // setProgress must come before we start listening to events, otherwise + // we may receive the initialization (i.e. 0) event destroying our value + seekBar.setProgress(value); + updateValueText(value); + + seekBar.setOnSeekBarChangeListener(this); + + return view; + } + + /** + * {@inheritDoc} + */ + @Override + public void onProgressChanged(@NotNull SeekBar seek, int newValue, + boolean fromTouch) { + // Round the value to the closest integer value. + if (stepSize >= 1) { + value = Math.round(newValue / stepSize) * stepSize; + } else { + value = newValue; + } + + updateValueText(value); + + callChangeListener(value); + } + + private void updateValueText(int value) { + valueText.setText(String.valueOf(value + minimumValue) + + (units == null ? "" : units)); + } + + @Override + public void onDialogClosed(boolean positiveResult) { + if (positiveResult && shouldPersist()) + persistInt(value + minimumValue); + } + + @Override + public void onStartTrackingTouch(SeekBar seekBar) { + } + + @Override + public void onStopTrackingTouch(SeekBar seekBar) { + } +} \ No newline at end of file diff --git a/app/src/main/java/org/bspeice/minimalbible/activity/downloader/DownloadPrefs.java b/app/src/main/java/org/bspeice/minimalbible/activity/downloader/DownloadPrefs.java index d5ba978..8570b9b 100644 --- a/app/src/main/java/org/bspeice/minimalbible/activity/downloader/DownloadPrefs.java +++ b/app/src/main/java/org/bspeice/minimalbible/activity/downloader/DownloadPrefs.java @@ -5,16 +5,19 @@ import de.devland.esperandro.annotations.SharedPreferences; /** * SharedPreferences interface to be built by Esperandro */ -@SharedPreferences(name="DownloadPrefs") +@SharedPreferences public interface DownloadPrefs { boolean hasEnabledDownload(); + void hasEnabledDownload(boolean hasEnabledDownload); boolean hasShownDownloadDialog(); + void hasShownDownloadDialog(boolean hasShownDownloadDialog); long downloadRefreshedOn(); + void downloadRefreshedOn(long downloadRefreshedOn); } diff --git a/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BibleViewer.java b/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BibleViewer.java index b5d747e..44dc3e9 100644 --- a/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BibleViewer.java +++ b/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BibleViewer.java @@ -17,6 +17,7 @@ import org.bspeice.minimalbible.R; import org.bspeice.minimalbible.activity.BaseActivity; import org.bspeice.minimalbible.activity.downloader.DownloadActivity; import org.bspeice.minimalbible.activity.navigation.NavDrawerFragment; +import org.bspeice.minimalbible.activity.settings.MinimalBibleSettings; import org.crosswire.jsword.book.Book; import javax.inject.Inject; @@ -142,8 +143,9 @@ public class BibleViewer extends BaseActivity implements // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { - return true; - } else if (id == R.id.action_downloads) { + Intent i = new Intent(this, MinimalBibleSettings.class); + startActivityForResult(i, 0); + } else if (id == R.id.action_downloads) { startActivity(new Intent(this, DownloadActivity.class)); } return super.onOptionsItemSelected(item); diff --git a/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BibleViewerPreferences.java b/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BibleViewerPreferences.java index f5fbd22..8efbb09 100644 --- a/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BibleViewerPreferences.java +++ b/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BibleViewerPreferences.java @@ -6,7 +6,7 @@ import de.devland.esperandro.annotations.SharedPreferences; /** * Created by bspeice on 7/11/14. */ -@SharedPreferences(name = "BibleViewerPreferences") +@SharedPreferences public interface BibleViewerPreferences { String defaultBookName(); diff --git a/app/src/main/kotlin/org/bspeice/minimalbible/activity/settings/MinimalBibleSettings.kt b/app/src/main/kotlin/org/bspeice/minimalbible/activity/settings/MinimalBibleSettings.kt new file mode 100644 index 0000000..cb573f0 --- /dev/null +++ b/app/src/main/kotlin/org/bspeice/minimalbible/activity/settings/MinimalBibleSettings.kt @@ -0,0 +1,16 @@ +package org.bspeice.minimalbible.activity.settings + +import android.preference.PreferenceActivity +import android.os.Bundle +import org.bspeice.minimalbible.R + +/** + * Created by bspeice on 12/1/14. + */ +class MinimalBibleSettings() : PreferenceActivity() { + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + addPreferencesFromResource(R.xml.preferences) + } +} \ No newline at end of file diff --git a/app/src/main/kotlin/org/bspeice/minimalbible/activity/viewer/BookAdapter.kt b/app/src/main/kotlin/org/bspeice/minimalbible/activity/viewer/BookAdapter.kt index 7f040ab..1ddcc40 100644 --- a/app/src/main/kotlin/org/bspeice/minimalbible/activity/viewer/BookAdapter.kt +++ b/app/src/main/kotlin/org/bspeice/minimalbible/activity/viewer/BookAdapter.kt @@ -74,8 +74,8 @@ class BookAdapter(val b: Book, val prefs: BibleViewerPreferences) val emptyView = LayoutInflater.from(parent?.getContext()) .inflate(R.layout.viewer_passage_view, parent, false) as TextView - // TODO: Prefs object for handling this? - emptyView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16f) + // TODO: Listen for changes to the text size? + emptyView.setTextSize(TypedValue.COMPLEX_UNIT_SP, prefs.baseTextSize().toFloat()) val passage = PassageView(emptyView, b) return passage diff --git a/app/src/main/res/layout/seekbardialogpreference_layout.xml b/app/src/main/res/layout/seekbardialogpreference_layout.xml new file mode 100644 index 0000000..33e172d --- /dev/null +++ b/app/src/main/res/layout/seekbardialogpreference_layout.xml @@ -0,0 +1,21 @@ + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/attrs.xml b/app/src/main/res/values/attrs.xml index e8555f4..44e32dd 100644 --- a/app/src/main/res/values/attrs.xml +++ b/app/src/main/res/values/attrs.xml @@ -15,5 +15,13 @@ - + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml new file mode 100644 index 0000000..0214586 --- /dev/null +++ b/app/src/main/res/xml/preferences.xml @@ -0,0 +1,17 @@ + + + + + + \ No newline at end of file