Skip to content

Commit

Permalink
[FEAT/#1] Data Layer 분리 및 자동 로그인 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
b1urrrr committed Apr 5, 2023
1 parent d0d287a commit 3b5df27
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.android.go.sopt.data.repository

import org.android.go.sopt.data.entity.User
import org.android.go.sopt.data.source.LocalPrefDataSource
import org.android.go.sopt.domain.AuthRepository
import javax.inject.Inject

class AuthRepositoryImpl @Inject constructor(
private val localPrefDataSource: LocalPrefDataSource,
) : AuthRepository {
override fun setAutoLogin(isAutoLogin: Boolean) {
localPrefDataSource.isAutoLogin = isAutoLogin
}

override fun getAutoLogin(): Boolean = localPrefDataSource.isAutoLogin

override fun setSignedUpUser(user: User) {
localPrefDataSource.signedUpUser = user
}

override fun getSignedUpUser(): User? = localPrefDataSource.signedUpUser
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.android.go.sopt.data.source

import android.content.SharedPreferences
import androidx.core.content.edit
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import org.android.go.sopt.data.entity.User
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class LocalPrefDataSource @Inject constructor(
private val prefs: SharedPreferences,
) {
var isAutoLogin: Boolean
set(value) = prefs.edit { putBoolean(PREF_IS_AUTO_LOGIN, value) }
get() = prefs.getBoolean(PREF_IS_AUTO_LOGIN, false)

var signedUpUser: User?
set(value) = prefs.edit {
val user = GsonBuilder().create().toJson(value)
putString(PREF_SIGNED_UP_USER, user)
}
get() {
val user = prefs.getString(PREF_SIGNED_UP_USER, "")
return try {
Gson().fromJson(user, User::class.java)
} catch (e: Exception) {
null
}
}

companion object {
const val PREF_IS_AUTO_LOGIN = "IS_AUTO_LOGIN"
const val PREF_SIGNED_UP_USER = "SIGNED_UP_USER"
}
}
19 changes: 19 additions & 0 deletions app/src/main/java/org/android/go/sopt/di/RepositoryModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.android.go.sopt.di

import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import org.android.go.sopt.data.repository.AuthRepositoryImpl
import org.android.go.sopt.domain.AuthRepository
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
abstract class RepositoryModule {
@Binds
@Singleton
abstract fun providesAuthRepository(
authRepositoryImpl: AuthRepositoryImpl,
): AuthRepository
}
27 changes: 27 additions & 0 deletions app/src/main/java/org/android/go/sopt/di/SharedPrefModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.android.go.sopt.di

import android.content.Context
import android.content.SharedPreferences
import androidx.security.crypto.EncryptedSharedPreferences
import androidx.security.crypto.MasterKey
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object SharedPrefModule {
@Provides
@Singleton
fun providesLocalPreferences(@ApplicationContext context: Context): SharedPreferences =
EncryptedSharedPreferences.create(
context,
context.packageName,
MasterKey.Builder(context).setKeyScheme(MasterKey.KeyScheme.AES256_GCM).build(),
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM,
)
}
13 changes: 13 additions & 0 deletions app/src/main/java/org/android/go/sopt/domain/AuthRepository.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.android.go.sopt.domain

import org.android.go.sopt.data.entity.User

interface AuthRepository {
fun setAutoLogin(isAutoLogin: Boolean)

fun getAutoLogin(): Boolean

fun setSignedUpUser(user: User)

fun getSignedUpUser(): User?
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.app.Activity
import android.content.Intent
import android.content.Intent.EXTRA_USER
import android.os.Bundle
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import dagger.hilt.android.AndroidEntryPoint
Expand All @@ -22,8 +21,6 @@ import org.android.go.sopt.util.extension.* // ktlint-disable no-wildcard-import
class LoginActivity : BindingActivity<ActivityLoginBinding>(R.layout.activity_login) {
private val viewModel by viewModels<LoginViewModel>()

private lateinit var signupResultLauncher: ActivityResultLauncher<Intent>

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding.vm = viewModel
Expand All @@ -38,7 +35,7 @@ class LoginActivity : BindingActivity<ActivityLoginBinding>(R.layout.activity_lo
}

private fun initSignupBtnClickListener() {
signupResultLauncher =
val signupResultLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
val resultData = result.data ?: return@registerForActivityResult
Expand All @@ -59,10 +56,7 @@ class LoginActivity : BindingActivity<ActivityLoginBinding>(R.layout.activity_lo
when (state) {
is Success -> {
showToast(getString(R.string.login_login_success_msg))
Intent(this, MainActivity::class.java).apply {
this.putExtra(EXTRA_USER, viewModel.signedUpUser)
startActivity(this)
}
startActivity(Intent(this, MainActivity::class.java))
finish()
}
is Failure -> showSnackbar(binding.root, getString(R.string.wrong_input_msg))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import org.android.go.sopt.data.entity.User
import org.android.go.sopt.domain.AuthRepository
import org.android.go.sopt.util.UiState
import org.android.go.sopt.util.UiState.Failure
import org.android.go.sopt.util.UiState.Success
import javax.inject.Inject

@HiltViewModel
class LoginViewModel @Inject constructor() : ViewModel() {
class LoginViewModel @Inject constructor(
private val authRepository: AuthRepository,
) : ViewModel() {
var signedUpUser = User()

private val _loginState = MutableLiveData<UiState>()
Expand All @@ -21,6 +24,14 @@ class LoginViewModel @Inject constructor() : ViewModel() {
val id = MutableLiveData("")
val pwd = MutableLiveData("")

init {
checkAutoLogin()
}

private fun checkAutoLogin() {
if (authRepository.getAutoLogin()) _loginState.value = Success
}

fun setSavedUser(savedUser: User) {
this.signedUpUser = savedUser
}
Expand All @@ -31,6 +42,7 @@ class LoginViewModel @Inject constructor() : ViewModel() {
fun login() {
if (!isValidInput()) {
_loginState.value = Failure(null)
authRepository.setAutoLogin(true)
return
}
_loginState.value = Success
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
package org.android.go.sopt.presentation.main

import android.content.Intent.EXTRA_USER
import android.os.Bundle
import androidx.activity.viewModels
import dagger.hilt.android.AndroidEntryPoint
import org.android.go.sopt.R
import org.android.go.sopt.data.entity.User
import org.android.go.sopt.databinding.ActivityMainBinding
import org.android.go.sopt.util.binding.BindingActivity
import org.android.go.sopt.util.extension.getCompatibleParcelableExtra

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

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

getUserData()
}

private fun getUserData() {
if (intent.hasExtra(EXTRA_USER)) {
intent.getCompatibleParcelableExtra<User>(EXTRA_USER)?.let { user ->
with(binding) {
tvMainName.text = getString(R.string.main_name, user.name)
tvMainSpecialty.text = getString(R.string.main_specialty, user.specialty)
tvMainMbti.text = getString(R.string.main_mbti, user.mbti)
}
viewModel.getSignedUpUser().also { user ->
with(binding) {
tvMainName.text = getString(R.string.main_name, user.name)
tvMainSpecialty.text = getString(R.string.main_specialty, user.specialty)
tvMainMbti.text = getString(R.string.main_mbti, user.mbti)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.android.go.sopt.presentation.main

import androidx.lifecycle.ViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import org.android.go.sopt.data.entity.User
import org.android.go.sopt.domain.AuthRepository
import javax.inject.Inject

@HiltViewModel
class MainViewModel @Inject constructor(
private val authRepository: AuthRepository,
) : ViewModel() {
fun getSignedUpUser(): User = authRepository.getSignedUpUser() ?: User()
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import org.android.go.sopt.data.entity.User
import org.android.go.sopt.domain.AuthRepository
import org.android.go.sopt.util.UiState
import org.android.go.sopt.util.UiState.Failure
import org.android.go.sopt.util.UiState.Success
Expand All @@ -13,7 +14,9 @@ import org.android.go.sopt.util.type.MBTI
import javax.inject.Inject

@HiltViewModel
class SignupViewModel @Inject constructor() : ViewModel() {
class SignupViewModel @Inject constructor(
private val authRepository: AuthRepository,
) : ViewModel() {
private val _signupState = MutableLiveData<UiState>()
val signupState: LiveData<UiState>
get() = _signupState
Expand All @@ -37,13 +40,14 @@ class SignupViewModel @Inject constructor() : ViewModel() {
_signupState.value = Failure(INVALID_PWD_CODE)
return
}
authRepository.setSignedUpUser(getUser())
_signupState.value = Success
}

fun getUser(): User {
return User(
id.value!!.trim(),
pwd.value!!.trim(),
requireNotNull(id.value).trim(),
requireNotNull(pwd.value).trim(),
name.value?.trim(),
specialty.value?.trim(),
safeValueOf<MBTI>(mbti.value?.trim()?.uppercase()),
Expand Down

0 comments on commit 3b5df27

Please sign in to comment.