Skip to content

Commit

Permalink
chore: 병합 후 누락 파일 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
kyujin0911 committed May 16, 2024
1 parent 789f070 commit 672c3d8
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 0 deletions.
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"
}
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())
}
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
}
}
}
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
}
24 changes: 24 additions & 0 deletions frontend/app/src/main/res/layout/activity_splash.xml
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>

0 comments on commit 672c3d8

Please sign in to comment.