-
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
Showing
5 changed files
with
141 additions
and
7 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
41 changes: 41 additions & 0 deletions
41
app/src/main/java/com/whyranoid/walkie/walkiedialog/NetworkInterceptor.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,41 @@ | ||
package com.whyranoid.walkie.walkiedialog | ||
|
||
import android.util.Log | ||
import okhttp3.Interceptor | ||
import okhttp3.Response | ||
|
||
class NetworkInterceptor( | ||
private val onRequest: () -> Unit, | ||
private val onResponse: (isSuccessFul: Boolean) -> Unit, | ||
private val excludedUrls: List<Regex>, | ||
) : Interceptor { | ||
override fun intercept(chain: Interceptor.Chain): Response { | ||
val request = chain.request() | ||
Log.d("ju0828", request.url.toString()) | ||
|
||
Log.d("ju0828", "${request.url} validate = ${excludedUrls.all { request.url.toString().contains(it).not() }}") | ||
|
||
// 요청 URL이 제외할 URL과 같지 않으면 콜백 호출 | ||
if (excludedUrls.all { request.url.toString().contains(it).not() }) { | ||
onRequest() // 요청 전 콜백 호출 | ||
Log.d("ju0828", "${request.url} request called") | ||
|
||
} | ||
|
||
return try { | ||
val response = chain.proceed(request) | ||
|
||
// 응답 후 콜백 호출 | ||
if (excludedUrls.all { request.url.toString().contains(it).not() }) { | ||
onResponse(response.isSuccessful) // 응답 후 콜백 호출 | ||
} | ||
response | ||
} catch (e: Exception) { | ||
// 예외가 발생한 경우 (타임아웃 포함) | ||
if (e is java.net.SocketTimeoutException) { | ||
onResponse(false) | ||
} | ||
throw e // 예외를 다시 던져서 호출자에게 알림 | ||
} | ||
} | ||
} |
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
33 changes: 33 additions & 0 deletions
33
presentation/src/main/java/com/whyranoid/presentation/util/ApiResponseManager.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,33 @@ | ||
package com.whyranoid.presentation.util | ||
|
||
|
||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import java.util.concurrent.atomic.AtomicInteger | ||
|
||
object ApiResponseDialog { | ||
private val loadingCount = AtomicInteger(0) | ||
|
||
private val _isLaoding = MutableStateFlow(false) | ||
val isLoading get() = _isLaoding.asStateFlow() | ||
|
||
private val _isShowError = MutableStateFlow(false) | ||
|
||
val isShowError get() = _isShowError.asStateFlow() | ||
|
||
fun startLoading() { | ||
if (loadingCount.incrementAndGet() > 0) _isLaoding.value = true | ||
} | ||
|
||
fun finishLoad(isSuccessFul: Boolean) { | ||
if (loadingCount.decrementAndGet() == 0) _isLaoding.value = false | ||
if (isSuccessFul.not()) { | ||
_isLaoding.value = false | ||
_isShowError.value = true | ||
} | ||
} | ||
|
||
fun closeErrorDialog() { | ||
_isShowError.value = false | ||
} | ||
} |