From 6884463d636b62946a38434acf985bce715b4f26 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Wed, 7 Feb 2024 10:34:03 +0100 Subject: [PATCH] Use observer, remove lifeCycleOwner Signed-off-by: alperozturk --- .../notes/persistence/NotesRepository.java | 85 +++++++++---------- 1 file changed, 41 insertions(+), 44 deletions(-) diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRepository.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRepository.java index a411a645..a57ec22a 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRepository.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRepository.java @@ -24,7 +24,6 @@ import androidx.annotation.MainThread; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.WorkerThread; -import androidx.lifecycle.LifecycleOwner; import androidx.lifecycle.LiveData; import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.Observer; @@ -84,7 +83,8 @@ public class NotesRepository { private Context context; private final NotesDatabase db; private final String defaultNonEmptyTitle; - private final LiveData connectionLiveData; + private final LiveData connectionLiveDataForSync; + private final LiveData connectionLiveDataForNetworkStatus; private boolean isSyncPossible = false; private boolean networkConnected = false; private String syncOnlyOnWifiKey; @@ -128,9 +128,10 @@ public class NotesRepository { this.apiProvider = apiProvider; this.defaultNonEmptyTitle = NoteUtil.generateNonEmptyNoteTitle("", this.context); this.syncOnlyOnWifiKey = context.getApplicationContext().getResources().getString(R.string.pref_key_wifi_only); - this.connectionLiveData = new ConnectionLiveData(context); + this.connectionLiveDataForSync = new ConnectionLiveData(context); + this.connectionLiveDataForNetworkStatus = new ConnectionLiveData(context); - connectionLiveData.observeForever(observer); + connectionLiveDataForSync.observeForever(syncObserver); final var prefs = PreferenceManager.getDefaultSharedPreferences(this.context); prefs.registerOnSharedPreferenceChangeListener(onSharedPreferenceChangeListener); @@ -139,15 +140,12 @@ public class NotesRepository { updateNetworkStatus(); } - private final Observer observer = (Observer) connectionType -> { - if (connectionType == ConnectionLiveData.ConnectionType.Lost) { - networkConnected = false; - isSyncPossible = false; - Log.d(TAG, "No network connection."); - } else { - Log.d(TAG, "Network connection established with " + connectionType.name()); - handleNetworkStatus(); - } + public void updateNetworkStatus() { + connectionLiveDataForNetworkStatus.observeForever(networkStatusObserver); + } + + private final Observer syncObserver = (Observer) connectionType -> { + observeNetworkStatus(connectionType); if (context == null || executor == null) { return; @@ -165,9 +163,38 @@ public class NotesRepository { } }; + private final Observer networkStatusObserver = (Observer) this::observeNetworkStatus; + + private void observeNetworkStatus(ConnectionLiveData.ConnectionType connectionType) { + if (connectionType == ConnectionLiveData.ConnectionType.Lost) { + networkConnected = false; + isSyncPossible = false; + Log.d(TAG, "No network connection."); + } else { + Log.d(TAG, "Network connection established with " + connectionType.name()); + handleNetworkStatus(); + } + } + + private void handleNetworkStatus() { + networkConnected = true; + isSyncPossible = !syncOnlyOnWifi; + + if (isSyncPossible) { + Log.d(TAG, "Network connection established."); + } else { + Log.d(TAG, "Network connected, but not used because only synced on wifi."); + } + } + + @Override + protected void finalize() throws Throwable { + connectionLiveDataForSync.removeObserver(syncObserver); + connectionLiveDataForNetworkStatus.removeObserver(networkStatusObserver); + super.finalize(); + } // Accounts - @AnyThread public LiveData addAccount(@NonNull String url, @NonNull String username, @NonNull String accountName, @NonNull Capabilities capabilities, @Nullable String displayName, @NonNull IResponseCallback callback) { final var account = db.getAccountDao().getAccountById(db.getAccountDao().insert(new Account(url, username, accountName, displayName, capabilities))); @@ -728,12 +755,6 @@ public class NotesRepository { return map(new SharedPreferenceIntLiveData(sp, prefKey, CategorySortingMethod.SORT_MODIFIED_DESC.getId()), CategorySortingMethod::findById); } - @Override - protected void finalize() throws Throwable { - connectionLiveData.removeObserver(observer); - super.finalize(); - } - /** * Synchronization is only possible, if there is an active network connection. *

@@ -890,30 +911,6 @@ public class NotesRepository { } } - public void updateNetworkStatus() { - connectionLiveData.observeForever(connectionType -> { - if (connectionType == ConnectionLiveData.ConnectionType.Lost) { - networkConnected = false; - isSyncPossible = false; - Log.d(TAG, "No network connection."); - } else { - Log.d(TAG, "Network connection established with " + connectionType.name()); - handleNetworkStatus(); - } - }); - } - - private void handleNetworkStatus() { - networkConnected = true; - isSyncPossible = !syncOnlyOnWifi; - - if (isSyncPossible) { - Log.d(TAG, "Network connection established."); - } else { - Log.d(TAG, "Network connected, but not used because only synced on wifi."); - } - } - @NonNull public LiveData getSyncStatus() { return distinctUntilChanged(this.syncStatus);