Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ConnectivityManager Instead Deprecated IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION) and BroadcastReceiver #2011

Merged
merged 5 commits into from
Feb 19, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add missing documentation
Signed-off-by: alperozturk <[email protected]>
alperozturk96 committed Feb 19, 2024
commit 79d417f47546acbda748309b3b571b534ec28fd4
Original file line number Diff line number Diff line change
@@ -92,6 +92,27 @@ public class NotesRepository {
private final MutableLiveData<Boolean> syncStatus = new MutableLiveData<>(false);
private final MutableLiveData<ArrayList<Throwable>> syncErrors = new MutableLiveData<>();

private final Observer<? super ConnectionLiveData.ConnectionType> syncObserver = (Observer<ConnectionLiveData.ConnectionType>) 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<? super ConnectionLiveData.ConnectionType> networkStatusObserver = (Observer<ConnectionLiveData.ConnectionType>) this::observeNetworkStatus;

/**
* @see <a href="https://stackoverflow.com/a/3104265">Do not make this a local variable.</a>
*/
@@ -144,27 +165,6 @@ 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;
}

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<? super ConnectionLiveData.ConnectionType> networkStatusObserver = (Observer<ConnectionLiveData.ConnectionType>) this::observeNetworkStatus;

private void observeNetworkStatus(ConnectionLiveData.ConnectionType connectionType) {
if (connectionType == ConnectionLiveData.ConnectionType.Lost) {
networkConnected = false;
Original file line number Diff line number Diff line change
@@ -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<ConnectionLiveData.ConnectionType>() {

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
}