Skip to content

Commit

Permalink
Merge pull request #58 from SSUDevelog/feature/#54-refactor-server
Browse files Browse the repository at this point in the history
Feature/#54 version config & refactor
  • Loading branch information
lsakee authored Nov 1, 2023
2 parents a9010b9 + df9ddc7 commit ec8d403
Show file tree
Hide file tree
Showing 26 changed files with 230 additions and 134 deletions.
2 changes: 2 additions & 0 deletions app/src/main/java/com/velogandroid/di/RetrofitModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ object RetrofitModule {
@Provides
@Singleton
fun provideDataStore(DataStore: TokenImpl): SharedPreferenceToken = DataStore

@Provides
@Singleton
@Token
fun provideAuthInterceptor(interceptor: TokenInterceptor): Interceptor = interceptor

@Provides
@Singleton
fun provideLoggingInterceptor(): HttpLoggingInterceptor {
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/java/Configuration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ object Configuration {
const val TARGET_SDK = 33
const val MIN_SDK = 26
const val VERSION_CODE = 1
const val VERSION_NAME = "1.0"
const val VERSION_NAME = "1.0.0"
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ abstract class BindingDialogFragment<T : ViewDataBinding>(
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
super.onDestroyView()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ abstract class BindingFragment<T : ViewDataBinding>(


override fun onDestroyView() {
super.onDestroyView()
_binding = null
super.onDestroyView()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ class TokenImpl @Inject constructor(
override var token: String
get() = prefs.getString("AccessToken", "")?:""
set(value) = prefs.edit { putString("AccessToken", value) }

override var checkLogin : Boolean
get() = prefs.getBoolean("CheckLogin",false)
set(value) = prefs.edit{putBoolean("CheckLogin",value)}

}
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
package com.velogm.data.repositoryimpl

import com.velogm.data.datasource.SharedPreferencesDataSource
import com.velogm.data_local.datasource.TokenImpl
import com.velogm.domain.repository.AuthRepository
import javax.inject.Inject

class AuthRepositoryImpl @Inject constructor(
private val sharedPrefDataSource: SharedPreferencesDataSource
private val sharedPrefDataSource: TokenImpl
) : AuthRepository {
override fun saveAccessToken(a: String) {
sharedPrefDataSource.accessToken = a
sharedPrefDataSource.token = a
}

override fun getAccessToken(): String {
return sharedPrefDataSource.accessToken ?: ""
}

override fun saveRefreshToken(b: String) {
sharedPrefDataSource.refreshToken = b
}

override fun getRefreshToken(): String {
return sharedPrefDataSource.refreshToken ?: ""
return sharedPrefDataSource.token ?: ""
}

override fun checkLogin(): Boolean {
Expand All @@ -32,11 +24,11 @@ class AuthRepositoryImpl @Inject constructor(
}

override fun getWithdrawal(): Boolean {
return sharedPrefDataSource.withdrawal
return sharedPrefDataSource.checkLogin
}

override fun saveWithdrawal(checkWithdrawal: Boolean) {
sharedPrefDataSource.withdrawal = checkWithdrawal
sharedPrefDataSource.checkLogin = checkWithdrawal
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
package com.velogm.data.repositoryimpl

import com.velogm.data.datasource.TagDataSource
import com.velogm.domain.OutResult
import com.velogm.domain.model.PostList
import com.velogm.domain.model.Tag
import com.velogm.domain.repository.TagRepository
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import retrofit2.HttpException
import java.lang.RuntimeException
import javax.inject.Inject

class TagRepositoryImpl @Inject constructor(
private val dataSource: TagDataSource
) : TagRepository {

override suspend fun getTag(): Flow<List<Tag>> {
return flow {
val result = kotlin.runCatching {
dataSource.getTag().map { Tag(it) }
}
emit(result.getOrDefault(emptyList()))
override suspend fun getTag(): Flow<OutResult<List<Tag>>> = flow {
val result = runCatching {
val tag = dataSource.getTag().map { Tag(it) }
OutResult.Success(tag)
}
val outcome = result.getOrElse {
val errorCode = (it as? HttpException)?.code() ?: -1
OutResult.Failure(error = VelogHttpException(errorCode, "$errorCode"))
}
emit(outcome)
}

override suspend fun getPopularTag(): Flow<List<Tag>> {
Expand Down Expand Up @@ -56,4 +62,9 @@ class TagRepositoryImpl @Inject constructor(
emit(result.getOrDefault(PostList(emptyList())))
}
}
}
}

class VelogHttpException(
val httpCode: Int,
override val message: String,
) : RuntimeException()
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.velogm.domain

sealed class NetworkErrorHandling {
object Unauthorized : NetworkErrorHandling()
object ServerError : NetworkErrorHandling()
object OtherError : NetworkErrorHandling()
}
20 changes: 20 additions & 0 deletions domain/src/main/java/com/velogm/domain/OutResult.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.velogm.domain

import kotlinx.coroutines.flow.Flow

sealed class OutResult<out T> {
data class Success<T>(val data: T) : OutResult<T>()
data class Failure<T>(val error: Throwable?) : OutResult<T>()
}

suspend fun <T> Flow<OutResult<T>>.collectOutResult(
handleSuccess: (OutResult.Success<T>) -> Unit = {},
handleFail: (OutResult.Failure<T>) -> Unit = {},
) {
collect { outcome ->
when (outcome) {
is OutResult.Success -> handleSuccess(outcome)
is OutResult.Failure -> handleFail(outcome)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ package com.velogm.domain

interface SharedPreferenceToken {
var token:String
var checkLogin: Boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package com.velogm.domain.repository
interface AuthRepository {
fun saveAccessToken(a: String)
fun getAccessToken(): String
fun saveRefreshToken(b: String)
fun getRefreshToken(): String
fun checkLogin(): Boolean
fun saveCheckLogin(checkLogin: Boolean)
fun getWithdrawal(): Boolean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.velogm.domain.repository

import com.velogm.domain.OutResult
import com.velogm.domain.model.PostList
import com.velogm.domain.model.Tag
import kotlinx.coroutines.flow.Flow

interface TagRepository {
suspend fun getTag(): Flow<List<Tag>>
suspend fun getTag(): Flow<OutResult<List<Tag>>>
suspend fun getPopularTag():Flow<List<Tag>>
suspend fun deleteTag(tag:String):Flow<String>
suspend fun addTag(tag:String):Flow<String>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.velogm.domain.usecase

import com.velogm.domain.OutResult
import com.velogm.domain.model.Tag
import com.velogm.domain.repository.TagRepository
import kotlinx.coroutines.flow.Flow

class GetTagUseCase(
private val repository: TagRepository
) {
suspend operator fun invoke(): Flow<List<Tag>> =
suspend operator fun invoke(): Flow<OutResult<List<Tag>>> =
repository.getTag()
}
5 changes: 5 additions & 0 deletions presentation/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ android {
buildConfigField("String", "CLIENT_ID", Properties().apply {
load(project.rootProject.file("local.properties").inputStream())
}["client.id"].toString())
buildConfigField("String", "VERSION_NAMES", Properties().apply {
load(project.rootProject.file("local.properties").inputStream())
}["versions.name"].toString())
}
release {
isMinifyEnabled = false
Expand Down Expand Up @@ -98,4 +101,6 @@ dependencies {
implementation(Google.GOOGLE_FIREBASE_ANALYTICS)
implementation(Google.GOOGLE_FIREBASE_MESSAGING)

implementation("com.google.firebase:firebase-config-ktx")

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.velogm.presentation.ui

import android.app.AlertDialog
import android.content.Intent
import android.os.Bundle
import android.view.View
Expand All @@ -9,27 +10,35 @@ import androidx.lifecycle.lifecycleScope
import androidx.navigation.NavController
import androidx.navigation.fragment.findNavController
import androidx.navigation.ui.setupWithNavController
import com.velogm.presentation.BuildConfig
import com.google.firebase.ktx.Firebase
import com.google.firebase.remoteconfig.FirebaseRemoteConfig
import com.google.firebase.remoteconfig.ktx.remoteConfig
import com.google.firebase.remoteconfig.ktx.remoteConfigSettings
import com.velogm.core_ui.base.BindingActivity
import com.velogm.core_ui.context.longToast
import com.velogm.core_ui.view.UiState
import com.velogm.presentation.R
import com.velogm.presentation.databinding.ActivityMainBinding
import com.velogm.presentation.ui.signin.SignCheck
import com.velogm.presentation.ui.signin.SignInActivity
import com.velogm.presentation.ui.signin.SignViewModel
import com.velogm.presentation.ui.signin.SignInViewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import timber.log.Timber

@AndroidEntryPoint
class MainActivity : BindingActivity<ActivityMainBinding>(R.layout.activity_main) {
private val mainViewModel by viewModels<SignViewModel>()

private val mainViewModel by viewModels<SignInViewModel>()
private lateinit var remoteConfig: FirebaseRemoteConfig
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
initView()
setupLogoutState()
setUpWithdrawalState()
setRemoteConfig()
fetchAppVersion()
}

private fun initView() {
Expand Down Expand Up @@ -94,4 +103,36 @@ class MainActivity : BindingActivity<ActivityMainBinding>(R.layout.activity_main

}
}

private fun setRemoteConfig() {
remoteConfig = Firebase.remoteConfig
val configSettings = remoteConfigSettings {
minimumFetchIntervalInSeconds = 1800
}
remoteConfig.setConfigSettingsAsync(configSettings)
}

private fun fetchAppVersion() {
var appVersion = remoteConfig.getString(REMOTE_KEY_APP_VERSION)

remoteConfig.fetchAndActivate()
.addOnCompleteListener {
if (it.isSuccessful) {
if (appVersion.equals(BuildConfig.VERSION_NAMES))
Timber.tag("remoteConfig").d("${BuildConfig.VERSION_NAMES}")
else {
AlertDialog.Builder(this)
.setTitle("Alert Version")
.setMessage("새로운 ${appVersion}이 출시했습니다.")
.show()
}
} else {
Timber.tag("remoteConfig").d("fail")
}
}
}

companion object {
private const val REMOTE_KEY_APP_VERSION = "app_version"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import android.os.Bundle
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import com.velogm.presentation.ui.signin.SignInActivity
import com.velogm.presentation.ui.signin.SignViewModel
import com.velogm.presentation.ui.signin.SignInViewModel
import dagger.hilt.android.AndroidEntryPoint
import timber.log.Timber

@AndroidEntryPoint
class SplashActivity : AppCompatActivity() {

private val viewModel by viewModels<SignViewModel>()
private val viewModel by viewModels<SignInViewModel>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (viewModel.getCheckLogin()) {
Expand Down
Loading

0 comments on commit ec8d403

Please sign in to comment.