mirror of
https://github.com/nextcloud/notes-android.git
synced 2024-11-21 20:35:58 +03:00
Use observer, remove lifeCycleOwner
Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
parent
0956ba300a
commit
36a01320ae
2 changed files with 31 additions and 44 deletions
|
@ -27,6 +27,7 @@ import androidx.annotation.WorkerThread;
|
|||
import androidx.lifecycle.LifecycleOwner;
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
import androidx.lifecycle.Observer;
|
||||
import androidx.preference.PreferenceManager;
|
||||
import com.nextcloud.android.sso.AccountImporter;
|
||||
import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException;
|
||||
|
@ -52,7 +53,6 @@ import it.niedermann.owncloud.notes.persistence.entity.CategoryWithNotesCount;
|
|||
import it.niedermann.owncloud.notes.persistence.entity.Note;
|
||||
import it.niedermann.owncloud.notes.persistence.entity.NotesListWidgetData;
|
||||
import it.niedermann.owncloud.notes.persistence.entity.SingleNoteWidgetData;
|
||||
import it.niedermann.owncloud.notes.shared.extensions.ContextExtensionsKt;
|
||||
import it.niedermann.owncloud.notes.shared.model.ApiVersion;
|
||||
import it.niedermann.owncloud.notes.shared.model.Capabilities;
|
||||
import it.niedermann.owncloud.notes.shared.model.CategorySortingMethod;
|
||||
|
@ -78,13 +78,12 @@ public class NotesRepository {
|
|||
private static NotesRepository instance;
|
||||
|
||||
private final ApiProvider apiProvider;
|
||||
private final ExecutorService executor;
|
||||
private ExecutorService executor;
|
||||
private final ExecutorService syncExecutor;
|
||||
private final ExecutorService importExecutor;
|
||||
private final Context context;
|
||||
private Context context;
|
||||
private final NotesDatabase db;
|
||||
private final String defaultNonEmptyTitle;
|
||||
|
||||
private final LiveData<ConnectionLiveData.ConnectionType> connectionLiveData;
|
||||
private boolean isSyncPossible = false;
|
||||
private boolean networkConnected = false;
|
||||
|
@ -131,27 +130,7 @@ public class NotesRepository {
|
|||
this.syncOnlyOnWifiKey = context.getApplicationContext().getResources().getString(R.string.pref_key_wifi_only);
|
||||
this.connectionLiveData = new ConnectionLiveData(context);
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
if (isSyncPossible() && SSOUtil.isConfigured(context)) {
|
||||
executor.submit(() -> {
|
||||
try {
|
||||
scheduleSync(getAccountByName(SingleAccountHelper.getCurrentSingleSignOnAccount(context).name), false);
|
||||
} catch (NextcloudFilesAppAccountNotFoundException |
|
||||
NoCurrentAccountSelectedException e) {
|
||||
Log.v(TAG, "Can not select current SingleSignOn account after network changed, do not sync.");
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
connectionLiveData.observeForever(observer);
|
||||
|
||||
final var prefs = PreferenceManager.getDefaultSharedPreferences(this.context);
|
||||
prefs.registerOnSharedPreferenceChangeListener(onSharedPreferenceChangeListener);
|
||||
|
@ -160,6 +139,32 @@ public class NotesRepository {
|
|||
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();
|
||||
}
|
||||
|
||||
if (context == null || executor == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (isSyncPossible() && SSOUtil.isConfigured(context)) {
|
||||
executor.submit(() -> {
|
||||
try {
|
||||
scheduleSync(getAccountByName(SingleAccountHelper.getCurrentSingleSignOnAccount(context).name), false);
|
||||
} catch (NextcloudFilesAppAccountNotFoundException |
|
||||
NoCurrentAccountSelectedException e) {
|
||||
Log.v(TAG, "Can not select current SingleSignOn account after network changed, do not sync.");
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// Accounts
|
||||
|
||||
|
@ -725,12 +730,7 @@ public class NotesRepository {
|
|||
|
||||
@Override
|
||||
protected void finalize() throws Throwable {
|
||||
LifecycleOwner lifecycleOwner = ContextExtensionsKt.lifecycleOwner(context);
|
||||
if (lifecycleOwner != null) {
|
||||
Log.d(TAG, "ConnectionLiveData Observer Removed");
|
||||
connectionLiveData.removeObservers(lifecycleOwner);
|
||||
}
|
||||
|
||||
connectionLiveData.removeObserver(observer);
|
||||
super.finalize();
|
||||
}
|
||||
|
||||
|
|
|
@ -1,13 +0,0 @@
|
|||
package it.niedermann.owncloud.notes.shared.extensions
|
||||
|
||||
import android.content.Context
|
||||
import android.content.ContextWrapper
|
||||
import androidx.lifecycle.LifecycleOwner
|
||||
|
||||
fun Context.lifecycleOwner(): LifecycleOwner? {
|
||||
var curContext: Context? = this
|
||||
while (curContext != null && curContext !is LifecycleOwner) {
|
||||
curContext = (curContext as? ContextWrapper)?.baseContext
|
||||
}
|
||||
return curContext as? LifecycleOwner
|
||||
}
|
Loading…
Reference in a new issue