Skip to content

Commit

Permalink
Merge pull request orgzly#50 from Riyyi/master
Browse files Browse the repository at this point in the history
Add auto-sync on suspend
  • Loading branch information
amberin authored Nov 1, 2023
2 parents 7867e18 + 1292f81 commit 63b81eb
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,12 @@ public static boolean syncOnResume(Context context) {
context.getResources().getBoolean(R.bool.pref_default_auto_sync_on_resume));
}

public static boolean syncOnSuspend(Context context) {
return getDefaultSharedPreferences(context).getBoolean(
context.getResources().getString(R.string.pref_key_auto_sync_on_suspend),
context.getResources().getBoolean(R.bool.pref_default_auto_sync_on_suspend));
}

/*
* Notes clipboard
*/
Expand Down
10 changes: 8 additions & 2 deletions app/src/main/java/com/orgzly/android/sync/AutoSync.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ class AutoSync @Inject constructor(val context: Application, val dataRepository:
if (AppPreferences.syncOnResume(context)) {
startSync()
}

Type.APP_SUSPENDED ->
if (AppPreferences.syncOnSuspend(context)) {
startSync()
}
}
}
}
Expand All @@ -43,10 +48,11 @@ class AutoSync @Inject constructor(val context: Application, val dataRepository:
enum class Type {
NOTE_CREATED,
DATA_MODIFIED,
APP_RESUMED
APP_RESUMED,
APP_SUSPENDED,
}

companion object {
private val TAG = AutoSync::class.java.name
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,13 @@ protected void onPause() {
}
}

@Override
protected void onStop() {
super.onStop();

autoSync.trigger(AutoSync.Type.APP_SUSPENDED);
}

@Override
protected void onDestroy() {
super.onDestroy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,13 @@ protected void onResume() {
}
}

@Override
protected void onStop() {
super.onStop();

autoSync.trigger(AutoSync.Type.APP_SUSPENDED);
}

public static PendingIntent createNewNotePendingIntent(Context context, String category, SavedSearch savedSearch) {
Intent resultIntent = createNewNoteIntent(context);

Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/prefs_keys.xml
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,9 @@
<string name="pref_key_auto_sync_on_resume" translatable="false">pref_key_auto_sync_on_resume</string>
<bool name="pref_default_auto_sync_on_resume" translatable="false">false</bool>

<string name="pref_key_auto_sync_on_suspend" translatable="false">pref_key_auto_sync_on_suspend</string>
<bool name="pref_default_auto_sync_on_suspend" translatable="false">false</bool>

<string name="pref_key_auto_sync_on_repo_change" translatable="false">pref_key_auto_sync_on_repo_change</string>
<bool name="pref_default_auto_sync_on_repo_change" translatable="false">false</bool>

Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,9 @@
<string name="pref_title_on_resume_sync">App started or resumed</string>
<string name="pref_summary_on_resume_sync">Sync whenever app comes to the foreground</string>

<string name="pref_title_on_suspend_sync">App suspended</string>
<string name="pref_summary_on_suspend_sync">Sync whenever app goes to the background</string>

<string name="pref_title_repo_update_sync">Repositories modified (not implemented yet)</string>
<string name="pref_summary_repo_update_sync">Sync whenever an update in repositories is detected</string>

Expand Down
7 changes: 7 additions & 0 deletions app/src/main/res/xml/prefs_screen_auto_sync.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@
android:summary="@string/pref_summary_on_resume_sync"
android:defaultValue="@bool/pref_default_auto_sync_on_resume"/>

<SwitchPreference
android:key="@string/pref_key_auto_sync_on_suspend"
android:dependency="@string/pref_key_auto_sync"
android:title="@string/pref_title_on_suspend_sync"
android:summary="@string/pref_summary_on_suspend_sync"
android:defaultValue="@bool/pref_default_auto_sync_on_suspend"/>

<SwitchPreference
android:key="@string/pref_key_auto_sync_on_repo_change"
android:enabled="false"
Expand Down

0 comments on commit 63b81eb

Please sign in to comment.