Skip to content

Commit

Permalink
Merge pull request #102 from boostcampwm-2022/96-fix-nav-graph-and-vi…
Browse files Browse the repository at this point in the history
…ewmodel

Navigation Component 버그 해결하기
  • Loading branch information
DoTheBestMayB authored Dec 14, 2022
2 parents 0dd73ce + bc5d96f commit 6ad014b
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 91 deletions.
4 changes: 4 additions & 0 deletions data/src/main/java/com/stop/data/di/NetworkModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import okhttp3.Response
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import java.util.concurrent.TimeUnit
import javax.inject.Named
import javax.inject.Singleton

Expand All @@ -40,6 +41,9 @@ internal object NetworkModule {
loggingInterceptor: HttpLoggingInterceptor,
): OkHttpClient {
return OkHttpClient.Builder()
.connectTimeout(5, TimeUnit.SECONDS)
.writeTimeout(5, TimeUnit.SECONDS)
.readTimeout(5, TimeUnit.SECONDS)
.addInterceptor(loggingInterceptor)
.addInterceptor(customInterceptor)
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ internal class GetLastTransportTimeUseCaseImpl @Inject constructor(
lastTime += TIME_CORRECTION_VALUE
}

val lastTimeString = lastTime.toString().chunked(2).joinToString(":")
val lastTimeString = lastTime.toString().padStart(6, '0').chunked(2).joinToString(":")

return TransportLastTime(
transportMoveType = TransportMoveType.BUS,
Expand Down Expand Up @@ -521,7 +521,7 @@ internal class GetLastTransportTimeUseCaseImpl @Inject constructor(
lastTime += TIME_CORRECTION_VALUE
}

val lastTimeString = lastTime.toString().chunked(2).joinToString(":")
val lastTimeString = lastTime.toString().padStart(6, '0').chunked(2).joinToString(":")

return TransportLastTime(
transportMoveType = TransportMoveType.BUS,
Expand Down
3 changes: 3 additions & 0 deletions presentation/src/main/java/com/stop/model/ErrorType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ enum class ErrorType(val stringResourcesId: Int) {
AVAILABLE_BUS_NO_EXIST_YET(R.string.available_bus_no_exist_yet),
BUS_DISAPPEAR_SUDDENLY(R.string.bus_disappear_suddenly),
MISSION_SOMETHING_WRONG(R.string.mission_something_wrong),
SOCKET_TIMEOUT_EXCEPTION(R.string.socket_timeout_exception_please_retry),
UNKNOWN_EXCEPTION(R.string.unknown_exception_occur),
UNKNOWN_HOST_EXCEPTION(R.string.unknown_host_exception_occur),
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ class AlarmSettingFragment : Fragment() {
//alarmSettingViewModel.makeAlarmWorker(transportLastTime.timeToBoard)

val navController = findNavController()
navController.setGraph(R.navigation.nav_graph)
navController.popBackStack(R.id.action_global_mapFragment, false)
navController.popBackStack(R.id.mapFragment, false)
requireActivity().viewModelStore.clear()
}

Expand Down
20 changes: 5 additions & 15 deletions presentation/src/main/java/com/stop/ui/map/MapFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
Expand All @@ -22,7 +23,6 @@ import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.skt.tmap.TMapPoint
import com.stop.AlarmActivity
import com.stop.R
import com.stop.RouteNavGraphDirections
import com.stop.alarm.SoundService
import com.stop.databinding.FragmentMapBinding
import com.stop.model.AlarmStatus
Expand Down Expand Up @@ -141,24 +141,14 @@ class MapFragment : Fragment(), MapHandler {

binding.homePanel.viewPanelStart.setOnClickListener {
placeSearchViewModel.setPanelVisibility(View.INVISIBLE)
findNavController().apply {
setGraph(R.navigation.route_nav_graph)
navigate(
RouteNavGraphDirections.actionGlobalRouteFragment()
.setStart(placeSearchViewModel.panelInfo)
)
}
val bundle = bundleOf("start" to placeSearchViewModel.panelInfo)
findNavController().navigate(R.id.action_mapFragment_to_route_nav_graph, bundle)
}

binding.homePanel.viewPanelEnd.setOnClickListener {
placeSearchViewModel.setPanelVisibility(View.INVISIBLE)
findNavController().apply {
setGraph(R.navigation.route_nav_graph)
navigate(
RouteNavGraphDirections.actionGlobalRouteFragment()
.setEnd(placeSearchViewModel.panelInfo)
)
}
val bundle = bundleOf("end" to placeSearchViewModel.panelInfo)
findNavController().navigate(R.id.action_mapFragment_to_route_nav_graph, bundle)
}
}

Expand Down
12 changes: 5 additions & 7 deletions presentation/src/main/java/com/stop/ui/route/RouteFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ class RouteFragment : Fragment() {

backPressedCallback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
val navController = findNavController()
navController.setGraph(R.navigation.nav_graph)
navController.popBackStack(R.id.action_global_mapFragment, false)
findNavController().popBackStack(R.id.mapFragment, false)
}
}
requireActivity().onBackPressedDispatcher.addCallback(this, backPressedCallback)
Expand Down Expand Up @@ -163,7 +161,7 @@ class RouteFragment : Fragment() {
requireArguments().clear()

if (args?.start != null || args?.end != null) {
routeViewModel.getRoute()
routeViewModel.patchRoute()
}
}

Expand All @@ -176,9 +174,9 @@ class RouteFragment : Fragment() {

val dialogView = layoutInflater.inflate(R.layout.dialog_progress, null)
alertDialog = AlertDialog.Builder(requireContext())
.setView(dialogView)
.setCancelable(false)
.create()
.setView(dialogView)
.setCancelable(false)
.create()
alertDialog.window?.setBackgroundDrawableResource(R.color.transparent)
routeViewModel.alertDialog = alertDialog
}
Expand Down
34 changes: 24 additions & 10 deletions presentation/src/main/java/com/stop/ui/route/RouteViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ import com.stop.model.ErrorType
import com.stop.model.Event
import com.stop.model.route.Place
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.net.SocketTimeoutException
import java.net.UnknownHostException
import javax.inject.Inject

@HiltViewModel
Expand Down Expand Up @@ -50,7 +54,17 @@ class RouteViewModel @Inject constructor(
val isLoading: LiveData<Event<Boolean>>
get() = _isLoading

fun getRoute(isShowError: Boolean = true) {
private val coroutineExceptionHandler = CoroutineExceptionHandler { _, throwable ->
val errorMessage = when (throwable) {
is SocketTimeoutException -> Event(ErrorType.SOCKET_TIMEOUT_EXCEPTION)
is UnknownHostException -> Event(ErrorType.UNKNOWN_HOST_EXCEPTION)
else -> Event(ErrorType.UNKNOWN_EXCEPTION)
}
_errorMessage.postValue(errorMessage)
_isLoading.postValue(Event(false))
}

fun patchRoute(isShowError: Boolean = true) {
val originValue = _origin.value ?: let {
if (!isShowError) {
return
Expand All @@ -75,30 +89,30 @@ class RouteViewModel @Inject constructor(
endY = destinationValue.coordinate.latitude,
)

viewModelScope.launch {
viewModelScope.launch(Dispatchers.Default + coroutineExceptionHandler) {
val itineraries = getRouteUseCase(routeRequest)
if (itineraries.isEmpty()) {
_errorMessage.value = Event(ErrorType.NO_ROUTE_RESULT)
_routeResponse.value = listOf()
_isLoading.value = Event(false)
_errorMessage.postValue(Event(ErrorType.NO_ROUTE_RESULT))
_routeResponse.postValue(listOf())
_isLoading.postValue(Event(false))
return@launch
}
this@RouteViewModel._routeResponse.value = itineraries
_isLoading.value = Event(false)
this@RouteViewModel._routeResponse.postValue(itineraries)
_isLoading.postValue(Event(false))
}
}

fun changeOriginAndDestination() {
_origin.value = _destination.value.also {
_destination.value = _origin.value
}
getRoute(false)
patchRoute(false)
}

fun calculateLastTransportTime(itinerary: Itinerary) {
checkClickedItinerary(itinerary)
viewModelScope.launch {
this@RouteViewModel._lastTimeResponse.value = Event(getLastTransportTimeUseCase(itinerary) )
viewModelScope.launch(Dispatchers.Default + coroutineExceptionHandler) {
this@RouteViewModel._lastTimeResponse.postValue(Event(getLastTransportTimeUseCase(itinerary)))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.GravityCompat
import androidx.navigation.findNavController
import androidx.navigation.fragment.findNavController
import com.stop.R
import androidx.navigation.navGraphViewModels
Expand Down Expand Up @@ -76,8 +75,7 @@ class RouteDetailFragment : Fragment(), RouteDetailHandler {

binding.imageViewClose.setOnClickListener {
findNavController().apply {
setGraph(R.navigation.nav_graph)
popBackStack(R.id.action_global_mapFragment, false)
popBackStack(R.id.mapFragment, false)
requireActivity().viewModelStore.clear()
}
}
Expand Down
62 changes: 57 additions & 5 deletions presentation/src/main/res/navigation/nav_graph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,26 @@
android:id="@+id/nav_graph"
app:startDestination="@id/mapFragment">

<include app:graph="@navigation/route_nav_graph" />

<fragment
android:id="@+id/mapFragment"
android:name="com.stop.ui.map.MapFragment"
android:label="MapFragment"
tools:layout="@layout/fragment_map">

<action
android:id="@+id/action_mapFragment_to_route_nav_graph"
app:destination="@id/route_nav_graph" />
<argument
android:name="start"
android:defaultValue="@null"
app:argType="com.stop.model.route.Place"
app:nullable="true" />
<argument
android:name="end"
android:defaultValue="@null"
app:argType="com.stop.model.route.Place"
app:nullable="true" />

<action
android:id="@+id/action_mapFragment_to_placeSearchFragment"
app:destination="@id/placeSearchFragment" />
Expand All @@ -35,9 +47,49 @@
app:destination="@id/mapFragment" />
</fragment>

<action
android:id="@+id/action_global_mapFragment"
app:destination="@id/mapFragment" />
<navigation
android:id="@+id/route_nav_graph"
app:startDestination="@id/routeFragment">

<fragment
android:id="@+id/routeFragment"
android:name="com.stop.ui.route.RouteFragment"
android:label="RouteFragment"
tools:layout="@layout/fragment_route">
<action
android:id="@+id/action_routeFragment_to_routeDetailFragment"
app:destination="@id/routeDetailFragment" />
<argument
android:name="start"
android:defaultValue="@null"
app:argType="com.stop.model.route.Place"
app:nullable="true" />
<argument
android:name="end"
android:defaultValue="@null"
app:argType="com.stop.model.route.Place"
app:nullable="true" />
</fragment>

<fragment
android:id="@+id/routeDetailFragment"
android:name="com.stop.ui.routedetail.RouteDetailFragment"
android:label="RouteDetailFragment"
tools:layout="@layout/fragment_route_detail">
<action
android:id="@+id/action_routeDetailFragment_to_alarmSetting"
app:destination="@id/alarmSetting" />
</fragment>

<fragment
android:id="@+id/alarmSetting"
android:name="com.stop.ui.alarmsetting.AlarmSettingFragment"
android:label="fragment_alarm_setting"
tools:layout="@layout/fragment_alarm_setting" />
<action
android:id="@+id/action_global_routeFragment"
app:destination="@id/routeFragment" />
</navigation>

<action
android:id="@+id/action_global_placeSearchFragment"
Expand Down
47 changes: 0 additions & 47 deletions presentation/src/main/res/navigation/route_nav_graph.xml

This file was deleted.

3 changes: 3 additions & 0 deletions presentation/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@
<string name="end_position_text">도착지 : %1$s</string>
<string name="last_time_text">막차시간 : %1$s</string>
<string name="walk_time_text">%1$d분</string>
<string name="socket_timeout_exception_please_retry">API 서버가 응답하지 않습니다. 다시 시도해주세요.</string>
<string name="unknown_exception_occur">알 수 없는 에러가 발생했습니다.</string>
<string name="unknown_host_exception_occur">인터넷이 연결되어 있지 않거나, 페쇄망 호스트에 연결을 시도했습니다.</string>
<string name="alarm_last_notification_text">알람이 %1$s의 %2$s분 전에 울릴 예정입니다.</string>
<string name="last_time_not_found">알람을 설정할 수 없습니다</string>
<string name="minute_before">분 전</string>
Expand Down

0 comments on commit 6ad014b

Please sign in to comment.