Skip to content

Commit

Permalink
Merge pull request #26 from Team-Ampersand/feature/25_write_auth_pres…
Browse files Browse the repository at this point in the history
…entation_logic

🔀 :: (#25) write auth presentation logic
  • Loading branch information
Cjsghkd authored Sep 9, 2023
2 parents 814742f + 93dc3c7 commit 2d04273
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 24 deletions.
1 change: 1 addition & 0 deletions buildSrc/src/main/java/Dependency.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ object Dependency {
const val CORE_KTX = "androidx.core:core-ktx:${Versions.CORE_KTX}"
const val APP_COMPAT = "androidx.appcompat:appcompat:${Versions.APP_COMPAT}"
const val DATASTORE = "androidx.datastore:datastore-preferences:${Versions.DATASTORE}"
const val VIEWMODEl = "androidx.lifecycle:lifecycle-viewmodel-compose:${Versions.VIEWMODEL}"
}

object Google {
Expand Down
1 change: 1 addition & 0 deletions buildSrc/src/main/java/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ object Versions {
const val CORE_KTX = "1.9.0"
const val APP_COMPAT = "1.6.1"
const val DATASTORE = "1.0.0"
const val VIEWMODEL = "2.5.1"

const val MATERIAL = "1.8.0"
const val HILT = "2.44"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ interface LocalDataSource {
suspend fun saveToken(
accessToken: String,
refreshToken: String,
accessExp: String,
refreshExp: String
expiresAt: String
)

fun getAccessToken(): Flow<String>

fun getRefreshToken(): Flow<String>

fun getAccessTokenExp(): Flow<String>
fun getExpiresAt(): Flow<String>

fun getRefreshTokenExp(): Flow<String>
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,20 @@ class LocalDataSourceImpl @Inject constructor(
companion object {
private val ACCESS_TOKEN = stringPreferencesKey("access_token")
private val REFRESH_TOKEN = stringPreferencesKey("refresh_token")
private val ACCESS_EXP = stringPreferencesKey("access_exp")
private val REFRESH_EXP = stringPreferencesKey("refresh_exp")
private val EXPIRES_AT = stringPreferencesKey("expires_at")
}

override suspend fun saveToken(accessToken: String, refreshToken: String, accessExp: String, refreshExp: String) {
override suspend fun saveToken(accessToken: String, refreshToken: String, expiresAt: String) {
context.dataStore.edit {
it[ACCESS_TOKEN] = accessToken
it[REFRESH_TOKEN] = refreshToken
it[ACCESS_EXP] = accessExp
it[REFRESH_EXP] = refreshExp
it[EXPIRES_AT] = expiresAt
}
}

override fun getAccessToken(): Flow<String> = context.dataStore.data.map { it[ACCESS_TOKEN] ?: "" }

override fun getRefreshToken(): Flow<String> = context.dataStore.data.map { it[REFRESH_TOKEN] ?: "" }

override fun getAccessTokenExp(): Flow<String> = context.dataStore.data.map { it[ACCESS_EXP] ?: "" }

override fun getRefreshTokenExp(): Flow<String> = context.dataStore.data.map { it[REFRESH_EXP] ?: "" }
override fun getExpiresAt(): Flow<String> = context.dataStore.data.map { it[EXPIRES_AT] ?: "" }
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ class AuthRepositoryImpl @Inject constructor(
override suspend fun saveToken(
accessToken: String,
refreshToken: String,
accessExp: String,
refreshExp: String
expiresAt: String
) = localDataSource.saveToken(
accessToken,
refreshToken,
accessExp,
refreshExp
expiresAt
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ interface AuthRepository {
suspend fun saveToken(
accessToken: String,
refreshToken: String,
accessExp: String,
refreshExp: String
expiresAt: String
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ class SaveTokenUseCase @Inject constructor(
suspend operator fun invoke(
accessToken: String,
refreshToken: String,
accessExp: String,
refreshExp: String
expiresAt: String
) = kotlin.runCatching {
authRepository.saveToken(
accessToken,
refreshToken,
accessExp,
refreshExp
accessToken = accessToken,
refreshToken = refreshToken,
expiresAt = expiresAt
)
}
}
2 changes: 2 additions & 0 deletions presentation/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,6 @@ dependencies {

implementation(Dependency.Google.HILT)
kapt(Dependency.Google.HILT_COMPILER)

implementation(Dependency.Androidx.VIEWMODEl)
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.msg.presentation.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.msg.domain.model.auth.LoginRequestModel
import com.msg.domain.model.auth.LoginResponseModel
import com.msg.domain.usecase.auth.LoginUseCase
import com.msg.domain.usecase.auth.SaveTokenUseCase
import com.msg.presentation.exception.exceptionHandling
import com.msg.presentation.viewmodel.util.Event
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

class LoginViewModel @Inject constructor(
private val loginUseCase: LoginUseCase,
private val saveTokenUseCase: SaveTokenUseCase
) : ViewModel() {

private val _loginState = MutableStateFlow<Event<LoginResponseModel>>(Event.Loading)
val loginState = _loginState.asStateFlow()

fun login(loginRequestModel: LoginRequestModel) = viewModelScope.launch {
loginUseCase(loginRequestModel)
.onSuccess {
_loginState.value = Event.Success(it)
saveTokenUseCase(
accessToken = it.accessToken,
refreshToken = it.refreshToken,
expiresAt = it.expiresAt
)
}
.onFailure {
it.exceptionHandling(
badRequestAction = {},
notFoundAction = {}
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.msg.presentation.viewmodel.util

sealed class Event<out T>(val data: T? = null) {
object Loading : Event<Nothing>()
class Success<T>(data: T? = null) : Event<T>(data = data)
object BadRequest : Event<Nothing>()
object Unauthorized : Event<Nothing>()
object ForBidden : Event<Nothing>()
object NotFound : Event<Nothing>()
object NotAcceptable : Event<Nothing>()
object TimeOut : Event<Nothing>()
object Conflict : Event<Nothing>()
object Server : Event<Nothing>()
object UnKnown : Event<Nothing>()
}

0 comments on commit 2d04273

Please sign in to comment.