Skip to content
This repository has been archived by the owner on Oct 20, 2024. It is now read-only.

Commit

Permalink
πŸ“› λ‹‰λ„€μž„ μˆ˜μ • κΈ°λŠ₯ (#70)
Browse files Browse the repository at this point in the history
* [feat] Add Feature Update Nickname

* [fix] Fix Fcm Token Save
  • Loading branch information
moondev03 authored Aug 11, 2024
1 parent f8dc6ec commit aa98c2d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,17 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {
}
}

fun getRegistrationToken(): String? = try {
FirebaseMessaging.getInstance().token.result
} catch (e: Exception) {
LoggerUtils.w("Fetching FCM Registration Token failed")
null
}
fun getRegistrationToken(callback: (String?) -> Unit) {
FirebaseMessaging.getInstance().token
.addOnCompleteListener { task ->
if (task.isSuccessful) {
val token = task.result
callback(token)
} else {
LoggerUtils.w("Fetching FCM Registration Token failed\n${task.exception}")
callback(null)
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ class HomeViewModel @Inject constructor(
private val _homeState = MutableLiveData<UiState<HomeInfo>>(UiState.Loading)
val homeState: LiveData<UiState<HomeInfo>> get() = _homeState

private val _fcmState = MutableLiveData<UiState<Unit>>(UiState.Loading)
val fcmState: LiveData<UiState<Unit>> get() = _fcmState

private val _addState = MutableLiveData<UiState<Int>>(UiState.Loading)
val addState: LiveData<UiState<Int>> get() = _addState

Expand All @@ -58,25 +55,26 @@ class HomeViewModel @Inject constructor(
}

fun registerFcmToken() {
_fcmState.value = UiState.Loading

viewModelScope.launch {
try {
val accessToken = dataStoreRepository.getAccessToken().getOrNull().orEmpty()
val fcmToken = MyFirebaseMessagingService().getRegistrationToken().orEmpty()

registerFcmTokenUseCase(accessToken, fcmToken)
.onSuccess {
_fcmState.value = UiState.Success(Unit)
}
.onFailure { e ->
LoggerUtils.e("Sign-in failed: ${e.message}")
_fcmState.value = UiState.Failure(message = e.message.toString())
try {
MyFirebaseMessagingService().getRegistrationToken { token ->
if (token != null) {
viewModelScope.launch {
val accessToken = dataStoreRepository.getAccessToken().getOrNull().orEmpty()

registerFcmTokenUseCase(accessToken, token)
.onSuccess {
LoggerUtils.d("Register fcm success: $token")
}
.onFailure { e ->
LoggerUtils.e("Register fcm failed: ${e.message}")
}
}
} catch (e: Exception) {
LoggerUtils.e("Sign-in exception: ${e.message}")
_fcmState.value = UiState.Failure(message = e.message.toString())
} else {
throw Exception("Null Fcm Token Exception")
}
}
} catch (e: Exception) {
LoggerUtils.e("Register fcm exception: ${e.message}")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import com.nabi.nabi.base.NabiApplication.Companion.consecutiveDay
import com.nabi.nabi.base.NabiApplication.Companion.nickname
import com.nabi.nabi.custom.CustomDialog
import com.nabi.nabi.databinding.FragmentMypageBinding
import com.nabi.nabi.views.MainActivity
import com.nabi.nabi.views.home.HomeFragment
import com.nabi.nabi.views.sign.SignInNicknameFragment

class MyPageFragment : BaseFragment<FragmentMypageBinding>(R.layout.fragment_mypage) {

Expand All @@ -22,7 +25,7 @@ class MyPageFragment : BaseFragment<FragmentMypageBinding>(R.layout.fragment_myp
}

binding.btnChangeNickname.setOnClickListener {

(requireActivity() as MainActivity).replaceFragment(SignInNicknameFragment(), true)
}

binding.btnLoadDiary.setOnClickListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import dagger.hilt.android.AndroidEntryPoint
import java.util.regex.Pattern

@AndroidEntryPoint
class SignInNicknameFragment: BaseFragment<FragmentSignNicknameBinding>(R.layout.fragment_sign_nickname) {
class SignInNicknameFragment(
private val isSignUp: Boolean = true
): BaseFragment<FragmentSignNicknameBinding>(R.layout.fragment_sign_nickname) {
private val viewModel: SignNicknameViewModel by viewModels()

override fun initView() {
Expand Down Expand Up @@ -78,9 +80,13 @@ class SignInNicknameFragment: BaseFragment<FragmentSignNicknameBinding>(R.layout
is UiState.Success -> {
nickname = binding.etNick.text.toString()

val intent = Intent(requireContext(), MainActivity::class.java)
startActivity(intent)
requireActivity().finish()
if(isSignUp){
val intent = Intent(requireContext(), MainActivity::class.java)
startActivity(intent)
requireActivity().finish()
} else {
requireActivity().supportFragmentManager.popBackStack()
}
}
}
}
Expand Down

0 comments on commit aa98c2d

Please sign in to comment.