From d032403b55acd88329380ccd27d688dc958ff659 Mon Sep 17 00:00:00 2001 From: hwiwon Date: Wed, 13 Nov 2024 00:36:44 +0900 Subject: [PATCH] =?UTF-8?q?[feat/#8]=20SharedPreferences=EB=A1=9C=20?= =?UTF-8?q?=ED=86=A0=ED=81=B0=20=EC=A0=80=EC=9E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/SharedPreferencesHelper.kt | 34 +++++++++++++++++++ .../and/presentation/ui/auth/SignInScreen.kt | 3 +- .../presentation/viewmodel/SignInViewModel.kt | 14 +++++++- 3 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/org/sopt/and/data/repository/SharedPreferencesHelper.kt diff --git a/app/src/main/java/org/sopt/and/data/repository/SharedPreferencesHelper.kt b/app/src/main/java/org/sopt/and/data/repository/SharedPreferencesHelper.kt new file mode 100644 index 0000000..f32de53 --- /dev/null +++ b/app/src/main/java/org/sopt/and/data/repository/SharedPreferencesHelper.kt @@ -0,0 +1,34 @@ +package org.sopt.and.data.repository + +import android.content.Context +import android.content.SharedPreferences +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 AppModule { + @Provides + @Singleton + fun provideSharedPreferencesHelper(@ApplicationContext context: Context): SharedPreferencesHelper { + return SharedPreferencesHelper(context) + } +} + + +class SharedPreferencesHelper(context: Context) { + private val prefs: SharedPreferences = + context.getSharedPreferences("app_prefs", Context.MODE_PRIVATE) + + fun saveToken(token: String) { + prefs.edit().putString("auth_token", token).apply() + } + + fun getToken(): String? { + return prefs.getString("auth_token", null) + } +} \ No newline at end of file diff --git a/app/src/main/java/org/sopt/and/presentation/ui/auth/SignInScreen.kt b/app/src/main/java/org/sopt/and/presentation/ui/auth/SignInScreen.kt index 2744060..866b0f1 100644 --- a/app/src/main/java/org/sopt/and/presentation/ui/auth/SignInScreen.kt +++ b/app/src/main/java/org/sopt/and/presentation/ui/auth/SignInScreen.kt @@ -24,6 +24,7 @@ import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color +import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.input.PasswordVisualTransformation import androidx.compose.ui.text.input.VisualTransformation @@ -46,6 +47,7 @@ fun SignInScreen( signInViewModel: SignInViewModel = hiltViewModel(), navController: NavHostController ) { + val context = LocalContext.current val signInResult by signInViewModel.loginResult.observeAsState() var isPasswordVisible by remember { mutableStateOf(false) } @@ -128,7 +130,6 @@ fun SignInScreen( ) signInResult?.let { result -> if (result.isSuccess) { - // 토큰 받아와서 저장 coroutineScope.launch { snackbarHostState.showSnackbar("로그인 성공!") } diff --git a/app/src/main/java/org/sopt/and/presentation/viewmodel/SignInViewModel.kt b/app/src/main/java/org/sopt/and/presentation/viewmodel/SignInViewModel.kt index e3d78b6..9663434 100644 --- a/app/src/main/java/org/sopt/and/presentation/viewmodel/SignInViewModel.kt +++ b/app/src/main/java/org/sopt/and/presentation/viewmodel/SignInViewModel.kt @@ -1,5 +1,6 @@ package org.sopt.and.presentation.viewmodel +import android.util.Log import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.setValue @@ -12,11 +13,13 @@ import kotlinx.coroutines.launch import org.sopt.and.data.dto.RequestLoginData import org.sopt.and.data.dto.ResponseLogin import org.sopt.and.data.repository.Auth.LoginRepository +import org.sopt.and.data.repository.SharedPreferencesHelper import javax.inject.Inject @HiltViewModel class SignInViewModel @Inject constructor( - private val loginRepository: LoginRepository + private val loginRepository: LoginRepository, + private val sharedPreferencesHelper: SharedPreferencesHelper ) : ViewModel() { private val _loginResult = MutableLiveData>() @@ -26,10 +29,19 @@ class SignInViewModel @Inject constructor( viewModelScope.launch { val loginRequest = RequestLoginData(username, password) val result = loginRepository.postLogin(loginRequest) + result.onSuccess { + val token = it.result.token + saveToken(token) + Log.d("SignInResultViewModel", "토큰이 저장됐을까.. $token") + } _loginResult.postValue(result) } } + fun saveToken(token: String) { + sharedPreferencesHelper.saveToken(token) + } + private var _username by mutableStateOf("") val username: String get() = _username