Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AN] feat: 설정 페이지 권한 확인 로직 구현 #500

Merged
merged 11 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import android.content.Intent
import android.net.Uri
import androidx.activity.viewModels
import com.google.android.material.snackbar.Snackbar
import com.happy.friendogly.R
import com.happy.friendogly.application.di.AppModule
import com.happy.friendogly.databinding.ActivitySettingBinding
Expand All @@ -14,26 +15,74 @@ import com.happy.friendogly.presentation.dialog.DefaultCoralAlertDialog
import com.happy.friendogly.presentation.dialog.DefaultRedAlertDialog
import com.happy.friendogly.presentation.dialog.LoadingDialog
import com.happy.friendogly.presentation.ui.MainActivity
import com.happy.friendogly.presentation.ui.permission.AlarmPermission
import com.happy.friendogly.presentation.ui.register.RegisterActivity

class SettingActivity : BaseActivity<ActivitySettingBinding>(R.layout.activity_setting) {
val viewModel: SettingViewModel by viewModels {
SettingViewModel.factory(
getWoofAlarmUseCase = AppModule.getInstance().getWoofAlarmUseCase,
getChatAlarmUseCase = AppModule.getInstance().getChatAlarmUseCase,
saveChatAlarmUseCase = AppModule.getInstance().saveChatAlarmUseCase,
saveWoofAlarmUseCase = AppModule.getInstance().saveWoofAlarmUseCase,
deleteTokenUseCase = AppModule.getInstance().deleteTokenUseCase,
)
}

private val alarmPermission: AlarmPermission =
AlarmPermission.from(this) { isPermitted ->
if (!isPermitted) {
Snackbar.make(
binding.root,
getString(R.string.chat_setting_alarm_alert),
Snackbar.LENGTH_SHORT,
).show()
with(binding) {
alarmSettingsChattingPushSwitch.isChecked = false
alarmSettingsWoofPushSwitch.isChecked = false
}
}
}

private val loadingDialog: LoadingDialog by lazy { LoadingDialog(this) }

override fun initCreateView() {
initDataBinding()
initObserve()
setSwitchCheckListener()
}

private fun initDataBinding() {
binding.vm = viewModel
viewModel.uiState.observe(this) { uiState ->
with(binding) {
alarmSettingsChattingPushSwitch.isChecked = uiState.chattingAlarmPushPermitted && alarmPermission.hasPermissions()
alarmSettingsWoofPushSwitch.isChecked = uiState.woofAlarmPushPermitted && alarmPermission.hasPermissions()
}
}
}

private fun setSwitchCheckListener() {
with(binding) {
alarmSettingsChattingPushSwitch.setOnCheckedChangeListener { _, isChecked ->
if (isChecked) {
requestNotificationPermission()
}
viewModel.saveWoofAlarmSetting(isChecked)
}
alarmSettingsWoofPushSwitch.setOnCheckedChangeListener { _, isChecked ->
if (isChecked) {
requestNotificationPermission()
}
viewModel.saveWoofAlarmSetting(isChecked)
}
}
}

private fun requestNotificationPermission() {
if (AlarmPermission.isValidPermissionSDK() && !alarmPermission.hasPermissions()) {
alarmPermission.createAlarmDialog().show(supportFragmentManager, "TAG")
}
}

private fun initObserve() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.happy.friendogly.presentation.ui.setting

data class SettingUiState(
val totalAlarmPushPermitted: Boolean = false,
val chattingAlarmPushPermitted: Boolean = false,
val clubAlarmPushPermitted: Boolean = false,
val woofAlarmPushPermitted: Boolean = false,
val totalAlarmPushPermitted: Boolean = true,
val chattingAlarmPushPermitted: Boolean = true,
val clubAlarmPushPermitted: Boolean = true,
val woofAlarmPushPermitted: Boolean = true,
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import androidx.lifecycle.ViewModelProvider
import com.happy.friendogly.domain.error.DataError
import com.happy.friendogly.domain.fold
import com.happy.friendogly.domain.usecase.DeleteTokenUseCase
import com.happy.friendogly.domain.usecase.GetChatAlarmUseCase
import com.happy.friendogly.domain.usecase.GetWoofAlarmUseCase
import com.happy.friendogly.domain.usecase.SaveChatAlarmUseCase
import com.happy.friendogly.domain.usecase.SaveWoofAlarmUseCase
import com.happy.friendogly.presentation.base.BaseViewModel
Expand All @@ -15,6 +17,8 @@ import com.happy.friendogly.presentation.base.emit

class SettingViewModel(
private val deleteTokenUseCase: DeleteTokenUseCase,
private val getChatAlarmUseCase: GetChatAlarmUseCase,
private val getWoofAlarmUseCase: GetWoofAlarmUseCase,
private val saveChatAlarmUseCase: SaveChatAlarmUseCase,
private val saveWoofAlarmUseCase: SaveWoofAlarmUseCase,
) : BaseViewModel() {
Expand All @@ -32,17 +36,41 @@ class SettingViewModel(
private val _loading: MutableLiveData<Event<Boolean>> = MutableLiveData(null)
val loading: LiveData<Event<Boolean>> get() = _loading

fun onTotalAlarmToggled(checked: Boolean) {}
init {
setAlarmSetting()
}

fun onChattingAlarmToggled(checked: Boolean) {
private fun setAlarmSetting() {
launch {
saveChatAlarmUseCase(checked)
getChatAlarmUseCase().onSuccess {
_uiState.value =
_uiState.value?.copy(
chattingAlarmPushPermitted = it,
)
}.onFailure {
// TODO 에러핸들링
}
}

launch {
getWoofAlarmUseCase().onSuccess {
_uiState.value =
_uiState.value?.copy(
woofAlarmPushPermitted = it,
)
}.onFailure {
// TODO 에러핸들링
}
}
}

fun onClubAlarmToggled(checked: Boolean) {}
fun saveChattingAlarmSetting(checked: Boolean) {
launch {
saveChatAlarmUseCase(checked)
}
}

fun onWoofAlarmToggled(checked: Boolean) {
fun saveWoofAlarmSetting(checked: Boolean) {
launch {
saveWoofAlarmUseCase(checked)
}
Expand Down Expand Up @@ -108,12 +136,16 @@ class SettingViewModel(

companion object {
fun factory(
getChatAlarmUseCase: GetChatAlarmUseCase,
getWoofAlarmUseCase: GetWoofAlarmUseCase,
saveChatAlarmUseCase: SaveChatAlarmUseCase,
saveWoofAlarmUseCase: SaveWoofAlarmUseCase,
deleteTokenUseCase: DeleteTokenUseCase,
): ViewModelProvider.Factory {
return BaseViewModelFactory { _ ->
SettingViewModel(
getChatAlarmUseCase = getChatAlarmUseCase,
getWoofAlarmUseCase = getWoofAlarmUseCase,
saveChatAlarmUseCase = saveChatAlarmUseCase,
saveWoofAlarmUseCase = saveWoofAlarmUseCase,
deleteTokenUseCase = deleteTokenUseCase,
Expand Down
24 changes: 11 additions & 13 deletions android/app/src/main/res/layout/activity_setting.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,28 @@
android:background="@color/white">

<com.google.android.material.appbar.MaterialToolbar
android:layout_width="match_parent"
android:id="@+id/container_header"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:contentInsetStart="0dp"
android:background="@color/transparent"
app:contentInsetStart="0dp"
app:layout_constraintTop_toTopOf="parent">

<TextView
android:id="@+id/tv_habit_create_header"
style="@style/Theme.AppCompat.TextView.SemiBold.Black.Size18"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/setting"
android:layout_gravity="center" />
android:layout_gravity="center"
android:text="@string/setting" />

<ImageView
android:id="@+id/iv_close"
onSingleClick="@{() -> vm.navigateToBack()}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:layout_gravity="start"
android:padding="16dp"
app:srcCompat="@drawable/ic_arrow_left" />

</com.google.android.material.appbar.MaterialToolbar>
Expand All @@ -63,6 +63,7 @@
android:layout_marginStart="16dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="16dp"
android:visibility="gone"
app:description="@{String.format(@string/alarm_settings_total_push_description)}"
app:layout_constraintEnd_toStartOf="@id/alarm_settings_total_push_switch"
app:layout_constraintStart_toStartOf="parent"
Expand All @@ -76,8 +77,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:checked="@{vm.uiState.totalAlarmPushPermitted}"
android:onCheckedChanged="@{(view, checked) -> vm.onTotalAlarmToggled(checked)}"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="@id/alarm_settings_total_push"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/alarm_settings_total_push"
Expand Down Expand Up @@ -108,8 +108,6 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:checked="@{vm.uiState.chattingAlarmPushPermitted}"
android:onCheckedChanged="@{(view, checked) -> vm.onChattingAlarmToggled(checked)}"
app:layout_constraintBottom_toBottomOf="@id/alarm_settings_chatting_push"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/alarm_settings_chatting_push"
Expand All @@ -127,6 +125,8 @@
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"

android:visibility="gone"
app:description="@{String.format(@string/alarm_settings_club_push_description)}"
app:layout_constraintEnd_toStartOf="@id/alarm_settings_club_push_switch"
app:layout_constraintHorizontal_bias="0.0"
Expand All @@ -140,8 +140,8 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:checked="@{vm.uiState.clubAlarmPushPermitted}"
android:onCheckedChanged="@{(view, checked) -> vm.onClubAlarmToggled(checked)}"
android:visibility="gone"

app:layout_constraintBottom_toBottomOf="@id/alarm_settings_club_push"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/alarm_settings_club_push"
Expand Down Expand Up @@ -171,8 +171,6 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:checked="@{vm.uiState.woofAlarmPushPermitted}"
android:onCheckedChanged="@{(view, checked) -> vm.onWoofAlarmToggled(checked)}"
app:layout_constraintBottom_toBottomOf="@id/alarm_settings_woof_push"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/alarm_settings_woof_push"
Expand Down
Loading