-
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.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
11 changed files
with
262 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
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,7 @@ | ||
package com.owori.android.core | ||
|
||
|
||
object AppConfig { | ||
const val TAG_DEBUG = "TAG_DEBUG" | ||
|
||
} |
36 changes: 33 additions & 3 deletions
36
app/src/main/java/com/owori/android/core/OworiApplication.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 |
---|---|---|
@@ -1,14 +1,44 @@ | ||
package com.owori.android.core | ||
|
||
import android.annotation.SuppressLint | ||
import android.app.Application | ||
import com.kakao.sdk.common.KakaoSdk | ||
import android.content.Context | ||
import androidx.annotation.StringRes | ||
import androidx.lifecycle.DefaultLifecycleObserver | ||
import androidx.lifecycle.LifecycleOwner | ||
import com.owori.android.R | ||
import com.owori.android.module.NetworkConnectionChecker | ||
import com.kakao.sdk.common.KakaoSdk | ||
import dagger.hilt.android.HiltAndroidApp | ||
|
||
@HiltAndroidApp | ||
class OworiApplication: Application() { | ||
class OworiApplication : Application(), DefaultLifecycleObserver { | ||
override fun onCreate() { | ||
super.onCreate() | ||
super<Application>.onCreate() | ||
context = applicationContext | ||
networkConnectionChecker = NetworkConnectionChecker(context) | ||
KakaoSdk.init(this, getString(R.string.kakao_login_key)) | ||
} | ||
|
||
override fun onStop(owner: LifecycleOwner) { | ||
networkConnectionChecker.unregister() | ||
super.onStop(owner) | ||
} | ||
|
||
override fun onStart(owner: LifecycleOwner) { | ||
networkConnectionChecker.register() | ||
super.onStart(owner) | ||
} | ||
|
||
companion object { | ||
@SuppressLint("StaticFieldLeak") | ||
private lateinit var context: Context | ||
|
||
fun getString(@StringRes stringResId: Int): String { | ||
return context.getString(stringResId) | ||
} | ||
|
||
private lateinit var networkConnectionChecker: NetworkConnectionChecker | ||
fun isOnline() = networkConnectionChecker.isOnline() | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
app/src/main/java/com/owori/android/core/di/NetworkModule.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,56 @@ | ||
package com.owori.android.core.di | ||
|
||
import com.owori.android.R | ||
import com.owori.android.core.OworiApplication | ||
import com.owori.android.data.api.auth.AuthApi | ||
import com.owori.android.module.HttpRequestInterceptor | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import okhttp3.OkHttpClient | ||
import retrofit2.Retrofit | ||
import retrofit2.converter.gson.GsonConverterFactory | ||
import retrofit2.converter.scalars.ScalarsConverterFactory | ||
import javax.inject.Singleton | ||
|
||
/* | ||
* Created by JJJoonngg | ||
*/ | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
object NetworkModule { | ||
const val NETWORK_EXCEPTION_OFFLINE_CASE = "network status is offline" | ||
const val NETWORK_EXCEPTION_BODY_IS_NULL = "result body is null" | ||
|
||
@Provides | ||
@Singleton | ||
fun provideOKHttpClient(): OkHttpClient { | ||
return OkHttpClient.Builder() | ||
.addInterceptor(HttpRequestInterceptor()) | ||
.retryOnConnectionFailure(false) | ||
.build() | ||
} | ||
|
||
@Provides | ||
@Singleton | ||
fun provideRetrofit(okHttpClient: OkHttpClient): Retrofit { | ||
return Retrofit.Builder() | ||
.client(okHttpClient) | ||
.baseUrl(OworiApplication.getString(R.string.base_url)) | ||
.addConverterFactory(ScalarsConverterFactory.create()) | ||
.addConverterFactory(GsonConverterFactory.create()) | ||
.build() | ||
} | ||
|
||
@Provides | ||
@Singleton | ||
fun provideAuthApi(retrofit: Retrofit): AuthApi { | ||
return retrofit.buildService() | ||
} | ||
|
||
private inline fun <reified T> Retrofit.buildService(): T { | ||
return this.create(T::class.java) | ||
} | ||
} |
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,9 @@ | ||
package com.owori.android.data.api.auth | ||
|
||
/* | ||
* Created by JJJoonngg | ||
*/ | ||
|
||
interface AuthApi { | ||
|
||
} |
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.owori.android.module | ||
|
||
/* | ||
* Created by JJJoonngg | ||
*/ | ||
|
||
sealed class DataResult<out T> { | ||
data class Success<T>(val data: T) : DataResult<T>() | ||
data class Fail(val statusCode: Int, val message: String) : DataResult<Nothing>() | ||
data class Error(val exception: Exception) : DataResult<Nothing>() | ||
} | ||
|
||
inline fun <T> DataResult<T>.onSuccess(action: (T) -> Unit): DataResult<T> { | ||
if (this is DataResult.Success) { | ||
action(data) | ||
} | ||
return this | ||
} | ||
|
||
inline fun <T> DataResult<T>.onFail(resultCode: (Int) -> Unit): DataResult<T> { | ||
if (this is DataResult.Fail) { | ||
resultCode(this.statusCode) | ||
} | ||
return this | ||
} | ||
|
||
inline fun <T> DataResult<T>.onError(action: (Exception) -> Unit): DataResult<T> { | ||
if (this is DataResult.Fail) { | ||
action(IllegalArgumentException("code : ${this.statusCode}, message : ${this.message}")) | ||
} else if (this is DataResult.Error) { | ||
action(this.exception) | ||
} | ||
return this | ||
} | ||
|
||
inline fun <T> DataResult<T>.onException(action: (Exception) -> Unit): DataResult<T> { | ||
if (this is DataResult.Error) { | ||
action(this.exception) | ||
} | ||
return this | ||
} |
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.owori.android.module | ||
|
||
import com.owori.android.core.OworiApplication | ||
import com.owori.android.core.di.NetworkModule | ||
import retrofit2.Response | ||
|
||
/* | ||
* Created by JJJoonngg | ||
*/ | ||
|
||
suspend fun <T : Any, R : Any> handleApi( | ||
execute: suspend () -> Response<T>, | ||
mapper: (T) -> R | ||
): DataResult<R> { | ||
if (OworiApplication.isOnline().not()) { | ||
return DataResult.Error(Exception(NetworkModule.NETWORK_EXCEPTION_OFFLINE_CASE)) | ||
} | ||
|
||
return try { | ||
val response = execute() | ||
val body = response.body() | ||
if (response.isSuccessful) { | ||
body?.let { | ||
DataResult.Success(mapper(it)) | ||
} ?: run { | ||
throw NullPointerException(NetworkModule.NETWORK_EXCEPTION_BODY_IS_NULL) | ||
} | ||
} else { | ||
getFailDataResult(body, response) | ||
} | ||
} catch (e: Exception) { | ||
DataResult.Error(e) | ||
} | ||
} | ||
|
||
|
||
private fun <T : Any> getFailDataResult(body: T?, response: Response<T>) = body?.let { | ||
DataResult.Fail(statusCode = response.code(), message = it.toString()) | ||
} ?: run { | ||
DataResult.Fail(statusCode = response.code(), message = response.message()) | ||
} |
21 changes: 21 additions & 0 deletions
21
app/src/main/java/com/owori/android/module/HttpRequestInterceptor.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,21 @@ | ||
package com.owori.android.module | ||
|
||
import android.util.Log | ||
import com.owori.android.core.AppConfig | ||
import okhttp3.Interceptor | ||
import okhttp3.Response | ||
|
||
|
||
class HttpRequestInterceptor : Interceptor { | ||
override fun intercept(chain: Interceptor.Chain): Response { | ||
try { | ||
val originRequest = chain.request() | ||
Log.d(AppConfig.TAG_DEBUG, "HttpRequestInterceptor: ${originRequest.url}") | ||
|
||
return chain.proceed(originRequest) | ||
} catch (e: Exception) { | ||
Log.d(AppConfig.TAG_DEBUG, "HttpRequestInterceptor error: ${e.message}") | ||
throw e | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
app/src/main/java/com/owori/android/module/NetworkConnectionChecker.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,38 @@ | ||
package com.owori.android.module | ||
|
||
import android.content.Context | ||
import android.net.ConnectivityManager | ||
import android.net.NetworkCapabilities | ||
import android.net.NetworkRequest | ||
|
||
/* | ||
* Created by JJJoonngg | ||
*/ | ||
|
||
class NetworkConnectionChecker(context: Context) : ConnectivityManager.NetworkCallback() { | ||
|
||
private val networkRequest: NetworkRequest = NetworkRequest.Builder() | ||
.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR) | ||
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI) | ||
.build() | ||
private val connectivityManager: ConnectivityManager = | ||
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | ||
|
||
fun register() { | ||
connectivityManager.registerNetworkCallback(networkRequest, this) | ||
} | ||
|
||
fun unregister() { | ||
connectivityManager.unregisterNetworkCallback(this) | ||
} | ||
|
||
fun isOnline(): Boolean { | ||
val nw = connectivityManager.activeNetwork ?: return false | ||
val actNw = connectivityManager.getNetworkCapabilities(nw) ?: return false | ||
return when { | ||
actNw.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true | ||
actNw.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true | ||
else -> false | ||
} | ||
} | ||
} |
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,4 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<resources> | ||
<string name="base_url">http://owori.store/api/v1</string> | ||
</resources> |
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