-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0c3ebdd
commit 5c828ce
Showing
17 changed files
with
168 additions
and
19 deletions.
There are no files selected for viewing
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
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
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
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest> | ||
|
||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> | ||
</manifest> |
84 changes: 84 additions & 0 deletions
84
core/common/src/main/java/com/teamwiney/core/common/ConnectivityManagerNetworkMonitor.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,84 @@ | ||
package com.teamwiney.core.common | ||
|
||
import android.content.Context | ||
import android.net.ConnectivityManager | ||
import android.net.ConnectivityManager.NetworkCallback | ||
import android.net.Network | ||
import android.net.NetworkCapabilities | ||
import android.net.NetworkRequest | ||
import android.os.Build | ||
import android.util.Log | ||
import androidx.core.content.getSystemService | ||
import com.teamwiney.core.common.di.DispatcherModule | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.Dispatchers.IO | ||
import kotlinx.coroutines.channels.awaitClose | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.callbackFlow | ||
import kotlinx.coroutines.flow.conflate | ||
import kotlinx.coroutines.flow.flowOn | ||
import javax.inject.Inject | ||
|
||
interface NetworkMonitor { | ||
val isOnline: Flow<Boolean> | ||
} | ||
|
||
class ConnectivityManagerNetworkMonitor @Inject constructor( | ||
@ApplicationContext private val context: Context, | ||
@DispatcherModule.IoDispatcher private val ioDispatcher: CoroutineDispatcher | ||
): NetworkMonitor { | ||
override val isOnline: Flow<Boolean> = callbackFlow { | ||
val connectivityManager = context.getSystemService<ConnectivityManager>() | ||
if (connectivityManager == null) { | ||
trySend(false) | ||
close() | ||
return@callbackFlow | ||
} | ||
|
||
val callback = object : NetworkCallback() { | ||
|
||
private val networks = mutableSetOf<Network>() | ||
|
||
override fun onAvailable(network: Network) { | ||
networks += network | ||
trySend(true) | ||
|
||
Log.d("NetworkMonitor", "Network available") | ||
} | ||
|
||
override fun onLost(network: Network) { | ||
networks -= network | ||
trySend(networks.isNotEmpty()) | ||
|
||
Log.d("NetworkMonitor", "Network lost") | ||
} | ||
} | ||
|
||
val request = NetworkRequest.Builder() | ||
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) | ||
.build() | ||
connectivityManager.registerNetworkCallback(request, callback) | ||
|
||
trySend(connectivityManager.isCurrentlyConnected()) | ||
|
||
awaitClose { | ||
connectivityManager.unregisterNetworkCallback(callback) | ||
} | ||
} | ||
.flowOn(ioDispatcher) | ||
.conflate() | ||
|
||
private fun ConnectivityManager.isCurrentlyConnected(): Boolean { | ||
return try { | ||
activeNetwork | ||
?.let(::getNetworkCapabilities) | ||
?.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) | ||
?: false | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
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
2 changes: 1 addition & 1 deletion
2
...com/teamwiney/data/di/DispatcherModule.kt → ...mwiney/core/common/di/DispatcherModule.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package com.teamwiney.data.di | ||
package com.teamwiney.core.common.di | ||
|
||
import dagger.Module | ||
import dagger.Provides | ||
|
25 changes: 25 additions & 0 deletions
25
core/common/src/main/java/com/teamwiney/core/common/di/NetworkMonitorModule.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,25 @@ | ||
package com.teamwiney.core.common.di | ||
|
||
import android.content.Context | ||
import com.teamwiney.core.common.ConnectivityManagerNetworkMonitor | ||
import com.teamwiney.core.common.NetworkMonitor | ||
import dagger.Binds | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import dagger.hilt.components.SingletonComponent | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object NetworkMonitorModule { | ||
|
||
@Provides | ||
fun providesNetworkMonitor( | ||
@ApplicationContext context: Context, | ||
@DispatcherModule.IoDispatcher ioDispatcher: CoroutineDispatcher | ||
): NetworkMonitor = | ||
ConnectivityManagerNetworkMonitor(context, ioDispatcher) | ||
|
||
} |
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
2 changes: 1 addition & 1 deletion
2
data/src/main/java/com/teamwiney/data/datasource/auth/AuthDataSourceImpl.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
2 changes: 1 addition & 1 deletion
2
data/src/main/java/com/teamwiney/data/datasource/map/MapDataSourceImpl.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
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
2 changes: 1 addition & 1 deletion
2
data/src/main/java/com/teamwiney/data/datasource/wine/WineDataSourceImpl.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
2 changes: 1 addition & 1 deletion
2
data/src/main/java/com/teamwiney/data/datasource/winebadge/WineBadgeDataSourceImpl.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
2 changes: 1 addition & 1 deletion
2
data/src/main/java/com/teamwiney/data/datasource/winegrade/WineGradeDataSourceImpl.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
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
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