generated from AND-SOPT-ANDROID/and-sopt-android-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat/#8: network 상태 관리 위임하여 관리하는 기능 구현
- Loading branch information
1 parent
3160ad8
commit 9fcd32e
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
presentation/src/main/java/org/sopt/and/presentation/delegate/NetworkDelegate.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,53 @@ | ||
package org.sopt.and.presentation.delegate | ||
|
||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import org.sopt.and.domain.exception.HttpCodeError | ||
import org.sopt.and.domain.exception.NetworkError | ||
import org.sopt.and.domain.exception.UnknownError | ||
import javax.inject.Inject | ||
|
||
class NetworkDelegate @Inject constructor() { | ||
private val _networkState = MutableStateFlow<NetworkState>(NetworkState.Loading) | ||
val networkState: StateFlow<NetworkState> get() = _networkState.asStateFlow() | ||
|
||
fun handleSignUpError(exception: NetworkError) { | ||
val updatedException = when(exception){ | ||
is HttpCodeError -> { | ||
when(exception.title) { | ||
"400" -> HttpCodeError( | ||
"요청에 문제가 있습니다. ", | ||
if (exception.message == "00") "유효하지 못한 요청입니다." else "8자를 넘기는 입력이 있습니다." | ||
) | ||
"404" -> HttpCodeError( | ||
"요청에 문제가 있습니다. ", | ||
"유효하지 못한 요청입니다." | ||
) | ||
"409" -> HttpCodeError( | ||
"요청에 문제가 있습니다. ", | ||
"이미 존재하는 username입니다." | ||
) | ||
else -> UnknownError | ||
} | ||
} | ||
else -> exception | ||
} | ||
_networkState.value = NetworkState.Error(updatedException.title, updatedException.message) | ||
} | ||
|
||
|
||
fun handleNetworkSuccess(){ | ||
_networkState.value = NetworkState.Success | ||
} | ||
|
||
fun handleNetworkLoading(){ | ||
_networkState.value = NetworkState.Loading | ||
} | ||
} | ||
|
||
sealed class NetworkState{ | ||
data object Loading: NetworkState() | ||
data object Success: NetworkState() | ||
data class Error(val title: String,val msg: String): NetworkState() | ||
} |