diff --git a/app/src/main/java/com/dongyang/android/youdongknowme/data/local/SharedPreference.kt b/app/src/main/java/com/dongyang/android/youdongknowme/data/local/SharedPreference.kt index 89e23e48..3f72ffb7 100644 --- a/app/src/main/java/com/dongyang/android/youdongknowme/data/local/SharedPreference.kt +++ b/app/src/main/java/com/dongyang/android/youdongknowme/data/local/SharedPreference.kt @@ -37,16 +37,24 @@ object SharedPreference { // 최초 접속 여부 private const val FIRST_LAUNCH = "FIRST_LAUNCH" fun getIsFirstLaunch(): Boolean = pref?.getBoolean(FIRST_LAUNCH, true) ?: true - fun setIsFirstLaunch(isFirstLaunch: Boolean) = pref?.edit()?.putBoolean(FIRST_LAUNCH, isFirstLaunch)?.apply() + fun setIsFirstLaunch(isFirstLaunch: Boolean) = + pref?.edit()?.putBoolean(FIRST_LAUNCH, isFirstLaunch)?.apply() // 학교 알림 설정 여부 private const val ACCESS_SCHOOL_ALARM = "ACCESS_SCHOOL_ALARM" fun getIsAccessSchoolAlarm(): Boolean = pref?.getBoolean(ACCESS_SCHOOL_ALARM, true) ?: true - fun setIsAccessSchoolAlarm(isAccessSchoolAlarm: Boolean) = pref?.edit()?.putBoolean(ACCESS_SCHOOL_ALARM, isAccessSchoolAlarm)?.apply() + fun setIsAccessSchoolAlarm(isAccessSchoolAlarm: Boolean) = + pref?.edit()?.putBoolean(ACCESS_SCHOOL_ALARM, isAccessSchoolAlarm)?.apply() // 학과 알림 설정 여부 private const val ACCESS_DEPART_ALARM = "ACCESS_DEPART_ALARM" fun getIsAccessDepartAlarm(): Boolean = pref?.getBoolean(ACCESS_DEPART_ALARM, true) ?: true - fun setIsAccessDepartAlarm(isAccessDepartAlarm: Boolean) = pref?.edit()?.putBoolean(ACCESS_DEPART_ALARM, isAccessDepartAlarm)?.apply() + fun setIsAccessDepartAlarm(isAccessDepartAlarm: Boolean) = + pref?.edit()?.putBoolean(ACCESS_DEPART_ALARM, isAccessDepartAlarm)?.apply() + + // FCM 토큰 + private const val FCM_TOKEN = "NO_TOKEN" + fun getFCMToken(): String = pref?.getString(FCM_TOKEN, "0") ?: "0" + fun setFcmToken(token: String) = pref?.edit()?.putString(FCM_TOKEN, token)?.apply() } \ No newline at end of file diff --git a/app/src/main/java/com/dongyang/android/youdongknowme/data/local/dao/KeywordDao.kt b/app/src/main/java/com/dongyang/android/youdongknowme/data/local/dao/KeywordDao.kt index fe8710d5..b9c3ea28 100644 --- a/app/src/main/java/com/dongyang/android/youdongknowme/data/local/dao/KeywordDao.kt +++ b/app/src/main/java/com/dongyang/android/youdongknowme/data/local/dao/KeywordDao.kt @@ -23,4 +23,7 @@ interface KeywordDao { @Query("DELETE FROM keyword WHERE name = :name") suspend fun deleteKeyword(name: String) + + @Query("SELECT * FROM keyword WHERE isSubscribe = 1") + suspend fun getSubscribedKeywords(): List } \ No newline at end of file diff --git a/app/src/main/java/com/dongyang/android/youdongknowme/data/remote/entity/Setting.kt b/app/src/main/java/com/dongyang/android/youdongknowme/data/remote/entity/Setting.kt new file mode 100644 index 00000000..d8dc559c --- /dev/null +++ b/app/src/main/java/com/dongyang/android/youdongknowme/data/remote/entity/Setting.kt @@ -0,0 +1,15 @@ +package com.dongyang.android.youdongknowme.data.remote.entity + +data class UpdateDepartment( + val token: String, + val department: String, +) + +data class RemoveToken( + val token: String, +) + +data class UpdateTopic( + val token: String, + val topics: List, +) \ No newline at end of file diff --git a/app/src/main/java/com/dongyang/android/youdongknowme/data/remote/service/SettingService.kt b/app/src/main/java/com/dongyang/android/youdongknowme/data/remote/service/SettingService.kt new file mode 100644 index 00000000..22aec639 --- /dev/null +++ b/app/src/main/java/com/dongyang/android/youdongknowme/data/remote/service/SettingService.kt @@ -0,0 +1,22 @@ +package com.dongyang.android.youdongknowme.data.remote.service + +import com.dongyang.android.youdongknowme.data.remote.entity.RemoveToken +import com.dongyang.android.youdongknowme.data.remote.entity.UpdateDepartment +import com.dongyang.android.youdongknowme.data.remote.entity.UpdateTopic +import retrofit2.http.Body +import retrofit2.http.POST + +interface SettingService { + + @POST("department/v1/dmu/updateDepartment") + suspend fun updateDepartment(@Body data: UpdateDepartment) + + @POST("department/v1/dmu/deleteDepartment") + suspend fun deleteDepartment(@Body token: RemoveToken) + + @POST("token/v1/dmu/updateTopic") + suspend fun updateTopic(@Body data: UpdateTopic) + + @POST("token/v1/dmu/deleteTopic") + suspend fun deleteTopic(@Body token: RemoveToken) +} \ No newline at end of file diff --git a/app/src/main/java/com/dongyang/android/youdongknowme/data/repository/SettingRepository.kt b/app/src/main/java/com/dongyang/android/youdongknowme/data/repository/SettingRepository.kt index 54315c51..8281ee08 100644 --- a/app/src/main/java/com/dongyang/android/youdongknowme/data/repository/SettingRepository.kt +++ b/app/src/main/java/com/dongyang/android/youdongknowme/data/repository/SettingRepository.kt @@ -1,10 +1,21 @@ package com.dongyang.android.youdongknowme.data.repository import com.dongyang.android.youdongknowme.data.local.SharedPreference +import com.dongyang.android.youdongknowme.data.local.dao.KeywordDao +import com.dongyang.android.youdongknowme.data.remote.entity.RemoveToken +import com.dongyang.android.youdongknowme.data.remote.entity.UpdateDepartment +import com.dongyang.android.youdongknowme.data.remote.entity.UpdateTopic +import com.dongyang.android.youdongknowme.data.remote.service.SettingService +import com.dongyang.android.youdongknowme.standard.network.ErrorResponseHandler +import com.dongyang.android.youdongknowme.standard.network.NetworkResult +import com.dongyang.android.youdongknowme.standard.network.RetrofitObject -class SettingRepository { +class SettingRepository( + private val keywordDao: KeywordDao, + private val errorResponseHandler: ErrorResponseHandler +) { - fun getIsAccessSchoolAlarm(): Boolean = SharedPreference.getIsAccessSchoolAlarm() + fun getIsAccessUniversityAlarm(): Boolean = SharedPreference.getIsAccessSchoolAlarm() fun getIsAccessDepartAlarm(): Boolean = SharedPreference.getIsAccessDepartAlarm() @@ -19,4 +30,65 @@ class SettingRepository { fun getUserDepartment(): String { return SharedPreference.getDepartment() } + + suspend fun getUserTopic(): List { + val subscribedTopic = keywordDao.getSubscribedKeywords() + return subscribedTopic.map { it.englishName } + } + + fun getUserFCMToken(): String { + return SharedPreference.getFCMToken() + } + + suspend fun updateUserDepartment( + data: UpdateDepartment + ): NetworkResult { + return try { + val response = RetrofitObject.getNetwork().create(SettingService::class.java) + .updateDepartment(data) + NetworkResult.Success(response) + } catch (exception: Exception) { + val error = errorResponseHandler.getError(exception) + NetworkResult.Error(error) + } + } + + suspend fun removeUserDepartment( + token: RemoveToken + ): NetworkResult { + return try { + val response = RetrofitObject.getNetwork().create(SettingService::class.java) + .deleteDepartment(token) + NetworkResult.Success(response) + } catch (exception: Exception) { + val error = errorResponseHandler.getError(exception) + NetworkResult.Error(error) + } + } + + suspend fun updateUserTopic( + data: UpdateTopic + ): NetworkResult { + return try { + val response = RetrofitObject.getNetwork().create(SettingService::class.java) + .updateTopic(data) + NetworkResult.Success(response) + } catch (exception: Exception) { + val error = errorResponseHandler.getError(exception) + NetworkResult.Error(error) + } + } + + suspend fun removeUserTopic( + token: RemoveToken + ): NetworkResult { + return try { + val response = RetrofitObject.getNetwork().create(SettingService::class.java) + .deleteTopic(token) + NetworkResult.Success(response) + } catch (exception: Exception) { + val error = errorResponseHandler.getError(exception) + NetworkResult.Error(error) + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/dongyang/android/youdongknowme/service/FCMService.kt b/app/src/main/java/com/dongyang/android/youdongknowme/service/FCMService.kt index 0bb10a83..d87e19bc 100644 --- a/app/src/main/java/com/dongyang/android/youdongknowme/service/FCMService.kt +++ b/app/src/main/java/com/dongyang/android/youdongknowme/service/FCMService.kt @@ -8,6 +8,7 @@ import android.content.Intent import android.media.RingtoneManager import android.os.Build import androidx.core.app.NotificationCompat +import com.dongyang.android.youdongknowme.data.local.SharedPreference import com.dongyang.android.youdongknowme.ui.view.main.MainActivity import com.google.firebase.messaging.FirebaseMessagingService import com.google.firebase.messaging.RemoteMessage @@ -59,4 +60,9 @@ class FCMService : FirebaseMessagingService() { // 알림 표시 notificationManager.notify(System.currentTimeMillis().toInt(), builder.build()) } + + override fun onNewToken(token: String) { + super.onNewToken(token) + SharedPreference.setFcmToken(token) + } } \ No newline at end of file diff --git a/app/src/main/java/com/dongyang/android/youdongknowme/standard/di/KoinModules.kt b/app/src/main/java/com/dongyang/android/youdongknowme/standard/di/KoinModules.kt index 1f28c5c0..97164703 100644 --- a/app/src/main/java/com/dongyang/android/youdongknowme/standard/di/KoinModules.kt +++ b/app/src/main/java/com/dongyang/android/youdongknowme/standard/di/KoinModules.kt @@ -117,7 +117,7 @@ val repositoryModule = module { KeywordRepository(get()) } single { - SettingRepository() + SettingRepository(get(), get()) } single { AlarmRepository(get()) diff --git a/app/src/main/java/com/dongyang/android/youdongknowme/standard/network/RetrofitObject.kt b/app/src/main/java/com/dongyang/android/youdongknowme/standard/network/RetrofitObject.kt index ecf3bbd4..72d38e3a 100644 --- a/app/src/main/java/com/dongyang/android/youdongknowme/standard/network/RetrofitObject.kt +++ b/app/src/main/java/com/dongyang/android/youdongknowme/standard/network/RetrofitObject.kt @@ -9,7 +9,7 @@ import retrofit2.converter.gson.GsonConverterFactory import java.util.concurrent.TimeUnit object RetrofitObject { - private const val TIME_OUT_COUNT: Long = 10 + private const val TIME_OUT_COUNT: Long = 30 fun getNetwork(): Retrofit { val baseInterceptor = Interceptor { chain -> diff --git a/app/src/main/java/com/dongyang/android/youdongknowme/ui/view/depart/DepartActivity.kt b/app/src/main/java/com/dongyang/android/youdongknowme/ui/view/depart/DepartActivity.kt index a36b066f..62c03183 100644 --- a/app/src/main/java/com/dongyang/android/youdongknowme/ui/view/depart/DepartActivity.kt +++ b/app/src/main/java/com/dongyang/android/youdongknowme/ui/view/depart/DepartActivity.kt @@ -64,9 +64,6 @@ class DepartActivity : BaseActivity(), D private fun getDepart(items: ArrayList) { return binding.btnDepartComplete.setOnClickListener { viewModel.setDepartment(items[viewModel.selectDepartPosition.value ?: 0]) - val intent = Intent(this, MainActivity::class.java) - intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) - startActivity(intent) finish() } } diff --git a/app/src/main/java/com/dongyang/android/youdongknowme/ui/view/keyword/KeywordActivity.kt b/app/src/main/java/com/dongyang/android/youdongknowme/ui/view/keyword/KeywordActivity.kt index e0db4e26..44f631c4 100644 --- a/app/src/main/java/com/dongyang/android/youdongknowme/ui/view/keyword/KeywordActivity.kt +++ b/app/src/main/java/com/dongyang/android/youdongknowme/ui/view/keyword/KeywordActivity.kt @@ -1,12 +1,10 @@ package com.dongyang.android.youdongknowme.ui.view.keyword -import android.content.Intent import androidx.lifecycle.Observer import com.dongyang.android.youdongknowme.R import com.dongyang.android.youdongknowme.data.local.entity.KeywordEntity import com.dongyang.android.youdongknowme.databinding.ActivityKeywordBinding import com.dongyang.android.youdongknowme.standard.base.BaseActivity -import com.dongyang.android.youdongknowme.ui.view.permission.OnboardingPermissionActivity import com.google.android.material.chip.Chip import com.google.android.material.chip.ChipGroup import org.koin.androidx.viewmodel.ext.android.viewModel @@ -17,7 +15,7 @@ class KeywordActivity : BaseActivity() override val viewModel: KeywordViewModel by viewModel() override fun initStartView() = Unit - + override fun initDataBinding() { // 효율을 위해 단 한번만 옵저빙하여 이미 구독중인 항목을 선택 처리 viewModel.localKeywordList.observe(this, object : Observer> { @@ -40,14 +38,9 @@ class KeywordActivity : BaseActivity() viewModel.checkFirstLaunch() viewModel.getLocalKeywordList() - // TODO :: 안드로이드 데이터베이스에 유저별 설정한 키워드 저장 및 파이어베이스 키워드 구독 설정 binding.btnKeywordComplete.setOnClickListener { viewModel.subscribeCheckedKeyword() - if (viewModel.isFirstLaunch.value == true) { - viewModel.setFirstLaunch(false) - val intent = Intent(this@KeywordActivity, OnboardingPermissionActivity::class.java) - startActivity(intent) - } + setResult(RESULT_OK) finish() } diff --git a/app/src/main/java/com/dongyang/android/youdongknowme/ui/view/main/MainActivity.kt b/app/src/main/java/com/dongyang/android/youdongknowme/ui/view/main/MainActivity.kt index d53b7b45..f57227a5 100644 --- a/app/src/main/java/com/dongyang/android/youdongknowme/ui/view/main/MainActivity.kt +++ b/app/src/main/java/com/dongyang/android/youdongknowme/ui/view/main/MainActivity.kt @@ -6,13 +6,13 @@ import androidx.navigation.fragment.NavHostFragment import androidx.navigation.plusAssign import androidx.navigation.ui.setupWithNavController import com.dongyang.android.youdongknowme.R +import com.dongyang.android.youdongknowme.data.local.SharedPreference import com.dongyang.android.youdongknowme.databinding.ActivityMainBinding import com.dongyang.android.youdongknowme.standard.base.BaseActivity import com.dongyang.android.youdongknowme.ui.view.util.KeepStateNavigator import com.google.android.gms.tasks.OnCompleteListener import com.google.firebase.messaging.FirebaseMessaging import org.koin.androidx.viewmodel.ext.android.viewModel -import timber.log.Timber /* 메인 액티비티 */ class MainActivity : BaseActivity() { @@ -38,9 +38,9 @@ class MainActivity : BaseActivity() { getFcmToken() } - override fun initDataBinding() {} + override fun initDataBinding() = Unit - override fun initAfterBinding() {} + override fun initAfterBinding() = Unit private fun getFcmToken() { FirebaseMessaging.getInstance().token.addOnCompleteListener(OnCompleteListener { task -> @@ -49,9 +49,7 @@ class MainActivity : BaseActivity() { } val token = task.result - Timber.d(token) - //TODO: prefs 선언 중복 제거될 경우 주석 제거 - //prefs.edit().putString("fcm_token", token).commit() + SharedPreference.setFcmToken(token) }) } diff --git a/app/src/main/java/com/dongyang/android/youdongknowme/ui/view/permission/OnboardingPermissionActivity.kt b/app/src/main/java/com/dongyang/android/youdongknowme/ui/view/permission/OnboardingPermissionActivity.kt index 6d6af0a3..c016c32c 100644 --- a/app/src/main/java/com/dongyang/android/youdongknowme/ui/view/permission/OnboardingPermissionActivity.kt +++ b/app/src/main/java/com/dongyang/android/youdongknowme/ui/view/permission/OnboardingPermissionActivity.kt @@ -75,7 +75,7 @@ class OnboardingPermissionActivity : binding.mvSwitchPermission.strokeColor = getColor(resources) binding.switchPermission.isChecked = isChecked - viewModel.setIsAccessSchoolAlarm(isChecked) + viewModel.setIsAccessUniversityAlarm(isChecked) viewModel.setIsAccessDepartAlarm(isChecked) } } \ No newline at end of file diff --git a/app/src/main/java/com/dongyang/android/youdongknowme/ui/view/setting/SettingFragment.kt b/app/src/main/java/com/dongyang/android/youdongknowme/ui/view/setting/SettingFragment.kt index 616dbcd4..8bec3cef 100644 --- a/app/src/main/java/com/dongyang/android/youdongknowme/ui/view/setting/SettingFragment.kt +++ b/app/src/main/java/com/dongyang/android/youdongknowme/ui/view/setting/SettingFragment.kt @@ -1,7 +1,10 @@ package com.dongyang.android.youdongknowme.ui.view.setting +import android.app.Activity import android.content.Intent import android.net.Uri +import androidx.activity.result.ActivityResultLauncher +import androidx.activity.result.contract.ActivityResultContracts import com.dongyang.android.youdongknowme.R import com.dongyang.android.youdongknowme.databinding.FragmentSettingBinding import com.dongyang.android.youdongknowme.standard.base.BaseFragment @@ -16,13 +19,22 @@ class SettingFragment : BaseFragment() override val layoutResourceId: Int = R.layout.fragment_setting override val viewModel: SettingViewModel by viewModel() + private lateinit var resultLauncher: ActivityResultLauncher + private var topics: List = emptyList() + override fun initStartView() { binding.tvSettingAppVersion.text = getAppVersion() + setResultKeyword() } override fun initDataBinding() { - viewModel.myDepartment.observe(viewLifecycleOwner) { department -> - binding.tvSettingDepartment.text = department + + viewModel.myDepartment.observe(viewLifecycleOwner) { myDepartment -> + binding.tvSettingDepartment.text = myDepartment + } + + viewModel.myTopics.observe(viewLifecycleOwner) { myTopics -> + topics = topics } viewModel.isAccessUniversityAlarm.observe(viewLifecycleOwner) { isChecked -> @@ -35,28 +47,30 @@ class SettingFragment : BaseFragment() } override fun initAfterBinding() { + viewModel.checkAccessAlarm() viewModel.getUserDepartment() + viewModel.getUserTopic() binding.switchSettingUniversityAlarm.setOnCheckedChangeListener { compoundButton, _ -> if (compoundButton.isChecked) { - viewModel.setIsAccessSchoolAlarm(true) + viewModel.updateUserTopic(topics) } else { - viewModel.setIsAccessSchoolAlarm(false) + viewModel.removeUserTopic() } } binding.switchSettingDepartmentAlarm.setOnCheckedChangeListener { compoundButton, _ -> if (compoundButton.isChecked) { - viewModel.setIsAccessDepartAlarm(true) + viewModel.updateUserDepartment() } else { - viewModel.setIsAccessDepartAlarm(false) + viewModel.removeUserDepartment() } } binding.btnSettingEditKeyword.setOnClickListener { val intent = Intent(requireActivity(), KeywordActivity::class.java) - startActivity(intent) + resultLauncher.launch(intent) } binding.btnSettingEditDepartment.setOnClickListener { @@ -92,4 +106,14 @@ class SettingFragment : BaseFragment() requireContext().packageManager.getPackageInfo(requireContext().packageName, 0) return packageManager.versionName } + + private fun setResultKeyword() { + resultLauncher = + registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> + if (result.resultCode == Activity.RESULT_OK) { + viewModel.getUserTopic() + viewModel.updateUserTopic(topics) + } + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/dongyang/android/youdongknowme/ui/view/setting/SettingViewModel.kt b/app/src/main/java/com/dongyang/android/youdongknowme/ui/view/setting/SettingViewModel.kt index 28c401e2..29d51831 100644 --- a/app/src/main/java/com/dongyang/android/youdongknowme/ui/view/setting/SettingViewModel.kt +++ b/app/src/main/java/com/dongyang/android/youdongknowme/ui/view/setting/SettingViewModel.kt @@ -2,12 +2,28 @@ package com.dongyang.android.youdongknowme.ui.view.setting import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.viewModelScope +import com.dongyang.android.youdongknowme.data.remote.entity.RemoveToken +import com.dongyang.android.youdongknowme.data.remote.entity.UpdateDepartment +import com.dongyang.android.youdongknowme.data.remote.entity.UpdateTopic import com.dongyang.android.youdongknowme.data.repository.SettingRepository import com.dongyang.android.youdongknowme.standard.base.BaseViewModel +import com.dongyang.android.youdongknowme.standard.network.NetworkResult +import com.dongyang.android.youdongknowme.ui.view.util.Event +import kotlinx.coroutines.launch /* 설정 뷰모델 */ class SettingViewModel(private val settingRepository: SettingRepository) : BaseViewModel() { + private val _errorState: MutableLiveData> = MutableLiveData() + val errorState: LiveData> = _errorState + + private val _isLoading: MutableLiveData = MutableLiveData() + val isLoading: LiveData = _isLoading + + private val _isError: MutableLiveData = MutableLiveData() + val isError: LiveData = _isError + private val _isAccessUniversityAlarm: MutableLiveData = MutableLiveData(false) val isAccessUniversityAlarm: LiveData get() = _isAccessUniversityAlarm @@ -17,15 +33,25 @@ class SettingViewModel(private val settingRepository: SettingRepository) : BaseV private val _myDepartment: MutableLiveData = MutableLiveData() val myDepartment: LiveData get() = _myDepartment + private val _myTopics: MutableLiveData> = MutableLiveData() + val myTopics: LiveData> get() = _myTopics + + private val _FCMToken: MutableLiveData = MutableLiveData() + val FCMToken: LiveData get() = _FCMToken + + init { + getUserFCMToken() + } + fun checkAccessAlarm() { - val isAccessSchoolAlarm = settingRepository.getIsAccessSchoolAlarm() - _isAccessUniversityAlarm.postValue(isAccessSchoolAlarm) + val isAccessUniversityAlarm = settingRepository.getIsAccessUniversityAlarm() + _isAccessUniversityAlarm.postValue(isAccessUniversityAlarm) val isAccessDepartAlarm = settingRepository.getIsAccessDepartAlarm() _isAccessDepartAlarm.postValue(isAccessDepartAlarm) } - fun setIsAccessSchoolAlarm(isAccessSchoolAlarm: Boolean) { + fun setIsAccessUniversityAlarm(isAccessSchoolAlarm: Boolean) { settingRepository.setIsAccessSchoolAlarm(isAccessSchoolAlarm) } @@ -37,4 +63,113 @@ class SettingViewModel(private val settingRepository: SettingRepository) : BaseV val myDepartment = settingRepository.getUserDepartment() _myDepartment.postValue(myDepartment) } + + fun getUserTopic() { + viewModelScope.launch { + val keyword = settingRepository.getUserTopic() + _myTopics.value = keyword + } + } + + private fun getUserFCMToken() { + val token = settingRepository.getUserFCMToken() + _FCMToken.postValue(token) + } + + fun updateUserDepartment() { + _isLoading.postValue(true) + + viewModelScope.launch { + when (val result = settingRepository.updateUserDepartment( + UpdateDepartment( + token = FCMToken.value.toString(), + department = myDepartment.value.toString() + ) + )) { + is NetworkResult.Success -> { + settingRepository.setIsAccessDepartAlarm(true) + _isLoading.postValue(false) + _isError.postValue(false) + } + + is NetworkResult.Error -> { + handleError(result, _errorState) + _isLoading.postValue(false) + _isError.postValue(true) + } + } + } + } + + fun removeUserDepartment() { + _isLoading.postValue(true) + + viewModelScope.launch { + when (val result = + settingRepository.removeUserDepartment( + RemoveToken(token = FCMToken.value.toString()) + ) + ) { + is NetworkResult.Success -> { + settingRepository.setIsAccessDepartAlarm(false) + _isLoading.postValue(false) + _isError.postValue(false) + } + + is NetworkResult.Error -> { + handleError(result, _errorState) + _isLoading.postValue(false) + _isError.postValue(true) + } + } + } + } + + fun updateUserTopic(topic: List) { + _isLoading.postValue(true) + + viewModelScope.launch { + when (val result = settingRepository.updateUserTopic( + UpdateTopic( + token = FCMToken.value.toString(), + topics = topic + ) + )) { + is NetworkResult.Success -> { + settingRepository.setIsAccessSchoolAlarm(true) + _isLoading.postValue(false) + _isError.postValue(false) + } + + is NetworkResult.Error -> { + handleError(result, _errorState) + _isLoading.postValue(false) + _isError.postValue(true) + } + } + } + } + + fun removeUserTopic() { + _isLoading.postValue(true) + + viewModelScope.launch { + when (val result = + settingRepository.removeUserTopic( + RemoveToken(token = FCMToken.value.toString()) + )) { + is NetworkResult.Success -> { + settingRepository.setIsAccessSchoolAlarm(false) + _isLoading.postValue(false) + _isError.postValue(false) + } + + is NetworkResult.Error -> { + handleError(result, _errorState) + _isLoading.postValue(false) + _isError.postValue(true) + } + } + } + } } \ No newline at end of file