Skip to content

Commit

Permalink
#91 네트워크 연결 확인 다이얼로그 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
pknujsp committed Jun 7, 2023
1 parent 9b97aee commit 17b1b81
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ dependencies {
implementation(project(":core:model"))
implementation(project(":core:database"))
implementation(project(":core:domain"))

implementation(project(":core:network"))

implementation(project(":feature:interestedmedicine"))
implementation(project(":feature:home"))
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/com/android/mediproject/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupWithNavController
import com.android.mediproject.core.common.uiutil.LayoutController
import com.android.mediproject.core.common.uiutil.SystemBarStyler
import com.android.mediproject.core.network.InternetNetworkListener
import com.android.mediproject.core.ui.WindowViewModel
import com.android.mediproject.core.ui.base.BaseActivity
import com.android.mediproject.databinding.ActivityMainBinding
Expand All @@ -32,6 +33,8 @@ class MainActivity : BaseActivity<ActivityMainBinding, MainViewModel>(ActivityMa

@Inject lateinit var systemBarStyler: SystemBarStyler

@Inject lateinit var internetNetworkListener: InternetNetworkListener

companion object {
const val VISIBLE = 0
const val INVISIBLE = 1
Expand All @@ -44,6 +47,14 @@ class MainActivity : BaseActivity<ActivityMainBinding, MainViewModel>(ActivityMa
systemBarStyler.init(this, window, this::changeFragmentContainerHeight)
systemBarStyler.setStyle(SystemBarStyler.StatusBarColor.WHITE, SystemBarStyler.NavigationBarColor.BLACK)

internetNetworkListener.activityLifeCycle = this.lifecycle
internetNetworkListener.networkStateCallback = InternetNetworkListener.NetworkStateCallback { isConnected ->
if (!isConnected) {
val modalBottomSheet = NetworkStateDialogFragment()
modalBottomSheet.show(supportFragmentManager, NetworkStateDialogFragment::class.java.name)
}
}

//SDK 31이상일 때 Splash가 소소하게 사라지는 이펙트 입니다. 추후 걸리적거리면 삭제해도 됌
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
splashScreen.setOnExitAnimationListener { splashScreenView ->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.android.mediproject

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class NetworkStateDialogFragment : BottomSheetDialogFragment() {

private var _binding: BindingView

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? = inflater.inflate(R.layout.modal_bottom_sheet_content, container, false)

}
30 changes: 30 additions & 0 deletions app/src/main/res/layout/view_network_state.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
style="@style/ModalBottomSheetDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="@color/white"
android:gravity="center"
android:orientation="vertical"
android:paddingHorizontal="24dp"
android:paddingVertical="24dp"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

<TextView
android:id="@+id/messageTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:gravity="center"
android:text="@string/noInternetConnection"
android:textSize="22sp" />

<Button
android:id="@+id/okButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="@color/main"
android:text="@string/confirm" />

</LinearLayout>
11 changes: 11 additions & 0 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<style name="ModalBottomSheet" parent="Widget.Material3.BottomSheet.Modal">
<item name="android:backgroundTint">@color/white</item>
<item name="behavior_hideable">false</item>
<item name="behavior_draggable">false</item>
</style>

<style name="ModalBottomSheetDialog" parent="ThemeOverlay.Material3.BottomSheetDialog">
<item name="bottomSheetStyle">@style/ModalBottomSheet</item>
</style>


</resources>
1 change: 1 addition & 0 deletions core/common/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,5 @@
<string name="efficacyGroupDuplicationCaution">효능군 중복</string>
<string name="divisionCaution">분할 주의</string>
<string name="additiveCaution">첨가제 주의</string>
<string name="noInternetConnection">인터넷 연결이 필요합니다.</string>
</resources>
1 change: 1 addition & 0 deletions core/network/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.android.mediproject.core.network

import android.content.Context
import android.net.ConnectivityManager
import android.net.LinkProperties
import android.net.Network
import android.net.NetworkCapabilities
import android.os.Handler
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import dagger.hilt.android.qualifiers.ApplicationContext
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class InternetNetworkListener @Inject constructor(@ApplicationContext private val context: Context) : LifecycleEventObserver {

private val connectivityManager by lazy {
context.getSystemService(ConnectivityManager::class.java)
}

private val networkCallback: ConnectivityManager.NetworkCallback = object : ConnectivityManager.NetworkCallback() {
override fun onAvailable(network: Network) {
super.onAvailable(network)
networkStateCallback?.onChangedState(true)
}

override fun onLost(network: Network) {
super.onLost(network)
networkStateCallback?.onChangedState(false)
}

override fun onCapabilitiesChanged(network: Network, networkCapabilities: NetworkCapabilities) {
super.onCapabilitiesChanged(network, networkCapabilities)
}

override fun onLinkPropertiesChanged(network: Network, linkProperties: LinkProperties) {
super.onLinkPropertiesChanged(network, linkProperties)
}
}

fun interface NetworkStateCallback {
fun onChangedState(isConnected: Boolean)
}

var networkStateCallback: NetworkStateCallback? = null
set(value) {
field = value
connectivityManager.registerDefaultNetworkCallback(networkCallback,
Handler.createAsync(context.mainLooper))
}

var activityLifeCycle: Lifecycle? = null
set(value) {
field = value
field?.addObserver(this)
}

override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
when (event) {
Lifecycle.Event.ON_START -> {
connectivityManager.unregisterNetworkCallback(networkCallback)
}

Lifecycle.Event.ON_PAUSE -> {
networkStateCallback?.onChangedState(false)
}

else -> {}
}
}
}
19 changes: 8 additions & 11 deletions feature/search/src/main/res/layout/fragment_search_medicines.xml
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
xmlns:app="http://schemas.android.com/apk/res-auto">

<data>

<variable
name="viewModel"
type="com.android.mediproject.feature.search.SearchMedicinesViewModel"
/>
type="com.android.mediproject.feature.search.SearchMedicinesViewModel" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
android:background="@color/white">

<com.android.mediproject.core.ui.base.view.Bar
android:id="@+id/searchBar"
Expand All @@ -23,8 +21,7 @@
app:layout_constraintTop_toTopOf="parent"
app:setTheme="white"
app:showBackButton="true"
app:title="@string/medicineSearch"
/>
app:title="@string/medicineSearch" />

<com.android.mediproject.core.ui.base.view.MediSearchbar
android:id="@+id/searchView"
Expand All @@ -42,20 +39,20 @@
app:layout_constraintTop_toBottomOf="@id/searchBar"
app:layout_goneMarginTop="30dp"
app:search_hint="@string/search_hint"
app:search_icon="@drawable/baseline_search_24"
/>
app:search_icon="@drawable/baseline_search_24" />

<androidx.fragment.app.FragmentContainerView
android:id="@+id/contentsFragmentContainerView"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="16dp"
android:background="@color/white"
android:paddingHorizontal="16dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/searchView"
app:navGraph="@navigation/search_medicines_nav"
/>
app:navGraph="@navigation/search_medicines_nav" />

</androidx.constraintlayout.widget.ConstraintLayout>

Expand Down

0 comments on commit 17b1b81

Please sign in to comment.