-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[feat] 네트워크 연결 상태 확인 (로딩,에러 뷰) #103
Changes from 13 commits
713cee0
7785dd3
4ba8f39
889fe5b
7ba46e2
c72be84
5fb8dae
62d2a4a
9e6beb6
5c05e4f
cfbe79d
14e0066
d15fe91
134615d
6b09687
17ad059
d062e7b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package sopt.motivoo.data.datasource.remote.intercepter | ||
|
||
import okhttp3.Interceptor | ||
import okhttp3.Response | ||
import sopt.motivoo.data.datasource.remote.listener.NetworkErrorListener | ||
import javax.inject.Inject | ||
|
||
class ErrorInterceptor @Inject constructor( | ||
private val networkErrorListener: NetworkErrorListener, | ||
) : Interceptor { | ||
override fun intercept(chain: Interceptor.Chain): Response { | ||
val request = chain.request() | ||
var response = chain.proceed(chain.request()) | ||
|
||
when (response.code) { | ||
|
||
in SERVER_ERROR_START..SERVER_ERROR_END -> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 상수화가 필요한 상수화인지 생각해봐도 좋을 것 같아요! 물론 나쁘다는 의미는 아니지만, HTTP 코드 정도는 어느정도 상식으로 알고 계시는 분도 많다보니 오히려 어색한 느낌이 들기도 하네요 😅 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 오...그런 부분은 생각 못해봤네요! 좋은 피드백 감사합니다! |
||
networkErrorListener.onApiCallFailed() | ||
|
||
response.close() | ||
response = chain.proceed(request) | ||
} | ||
} | ||
return response | ||
} | ||
|
||
companion object { | ||
private const val SERVER_ERROR_START = 500 | ||
private const val SERVER_ERROR_END = 599 | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,11 +3,17 @@ package sopt.motivoo.data.datasource.remote.listener | |
import javax.inject.Inject | ||
|
||
class AuthTokenRefreshListenerImpl @Inject constructor() : AuthTokenRefreshListener { | ||
lateinit var onTokenRefreshFailedCallback: (() -> Unit) | ||
private var onTokenRefreshFailedCallback: (() -> Unit)? = null | ||
|
||
override fun onTokenRefreshFailed() { | ||
if (this::onTokenRefreshFailedCallback.isInitialized) { | ||
onTokenRefreshFailedCallback() | ||
} | ||
onTokenRefreshFailedCallback?.invoke() | ||
} | ||
|
||
override fun setOnTokenRefreshFailedCallback(callback: () -> Unit) { | ||
onTokenRefreshFailedCallback = callback | ||
} | ||
|
||
override fun clearOnTokenRefreshFailedCallback() { | ||
onTokenRefreshFailedCallback = null | ||
Comment on lines
+16
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😎 나이스 코드네요! |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package sopt.motivoo.data.datasource.remote.listener | ||
|
||
interface NetworkErrorListener { | ||
fun onApiCallFailed() | ||
fun setOnApiCallFailedCallback(callback: () -> Unit) | ||
fun clearOnApiCallFailedCallback() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package sopt.motivoo.data.datasource.remote.listener | ||
|
||
import javax.inject.Inject | ||
|
||
class NetworkErrorListenerImpl @Inject constructor() : NetworkErrorListener { | ||
private var onApiCallFailedCallback: (() -> Unit)? = null | ||
|
||
override fun onApiCallFailed() { | ||
onApiCallFailedCallback?.invoke() | ||
} | ||
|
||
override fun setOnApiCallFailedCallback(callback: () -> Unit) { | ||
onApiCallFailedCallback = callback | ||
} | ||
|
||
override fun clearOnApiCallFailedCallback() { | ||
onApiCallFailedCallback = null | ||
} | ||
} | ||
Comment on lines
+5
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 오 너무 좋은 설계인 것 같은데요 ?? 👍 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package sopt.motivoo.data.repository | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import sopt.motivoo.domain.repository.NetworkRepository | ||
import sopt.motivoo.util.NetworkStateLiveData | ||
import javax.inject.Inject | ||
|
||
class NetworkRepositoryImpl @Inject constructor( | ||
networkState: NetworkStateLiveData | ||
) : NetworkRepository { | ||
|
||
override val networkStateLiveData = networkState | ||
|
||
private val _isLoading = MutableLiveData<Boolean>() | ||
override val isLoading: LiveData<Boolean> = _isLoading | ||
|
||
override fun setLoading(isLoading: Boolean) { | ||
_isLoading.value = isLoading | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package sopt.motivoo.di | ||
|
||
import android.content.Context | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import dagger.hilt.components.SingletonComponent | ||
import sopt.motivoo.util.NetworkStateLiveData | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object NetworkModule { | ||
|
||
@Provides | ||
@Singleton | ||
fun provideNetworkStateLiveData(@ApplicationContext context: Context): NetworkStateLiveData { | ||
return NetworkStateLiveData(context) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package sopt.motivoo.domain.repository | ||
|
||
import androidx.lifecycle.LiveData | ||
|
||
interface NetworkRepository { | ||
|
||
val networkStateLiveData: LiveData<Boolean> | ||
val isLoading: LiveData<Boolean> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LiveData 를 사용하신 이유가 궁금합니다!! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 엇 기존에 베이스 뷰모델로 하려던게 인스턴스 문제 때문에 repository로 옴겼는데 그 과정에서 놓친 부분이네요ㅠㅠ |
||
fun setLoading(isLoading: Boolean) | ||
} |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
현재는 if문을 써도 될 것 같은데 확장성 고려하신 건가요 ??
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
넵! 에러코드가 아직 정리가 안된것 같아서 우선 500번대만 처리해 두었습니다!