Skip to content

Commit

Permalink
Merge pull request #59 from SEONGGYU96/hotfix/npe_after_kill_process
Browse files Browse the repository at this point in the history
Hotfix/npe after kill process
  • Loading branch information
ethan-223 authored Mar 16, 2021
2 parents a442ef3 + f47be1d commit 121e082
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 44 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class SharedPreferenceModule {
SharedPreferenceManagerImpl(sharedPreferences)

@Provides
@Singleton
fun provideUserPreferenceManager(sharedPreferenceManager: SharedPreferenceManager): UserPreferenceManager =
UserPreferenceManager(sharedPreferenceManager)
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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?,
Expand All @@ -47,6 +39,11 @@ class FinishFragment: Fragment() {
})
}

manittoRoomViewModel.run {
refreshManittoRoomInfo()
getPersonalRelationInfo()
}

setOnClickListener()

return binding.root
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -41,6 +33,12 @@ class MatchedFragment : Fragment() {
}
})
}

manittoRoomViewModel.run {
refreshManittoRoomInfo()
getPersonalRelationInfo()
}

initManittoTitle()

setOnClickListener()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

This file was deleted.

9 changes: 3 additions & 6 deletions app/src/main/java/org/sopt/santamanitto/user/UserModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
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.*
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
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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
}

Expand Down Expand Up @@ -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()!!)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,7 +24,7 @@ class LoginUserCachedDataSourceTest {
@Mock private lateinit var mockLoginUser: LoginUser
@Mock private lateinit var mockUser: User
@Mock private lateinit var mockJoinedRooms: List<JoinedRoom>
@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
Expand All @@ -47,7 +47,7 @@ class LoginUserCachedDataSourceTest {
@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
userCachedDataSource = UserCachedDataSource(userRemoteDataSource, mockAccessTokenContainer)
userCachedDataSource = UserCachedDataSource(userRemoteDataSource, mockUserPreferenceManager)
}

@Test
Expand Down Expand Up @@ -98,7 +98,7 @@ class LoginUserCachedDataSourceTest {
fun accessTokenTest() {
tryLogin()

assertThat(mockAccessTokenContainer.accessToken, `is`(mockLoginUser.accessToken))
assertThat(mockUserPreferenceManager.getAccessToken(), `is`(mockLoginUser.accessToken))
}

private fun tryLogin() {
Expand Down

0 comments on commit 121e082

Please sign in to comment.