-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: offline login persistence (#275)
* feat: add offline login persistence * refactor: move context out of the netconn helper --------- Co-authored-by: Arthur Chalard <49678951+ArthurChalard@users.noreply.github.com>
- Loading branch information
1 parent
2bcfef4
commit 2655858
Showing
2 changed files
with
69 additions
and
3 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
...src/main/java/com/github/se/travelpouch/model/authentication/NetworkConnectivityHelper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.github.se.travelpouch.model.authentication | ||
|
||
import android.net.ConnectivityManager | ||
import android.net.Network | ||
import android.net.NetworkCapabilities | ||
import android.util.Log | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
|
||
class NetworkConnectivityHelper(private val connectivityManager: ConnectivityManager) { | ||
|
||
private val _isConnected: MutableStateFlow<Boolean?> = MutableStateFlow(null) | ||
val isConnected: StateFlow<Boolean?> = _isConnected.asStateFlow() | ||
|
||
fun registerNetworkCallback() { | ||
connectivityManager.registerDefaultNetworkCallback( | ||
object : ConnectivityManager.NetworkCallback() { | ||
override fun onAvailable(network: Network) { | ||
super.onAvailable(network) | ||
Log.d("NetworkConnectivityHelper", "Connection available") | ||
_isConnected.value = true | ||
} | ||
|
||
override fun onUnavailable() { | ||
super.onUnavailable() | ||
Log.d("NetworkConnectivityHelper", "Connection unavailable") | ||
_isConnected.value = false | ||
} | ||
}) | ||
|
||
val activeNetwork = connectivityManager.activeNetwork | ||
val initialCapabilities = connectivityManager.getNetworkCapabilities(activeNetwork) | ||
if (initialCapabilities != null) { | ||
if (initialCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) { | ||
Log.d("NetworkConnectivityHelper", "Connected to the internet") | ||
_isConnected.value = true | ||
} else { | ||
Log.d("NetworkConnectivityHelper", "Not connected to the internet") | ||
_isConnected.value = false | ||
} | ||
} else { | ||
Log.d("NetworkConnectivityHelper", "No network capabilities") | ||
_isConnected.value = false | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters