From 2dd865fa5ee3f7fb9a3b818cbc47b65a2e1dff28 Mon Sep 17 00:00:00 2001 From: KIM SEONGGYU Date: Tue, 16 Mar 2021 14:13:44 +0900 Subject: [PATCH 1/3] feat: Save UserLoginData to SheredPreference --- .../santamanitto/network/NetworkModule.kt | 7 ++--- .../preference/SharedPreferenceModule.kt | 1 + .../santamanitto/user/AccessTokenContainer.kt | 5 ---- .../org/sopt/santamanitto/user/UserModule.kt | 9 ++---- .../user/data/source/UserCachedDataSource.kt | 28 ++++++++++++++++--- .../source/LoginUserCachedDataSourceTest.kt | 8 +++--- 6 files changed, 35 insertions(+), 23 deletions(-) delete mode 100644 app/src/main/java/org/sopt/santamanitto/user/AccessTokenContainer.kt diff --git a/app/src/main/java/org/sopt/santamanitto/network/NetworkModule.kt b/app/src/main/java/org/sopt/santamanitto/network/NetworkModule.kt index 2e654d0c..9aa3a39d 100644 --- a/app/src/main/java/org/sopt/santamanitto/network/NetworkModule.kt +++ b/app/src/main/java/org/sopt/santamanitto/network/NetworkModule.kt @@ -7,10 +7,9 @@ import dagger.hilt.android.components.ApplicationComponent import okhttp3.OkHttpClient import okhttp3.logging.HttpLoggingInterceptor import org.sopt.santamanitto.SecretString -import org.sopt.santamanitto.user.AccessTokenContainer +import org.sopt.santamanitto.preference.UserPreferenceManager import retrofit2.Retrofit import retrofit2.converter.gson.GsonConverterFactory -import javax.inject.Named import javax.inject.Qualifier import javax.inject.Singleton @@ -33,11 +32,11 @@ class NetworkModule { @Provides @Singleton @AuthInterceptorOkHttpClient - fun provideAuthInterceptorOkHttpClient(accessTokenContainer: AccessTokenContainer): OkHttpClient { + fun provideAuthInterceptorOkHttpClient(userPreferenceManager: UserPreferenceManager): OkHttpClient { return OkHttpClient.Builder() .addInterceptor { val request = it.request().newBuilder() - .addHeader("jwt", accessTokenContainer.accessToken) + .addHeader("jwt", userPreferenceManager.getAccessToken()!!) .build() it.proceed(request) } diff --git a/app/src/main/java/org/sopt/santamanitto/preference/SharedPreferenceModule.kt b/app/src/main/java/org/sopt/santamanitto/preference/SharedPreferenceModule.kt index 56109fd3..9438adff 100644 --- a/app/src/main/java/org/sopt/santamanitto/preference/SharedPreferenceModule.kt +++ b/app/src/main/java/org/sopt/santamanitto/preference/SharedPreferenceModule.kt @@ -24,6 +24,7 @@ class SharedPreferenceModule { SharedPreferenceManagerImpl(sharedPreferences) @Provides + @Singleton fun provideUserPreferenceManager(sharedPreferenceManager: SharedPreferenceManager): UserPreferenceManager = UserPreferenceManager(sharedPreferenceManager) } \ No newline at end of file diff --git a/app/src/main/java/org/sopt/santamanitto/user/AccessTokenContainer.kt b/app/src/main/java/org/sopt/santamanitto/user/AccessTokenContainer.kt deleted file mode 100644 index 1fee62c5..00000000 --- a/app/src/main/java/org/sopt/santamanitto/user/AccessTokenContainer.kt +++ /dev/null @@ -1,5 +0,0 @@ -package org.sopt.santamanitto.user - -class AccessTokenContainer { - lateinit var accessToken: String -} \ No newline at end of file diff --git a/app/src/main/java/org/sopt/santamanitto/user/UserModule.kt b/app/src/main/java/org/sopt/santamanitto/user/UserModule.kt index c4a638de..fd39ca29 100644 --- a/app/src/main/java/org/sopt/santamanitto/user/UserModule.kt +++ b/app/src/main/java/org/sopt/santamanitto/user/UserModule.kt @@ -8,6 +8,7 @@ import dagger.Provides import dagger.hilt.InstallIn import dagger.hilt.android.components.ApplicationComponent import dagger.hilt.android.qualifiers.ApplicationContext +import org.sopt.santamanitto.preference.UserPreferenceManager import org.sopt.santamanitto.user.data.source.UserCachedDataSource import org.sopt.santamanitto.user.data.source.UserDataSource import javax.inject.Named @@ -28,10 +29,6 @@ class UserModule { @Named("cached") fun provideUserDataSource( @Named("remote") userRemoteDataSource: UserDataSource, - accessTokenContainer: AccessTokenContainer - ) : UserDataSource = UserCachedDataSource(userRemoteDataSource, accessTokenContainer) - - @Provides - @Singleton - fun provideAccessToken() : AccessTokenContainer = AccessTokenContainer() + userPreferenceManager: UserPreferenceManager + ) : UserDataSource = UserCachedDataSource(userRemoteDataSource, userPreferenceManager) } \ No newline at end of file diff --git a/app/src/main/java/org/sopt/santamanitto/user/data/source/UserCachedDataSource.kt b/app/src/main/java/org/sopt/santamanitto/user/data/source/UserCachedDataSource.kt index c8d8cb29..76b4eddf 100644 --- a/app/src/main/java/org/sopt/santamanitto/user/data/source/UserCachedDataSource.kt +++ b/app/src/main/java/org/sopt/santamanitto/user/data/source/UserCachedDataSource.kt @@ -1,7 +1,7 @@ package org.sopt.santamanitto.user.data.source +import org.sopt.santamanitto.preference.UserPreferenceManager import org.sopt.santamanitto.room.data.JoinedRoom -import org.sopt.santamanitto.user.AccessTokenContainer import org.sopt.santamanitto.user.data.LoginUser import org.sopt.santamanitto.user.data.User import org.sopt.santamanitto.user.data.source.UserDataSource.* @@ -9,7 +9,7 @@ import javax.inject.Named class UserCachedDataSource( @Named("remote") private val userRemoteDataSource: UserDataSource, - private val accessTokenContainer: AccessTokenContainer + private val userPreferenceManager: UserPreferenceManager ): UserDataSource { private var _cachedLoginUser: LoginUser? = null @@ -27,7 +27,7 @@ class UserCachedDataSource( override fun login(serialNumber: String, callback: LoginCallback) { userRemoteDataSource.login(serialNumber, object : LoginCallback { override fun onLoginSuccess(loginUser: LoginUser) { - accessTokenContainer.accessToken = loginUser.accessToken + initUserPreference(loginUser, serialNumber) _cachedLoginUser = loginUser callback.onLoginSuccess(loginUser) } @@ -41,7 +41,7 @@ class UserCachedDataSource( override fun createAccount(userName: String, serialNumber: String, callback: CreateAccountCallback) { userRemoteDataSource.createAccount(userName, serialNumber, object: CreateAccountCallback { override fun onCreateAccountSuccess(loginUser: LoginUser) { - accessTokenContainer.accessToken = loginUser.accessToken + initUserPreference(loginUser, serialNumber) _cachedLoginUser = loginUser callback.onCreateAccountSuccess(loginUser) } @@ -53,14 +53,17 @@ class UserCachedDataSource( } override fun getUserId(): Int { + initCachedLoginUser() return cachedLoginUser!!.id } override fun getAccessToken(): String { + initCachedLoginUser() return cachedLoginUser!!.accessToken } override fun getUserName(): String { + initCachedLoginUser() return cachedLoginUser!!.userName } @@ -98,4 +101,21 @@ class UserCachedDataSource( }) } } + + private fun initUserPreference(loginUser: LoginUser, serialNumber: String) { + userPreferenceManager.run { + setAccessToken(loginUser.accessToken) + setUserId(loginUser.id) + setUserUpdateTime(loginUser.userName) + setSerialNumber(serialNumber) + } + } + + private fun initCachedLoginUser() { + if (_cachedLoginUser == null) { + userPreferenceManager.let { + _cachedLoginUser = LoginUser(it.getUserName()!!, it.getSerialNumber()!!, it.getUserId()!!, it.getAccessToken()!!) + } + } + } } \ No newline at end of file diff --git a/app/src/test/java/org/sopt/santamanitto/user/data/source/LoginUserCachedDataSourceTest.kt b/app/src/test/java/org/sopt/santamanitto/user/data/source/LoginUserCachedDataSourceTest.kt index 44f3153b..c8237e19 100644 --- a/app/src/test/java/org/sopt/santamanitto/user/data/source/LoginUserCachedDataSourceTest.kt +++ b/app/src/test/java/org/sopt/santamanitto/user/data/source/LoginUserCachedDataSourceTest.kt @@ -10,8 +10,8 @@ import org.mockito.Captor import org.mockito.Mock import org.mockito.Mockito.verify import org.mockito.MockitoAnnotations +import org.sopt.santamanitto.preference.UserPreferenceManager import org.sopt.santamanitto.room.data.JoinedRoom -import org.sopt.santamanitto.user.AccessTokenContainer import org.sopt.santamanitto.user.data.LoginUser import org.sopt.santamanitto.user.data.User import org.sopt.santamanitto.util.capture @@ -24,7 +24,7 @@ class LoginUserCachedDataSourceTest { @Mock private lateinit var mockLoginUser: LoginUser @Mock private lateinit var mockUser: User @Mock private lateinit var mockJoinedRooms: List - @Mock private lateinit var mockAccessTokenContainer: AccessTokenContainer + @Mock private lateinit var mockUserPreferenceManager: UserPreferenceManager @Mock private lateinit var loginCallback: UserDataSource.LoginCallback @Mock private lateinit var createAccountCallback: UserDataSource.CreateAccountCallback @@ -47,7 +47,7 @@ class LoginUserCachedDataSourceTest { @Before fun setUp() { MockitoAnnotations.initMocks(this) - userCachedDataSource = UserCachedDataSource(userRemoteDataSource, mockAccessTokenContainer) + userCachedDataSource = UserCachedDataSource(userRemoteDataSource, mockUserPreferenceManager) } @Test @@ -98,7 +98,7 @@ class LoginUserCachedDataSourceTest { fun accessTokenTest() { tryLogin() - assertThat(mockAccessTokenContainer.accessToken, `is`(mockLoginUser.accessToken)) + assertThat(mockUserPreferenceManager.getAccessToken(), `is`(mockLoginUser.accessToken)) } private fun tryLogin() { From 4491f059eae6f978050673fb5409f5f31b084575 Mon Sep 17 00:00:00 2001 From: KIM SEONGGYU Date: Tue, 16 Mar 2021 14:52:35 +0900 Subject: [PATCH 2/3] fix: No data after start activity --- .../room/manittoroom/ManittoRoomActivity.kt | 5 ++--- .../room/manittoroom/fragment/FinishFragment.kt | 13 +++++-------- .../room/manittoroom/fragment/MatchedFragment.kt | 14 ++++++-------- .../room/manittoroom/fragment/ResultFragment.kt | 4 ++++ 4 files changed, 17 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/org/sopt/santamanitto/room/manittoroom/ManittoRoomActivity.kt b/app/src/main/java/org/sopt/santamanitto/room/manittoroom/ManittoRoomActivity.kt index e7c8489e..401fc07a 100644 --- a/app/src/main/java/org/sopt/santamanitto/room/manittoroom/ManittoRoomActivity.kt +++ b/app/src/main/java/org/sopt/santamanitto/room/manittoroom/ManittoRoomActivity.kt @@ -1,11 +1,10 @@ package org.sopt.santamanitto.room.manittoroom -import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.util.Log import androidx.activity.viewModels -import androidx.databinding.DataBindingUtil -import androidx.databinding.DataBindingUtil.* +import androidx.appcompat.app.AppCompatActivity +import androidx.databinding.DataBindingUtil.setContentView import dagger.hilt.android.AndroidEntryPoint import org.sopt.santamanitto.R import org.sopt.santamanitto.databinding.ActivityManittoRoomBinding diff --git a/app/src/main/java/org/sopt/santamanitto/room/manittoroom/fragment/FinishFragment.kt b/app/src/main/java/org/sopt/santamanitto/room/manittoroom/fragment/FinishFragment.kt index fd02e3dd..50860c80 100644 --- a/app/src/main/java/org/sopt/santamanitto/room/manittoroom/fragment/FinishFragment.kt +++ b/app/src/main/java/org/sopt/santamanitto/room/manittoroom/fragment/FinishFragment.kt @@ -20,14 +20,6 @@ class FinishFragment: Fragment() { private val manittoRoomViewModel: ManittoRoomViewModel by activityViewModels() - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - manittoRoomViewModel.run { - refreshManittoRoomInfo() - getPersonalRelationInfo() - } - } - override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -47,6 +39,11 @@ class FinishFragment: Fragment() { }) } + manittoRoomViewModel.run { + refreshManittoRoomInfo() + getPersonalRelationInfo() + } + setOnClickListener() return binding.root diff --git a/app/src/main/java/org/sopt/santamanitto/room/manittoroom/fragment/MatchedFragment.kt b/app/src/main/java/org/sopt/santamanitto/room/manittoroom/fragment/MatchedFragment.kt index 60f94109..f35cf5c9 100644 --- a/app/src/main/java/org/sopt/santamanitto/room/manittoroom/fragment/MatchedFragment.kt +++ b/app/src/main/java/org/sopt/santamanitto/room/manittoroom/fragment/MatchedFragment.kt @@ -19,14 +19,6 @@ class MatchedFragment : Fragment() { private val manittoRoomViewModel: ManittoRoomViewModel by activityViewModels() - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - manittoRoomViewModel.run { - refreshManittoRoomInfo() - getPersonalRelationInfo() - } - } - override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { binding = FragmentMatchedBinding.inflate(inflater, container, false).apply { viewModel = manittoRoomViewModel @@ -41,6 +33,12 @@ class MatchedFragment : Fragment() { } }) } + + manittoRoomViewModel.run { + refreshManittoRoomInfo() + getPersonalRelationInfo() + } + initManittoTitle() setOnClickListener() diff --git a/app/src/main/java/org/sopt/santamanitto/room/manittoroom/fragment/ResultFragment.kt b/app/src/main/java/org/sopt/santamanitto/room/manittoroom/fragment/ResultFragment.kt index 01ffb072..45a44f94 100644 --- a/app/src/main/java/org/sopt/santamanitto/room/manittoroom/fragment/ResultFragment.kt +++ b/app/src/main/java/org/sopt/santamanitto/room/manittoroom/fragment/ResultFragment.kt @@ -35,6 +35,10 @@ class ResultFragment: Fragment() { recyclerviewResult.adapter = resultAdapter } + if (manittoRoomViewModel.members.value.isNullOrEmpty()) { + manittoRoomViewModel.refreshManittoRoomInfo() + } + manittoRoomViewModel.members.observe(viewLifecycleOwner) { binding.textviewResultTitle.text = String.format(getString(R.string.result_title), it.size) } From f47be1d87364ce2ee5c0dea17ce59b04fb635e7e Mon Sep 17 00:00:00 2001 From: KIM SEONGGYU Date: Tue, 16 Mar 2021 14:55:07 +0900 Subject: [PATCH 3/3] chore: Update versionCode --- app/build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 023c2c16..5a3a3613 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -16,8 +16,8 @@ android { applicationId "org.sopt.santamanitto" minSdkVersion 23 targetSdkVersion 30 - versionCode 4 - versionName "1.1.0" + versionCode 5 + versionName "1.1.1" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" }