Skip to content

Commit

Permalink
Support for realtime divergence update in MainActivity
Browse files Browse the repository at this point in the history
  • Loading branch information
retanar committed Jan 20, 2021
1 parent 5e1f4b2 commit fe488aa
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ android {
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "0.4.0 alpha 2"
versionName "0.4.0 alpha 3"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
8 changes: 7 additions & 1 deletion app/src/main/java/com/vlprojects/divergence/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class MainActivity : AppCompatActivity() {

setDivergenceText()
binding.changeDivergenceButton.setOnClickListener { changeDivergence(prefs) }
prefs.registerOnSharedPreferenceChangeListener(onPreferenceChangeListener)
}

private fun setDivergenceText() {
Expand All @@ -47,7 +48,6 @@ class MainActivity : AppCompatActivity() {
updateWidget()

Toast.makeText(this, "Updated!", Toast.LENGTH_SHORT).show()
recreate()
}

private fun updateWidget() {
Expand All @@ -62,4 +62,10 @@ class MainActivity : AppCompatActivity() {
val pendingIntent = PendingIntent.getBroadcast(this, 0, intentUpdate, PendingIntent.FLAG_UPDATE_CURRENT)
pendingIntent.send()
}

// Using field so it won't be garbage collected
private val onPreferenceChangeListener = SharedPreferences.OnSharedPreferenceChangeListener { _, tag ->
if (tag == SHARED_CURRENT_DIVERGENCE)
setDivergenceText()
}
}
10 changes: 9 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/textView"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textSize="20sp"
android:layout_marginBottom="6dp"/>
<TextView
android:id="@+id/textView3"
android:layout_height="wrap_content"
android:text="@string/resize_widget_guide"
android:layout_width="match_parent"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textSize="20sp"/>
<View
android:id="@+id/divider"
Expand Down Expand Up @@ -43,7 +51,7 @@
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_marginBottom="12dp">
android:layout_height="match_parent" android:layout_marginBottom="12dp" android:visibility="gone">
<TextView
android:text="@string/next_divergence"
android:layout_width="wrap_content"
Expand Down
10 changes: 5 additions & 5 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<resources>
<string name="app_name">Divergence meter</string>
<string name="default_divergence">1.048596</string>
<string name="current_divergence">Current divergence</string>
<string name="current_divergence">Current divergence:</string>
<string name="next_divergence">Next divergence:</string>
<string name="hint_enter_new_divergence_here">Enter new divergence here</string>
<string name="change">Change</string>
<string name="add_widget_guide">To add the widget:
\n- On a Home screen, hold an empty space.
\n- Tap Widgets. Then find a Divergence widget.
\n- Hold and move the widget wherever you want.
\nTo resize the widget, hold it and release.
\n- On a Home screen, hold an empty space. Tap Widgets section.
\n- Find a Divergence widget. Hold it and move wherever you want.
</string>
<string name="resize_widget_guide">To resize the widget, hold it and release.
You\'ll see the dots around it that you can move.
</string>
</resources>

0 comments on commit fe488aa

Please sign in to comment.