-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #336 from boostcampwm2023/AOS-feat/NetworkConnect
Network ์ํ ๊ด๋ฆฌํ๊ธฐ
- Loading branch information
Showing
24 changed files
with
374 additions
and
22 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
AOS/app/src/main/java/boostcamp/and07/mindsync/data/di/ConnectivityManagerModule.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,22 @@ | ||
package boostcamp.and07.mindsync.data.di | ||
|
||
import android.content.Context | ||
import android.net.ConnectivityManager | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import dagger.hilt.components.SingletonComponent | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object ConnectivityManagerModule { | ||
@Singleton | ||
@Provides | ||
fun provideConnectivityManager( | ||
@ApplicationContext context: Context, | ||
): ConnectivityManager { | ||
return context.getSystemService(ConnectivityManager::class.java) | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
AOS/app/src/main/java/boostcamp/and07/mindsync/data/network/NetworkManager.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,42 @@ | ||
package boostcamp.and07.mindsync.data.network | ||
|
||
import android.net.ConnectivityManager | ||
import android.net.Network | ||
import android.net.NetworkCapabilities | ||
import android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET | ||
import android.net.NetworkRequest | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import javax.inject.Inject | ||
|
||
class NetworkManager | ||
@Inject | ||
constructor(private val connectivityManager: ConnectivityManager) { | ||
private val _isConnected = MutableStateFlow(false) | ||
val isConnected: StateFlow<Boolean> = _isConnected | ||
|
||
private val request: NetworkRequest = | ||
NetworkRequest.Builder() | ||
.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED) | ||
.addCapability(NET_CAPABILITY_INTERNET) | ||
.build() | ||
|
||
private val networkCallback = | ||
object : ConnectivityManager.NetworkCallback() { | ||
override fun onAvailable(network: Network) { | ||
_isConnected.value = true | ||
} | ||
|
||
override fun onLost(network: Network) { | ||
_isConnected.value = false | ||
} | ||
} | ||
|
||
fun registerNetworkCallback() { | ||
connectivityManager.registerNetworkCallback(request, networkCallback) | ||
} | ||
|
||
fun unRegisterNetworkCallback() { | ||
connectivityManager.unregisterNetworkCallback(networkCallback) | ||
} | ||
} |
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
79 changes: 79 additions & 0 deletions
79
AOS/app/src/main/java/boostcamp/and07/mindsync/ui/dialog/DisConnectedNetworkDialog.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,79 @@ | ||
package boostcamp.and07.mindsync.ui.dialog | ||
|
||
import android.app.Dialog | ||
import android.content.DialogInterface | ||
import android.graphics.Color | ||
import android.graphics.drawable.ColorDrawable | ||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.view.WindowManager | ||
import androidx.fragment.app.DialogFragment | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.lifecycle.lifecycleScope | ||
import androidx.lifecycle.repeatOnLifecycle | ||
import boostcamp.and07.mindsync.R | ||
import boostcamp.and07.mindsync.databinding.DialogDisconnectNetworkBinding | ||
import dagger.hilt.android.AndroidEntryPoint | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.collectLatest | ||
import kotlinx.coroutines.launch | ||
|
||
@AndroidEntryPoint | ||
class DisConnectedNetworkDialog(private val isConnected: StateFlow<Boolean>) : | ||
DialogFragment() { | ||
private var _binding: DialogDisconnectNetworkBinding? = null | ||
private val binding get() = _binding!! | ||
|
||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { | ||
val dialog = super.onCreateDialog(savedInstanceState) | ||
dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT)) | ||
dialog.window?.attributes?.windowAnimations = R.style.AnimationDialogStyle | ||
isCancelable = false | ||
return dialog | ||
} | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, | ||
container: ViewGroup?, | ||
savedInstanceState: Bundle?, | ||
): View? { | ||
_binding = DialogDisconnectNetworkBinding.inflate(inflater, container, false) | ||
observerNetworkState() | ||
return binding.root | ||
} | ||
|
||
override fun onStart() { | ||
super.onStart() | ||
resizeDialog() | ||
} | ||
|
||
private fun observerNetworkState() { | ||
lifecycleScope.launch { | ||
repeatOnLifecycle(Lifecycle.State.STARTED) { | ||
isConnected.collectLatest { isConnected -> | ||
if (isConnected) { | ||
dismiss() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun resizeDialog() { | ||
val params: ViewGroup.LayoutParams? = dialog?.window?.attributes | ||
|
||
val displayMetrics = requireActivity().resources.displayMetrics | ||
val deviceWidth = displayMetrics.widthPixels | ||
|
||
params?.width = (deviceWidth * 0.8).toInt() | ||
params?.height = WindowManager.LayoutParams.WRAP_CONTENT | ||
dialog?.window?.attributes = params as WindowManager.LayoutParams | ||
} | ||
|
||
override fun onDismiss(dialog: DialogInterface) { | ||
_binding = null | ||
super.onDismiss(dialog) | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
AOS/app/src/main/java/boostcamp/and07/mindsync/ui/dialog/DisConntedNetworkDialog.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,68 @@ | ||
package boostcamp.and07.mindsync.ui.dialog | ||
|
||
import androidx.compose.foundation.Image | ||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.Spacer | ||
import androidx.compose.foundation.layout.height | ||
import androidx.compose.foundation.layout.width | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.LocalConfiguration | ||
import androidx.compose.ui.res.painterResource | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.compose.ui.unit.dp | ||
import androidx.compose.ui.window.Dialog | ||
import boostcamp.and07.mindsync.R | ||
import boostcamp.and07.mindsync.ui.theme.MindSyncTheme | ||
|
||
@Composable | ||
fun DisConnectedNetworkDialogScreen() { | ||
val screenWidth = LocalConfiguration.current.screenWidthDp.dp | ||
val screenHeight = LocalConfiguration.current.screenHeightDp.dp | ||
val dialogWidth = screenWidth * 0.8f | ||
|
||
Dialog(onDismissRequest = {}) { | ||
Column( | ||
modifier = | ||
Modifier | ||
.width(dialogWidth) | ||
.height(screenHeight), | ||
verticalArrangement = Arrangement.Center, | ||
) { | ||
Row( | ||
modifier = | ||
Modifier | ||
.align(Alignment.CenterHorizontally), | ||
verticalAlignment = Alignment.CenterVertically, | ||
) { | ||
Image( | ||
painter = painterResource(id = R.drawable.ic_network_off), | ||
contentDescription = null, | ||
) | ||
} | ||
Spacer(modifier = Modifier.height(30.dp)) | ||
Row( | ||
modifier = Modifier.align(Alignment.CenterHorizontally), | ||
) { | ||
Text( | ||
text = stringResource(id = R.string.disconnected_network_message), | ||
style = MaterialTheme.typography.displaySmall, | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Preview(showBackground = true) | ||
@Composable | ||
private fun LoadingDialogPreview() { | ||
MindSyncTheme { | ||
DisConnectedNetworkDialogScreen() | ||
} | ||
} |
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
Oops, something went wrong.