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 a57ec22a..27effc01 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 @@ -92,6 +92,27 @@ public class NotesRepository { private final MutableLiveData syncStatus = new MutableLiveData<>(false); private final MutableLiveData> syncErrors = new MutableLiveData<>(); + private final Observer syncObserver = (Observer) connectionType -> { + observeNetworkStatus(connectionType); + + 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."); + } + }); + } + }; + + private final Observer networkStatusObserver = (Observer) this::observeNetworkStatus; + /** * @see Do not make this a local variable. */ @@ -144,27 +165,6 @@ public class NotesRepository { connectionLiveDataForNetworkStatus.observeForever(networkStatusObserver); } - private final Observer syncObserver = (Observer) connectionType -> { - observeNetworkStatus(connectionType); - - 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."); - } - }); - } - }; - - private final Observer networkStatusObserver = (Observer) this::observeNetworkStatus; - private void observeNetworkStatus(ConnectionLiveData.ConnectionType connectionType) { if (connectionType == ConnectionLiveData.ConnectionType.Lost) { networkConnected = false; diff --git a/app/src/main/java/it/niedermann/owncloud/notes/shared/util/ConnectionLiveData.kt b/app/src/main/java/it/niedermann/owncloud/notes/shared/util/ConnectionLiveData.kt index 197b11d8..9a28ba80 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/shared/util/ConnectionLiveData.kt +++ b/app/src/main/java/it/niedermann/owncloud/notes/shared/util/ConnectionLiveData.kt @@ -7,12 +7,21 @@ import android.net.NetworkCapabilities import android.net.NetworkRequest import androidx.lifecycle.LiveData +/** + * LiveData subclass that provides network connection status updates. + * It observes changes in network connectivity and posts updates to its observers. + * + * @property context The application context used to access system services. + */ class ConnectionLiveData(val context: Context) : LiveData() { private val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager private val networkRequest = NetworkRequest.Builder().build() + /** + * Enum representing different types of network connections. + */ enum class ConnectionType { Lost, WiFi, Ethernet, MobileData, Other }