Skip to content

Commit

Permalink
Use observer, remove lifeCycleOwner
Browse files Browse the repository at this point in the history
Signed-off-by: alperozturk <alper_ozturk@proton.me>
  • Loading branch information
alperozturk96 committed Feb 7, 2024
1 parent 8b4c34d commit f8070a7
Showing 1 changed file with 41 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
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;
Expand Down Expand Up @@ -84,7 +83,8 @@ public class NotesRepository {
private Context context;
private final NotesDatabase db;
private final String defaultNonEmptyTitle;
private final LiveData<ConnectionLiveData.ConnectionType> connectionLiveData;
private final LiveData<ConnectionLiveData.ConnectionType> connectionLiveDataForSync;
private final LiveData<ConnectionLiveData.ConnectionType> connectionLiveDataForNetworkStatus;
private boolean isSyncPossible = false;
private boolean networkConnected = false;
private String syncOnlyOnWifiKey;
Expand Down Expand Up @@ -128,9 +128,10 @@ private NotesRepository(@NonNull final Context context, @NonNull final NotesData
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);
Expand All @@ -139,15 +140,12 @@ private NotesRepository(@NonNull final Context context, @NonNull final NotesData
updateNetworkStatus();
}

private final Observer<? super ConnectionLiveData.ConnectionType> observer = (Observer<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();
}
public void updateNetworkStatus() {
connectionLiveDataForNetworkStatus.observeForever(networkStatusObserver);
}

private final Observer<? super ConnectionLiveData.ConnectionType> syncObserver = (Observer<ConnectionLiveData.ConnectionType>) connectionType -> {
observeNetworkStatus(connectionType);

if (context == null || executor == null) {
return;
Expand All @@ -165,9 +163,38 @@ private NotesRepository(@NonNull final Context context, @NonNull final NotesData
}
};

private final Observer<? super ConnectionLiveData.ConnectionType> networkStatusObserver = (Observer<ConnectionLiveData.ConnectionType>) this::observeNetworkStatus;

// Accounts
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<ImportStatus> addAccount(@NonNull String url, @NonNull String username, @NonNull String accountName, @NonNull Capabilities capabilities, @Nullable String displayName, @NonNull IResponseCallback<Account> callback) {
final var account = db.getAccountDao().getAccountById(db.getAccountDao().insert(new Account(url, username, accountName, displayName, capabilities)));
Expand Down Expand Up @@ -728,12 +755,6 @@ public LiveData<CategorySortingMethod> getCategoryOrder(@NonNull NavigationCateg
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.
* <p>
Expand Down Expand Up @@ -890,30 +911,6 @@ void onPostExecute(SyncResultStatus status) {
}
}

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<Boolean> getSyncStatus() {
return distinctUntilChanged(this.syncStatus);
Expand Down

0 comments on commit f8070a7

Please sign in to comment.