-
Notifications
You must be signed in to change notification settings - Fork 3
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
1 parent
789f070
commit 672c3d8
Showing
5 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
frontend/app/src/main/java/kr/ac/tukorea/whereareu/util/AppConfig.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,5 @@ | ||
package kr.ac.tukorea.whereareu.util | ||
|
||
object AppConfig { | ||
const val TAG_DEBUG = "TAG_DEBUG" | ||
} |
37 changes: 37 additions & 0 deletions
37
frontend/app/src/main/java/kr/ac/tukorea/whereareu/util/network/HandleApi.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,37 @@ | ||
package kr.ac.tukorea.whereareu.util.network | ||
|
||
import kr.ac.tukorea.whereareu.WhereAreUApplication | ||
import kr.ac.tukorea.whereareu.di.NetworkModule | ||
import retrofit2.Response | ||
|
||
suspend fun <T : Any, R : Any> handleApi( | ||
execute: suspend () -> Response<T>, | ||
mapper: (T) -> R | ||
): NetworkResult<R> { | ||
if (WhereAreUApplication.isOnline().not()) { | ||
return NetworkResult.Error(Exception(NetworkModule.NETWORK_EXCEPTION_OFFLINE_CASE)) | ||
} | ||
|
||
return try { | ||
val response = execute() | ||
val body = response.body() | ||
if (response.isSuccessful) { | ||
body?.let { | ||
NetworkResult.Success(mapper(it)) | ||
} ?: run { | ||
throw NullPointerException(NetworkModule.NETWORK_EXCEPTION_BODY_IS_NULL) | ||
} | ||
} else { | ||
getFailDataResult(body, response) | ||
} | ||
} catch (e: Exception) { | ||
NetworkResult.Error(e) | ||
} | ||
} | ||
|
||
|
||
private fun <T : Any> getFailDataResult(body: T?, response: Response<T>) = body?.let { | ||
NetworkResult.Fail(statusCode = response.code(), message = it.toString()) | ||
} ?: run { | ||
NetworkResult.Fail(statusCode = response.code(), message = response.message()) | ||
} |
34 changes: 34 additions & 0 deletions
34
frontend/app/src/main/java/kr/ac/tukorea/whereareu/util/network/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,34 @@ | ||
package kr.ac.tukorea.whereareu.util.network | ||
|
||
import android.content.Context | ||
import android.net.ConnectivityManager | ||
import android.net.NetworkCapabilities | ||
import android.net.NetworkRequest | ||
|
||
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 | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
frontend/app/src/main/java/kr/ac/tukorea/whereareu/util/network/NetworkResult.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,37 @@ | ||
package kr.ac.tukorea.whereareu.util.network | ||
|
||
sealed class NetworkResult<out T> { | ||
data class Success<T>(val data: T) : NetworkResult<T>() | ||
data class Fail(val statusCode: Int, val message: String) : NetworkResult<Nothing>() | ||
data class Error(val exception: Exception) : NetworkResult<Nothing>() | ||
} | ||
|
||
inline fun <T> NetworkResult<T>.onSuccess(action: (T) -> Unit): NetworkResult<T> { | ||
if (this is NetworkResult.Success) { | ||
action(data) | ||
} | ||
return this | ||
} | ||
|
||
inline fun <T> NetworkResult<T>.onFail(resultCode: (Int) -> Unit): NetworkResult<T> { | ||
if (this is NetworkResult.Fail) { | ||
resultCode(this.statusCode) | ||
} | ||
return this | ||
} | ||
|
||
inline fun <T> NetworkResult<T>.onError(action: (Exception) -> Unit): NetworkResult<T> { | ||
if (this is NetworkResult.Fail) { | ||
action(IllegalArgumentException("code : ${this.statusCode}, message : ${this.message}")) | ||
} else if (this is NetworkResult.Error) { | ||
action(this.exception) | ||
} | ||
return this | ||
} | ||
|
||
inline fun <T> NetworkResult<T>.onException(action: (Exception) -> Unit): NetworkResult<T> { | ||
if (this is NetworkResult.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,24 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context=".presentation.login.SplashActivity" | ||
android:background="@color/yellow"> | ||
|
||
<TextView | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:fontFamily="@font/jalnan2" | ||
android:text="어디U" | ||
android:textSize="60dp" | ||
android:textColor="@color/white" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
android:rotation="-30"/> | ||
|
||
|
||
</androidx.constraintlayout.widget.ConstraintLayout> |