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

Fix : 설정 내 알람 스위치 상태 동기화 처리 #240

Merged
merged 5 commits into from
Apr 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 @@ -11,7 +11,6 @@ import com.dongyang.android.youdongknowme.standard.base.BaseActivity
import com.dongyang.android.youdongknowme.ui.view.util.KeepStateNavigator
import com.google.firebase.messaging.FirebaseMessaging
import org.koin.androidx.viewmodel.ext.android.viewModel
import timber.log.Timber

/* 메인 액티비티 */
class MainActivity : BaseActivity<ActivityMainBinding, MainViewModel>() {
Expand Down Expand Up @@ -46,11 +45,10 @@ class MainActivity : BaseActivity<ActivityMainBinding, MainViewModel>() {
private fun getFcmToken() {
viewModel.setIsFirstLaunch(false)
FirebaseMessaging.getInstance().token.addOnCompleteListener { task ->
val token = task.result

if (task.isSuccessful) {
viewModel.setFCMToken(task.result).run { viewModel.setInitToken() }
Timber.d("token ${task.result}")
} else {
return@addOnCompleteListener
viewModel.setFCMToken(token).run { viewModel.setInitToken() }
}
}
}
Expand All @@ -61,4 +59,4 @@ class MainActivity : BaseActivity<ActivityMainBinding, MainViewModel>() {
return Intent(context, MainActivity::class.java)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,20 @@ import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import com.dongyang.android.youdongknowme.databinding.DialogPermissionBinding

class PermissionDialog(val title: String, val content: String, val pacakageName: String) : DialogFragment() {
class PermissionDialog(
val title: String,
val content: String,
val pacakageName: String,
val cancelListener: (() -> Unit)? = null,
) : DialogFragment() {
private var _binding: DialogPermissionBinding? = null
private val binding get() = _binding!!

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
): View {
_binding = DialogPermissionBinding.inflate(inflater, container, false)
val view = binding.root

Expand All @@ -29,8 +38,10 @@ class PermissionDialog(val title: String, val content: String, val pacakageName:

// 취소 버튼
binding.customTvBtn1.setOnClickListener {
cancelListener?.invoke()
dismiss()
}

// 확인 버튼
binding.customTvBtn2.setOnClickListener {
val intent =
Expand All @@ -46,4 +57,4 @@ class PermissionDialog(val title: String, val content: String, val pacakageName:
super.onDestroyView()
_binding = null
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
package com.dongyang.android.youdongknowme.ui.view.setting

import android.app.Activity
import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.content.res.ColorStateList
import android.net.Uri
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import android.os.Build
import android.view.View
import android.widget.Switch
import androidx.appcompat.widget.SwitchCompat
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
import androidx.core.content.ContextCompat
import com.dongyang.android.youdongknowme.R
import com.dongyang.android.youdongknowme.databinding.FragmentSettingBinding
Expand All @@ -27,29 +21,21 @@ class SettingFragment : BaseFragment<FragmentSettingBinding, SettingViewModel>()
override val layoutResourceId: Int = R.layout.fragment_setting
override val viewModel: SettingViewModel by viewModel()

private lateinit var resultLauncherKeyword: ActivityResultLauncher<Intent>
private lateinit var resultResultDepartment: ActivityResultLauncher<Intent>

private var topics: List<String> = emptyList()
private var department: String = ""
private val departmentActivityResultLauncher =
registerForActivityResult(StartActivityForResult()) { _ ->
// 학과 선택 화면 이동 후 재진입 시 학과 재조회 처리
viewModel.getUserDepartment()
}

override fun initStartView() {
binding.tvSettingAppVersion.text = getAppVersion()
setResultKeyword()
setResultDepartment()
initAlarmSwitchStateByNotificationPermission()
}

override fun initDataBinding() {

viewModel.myDepartment.observe(viewLifecycleOwner) { myDepartment ->
binding.tvSettingDepartment.text = myDepartment
department = myDepartment
viewModel.updateUserDepartment(department)
}

viewModel.myTopics.observe(viewLifecycleOwner) { myTopics ->
topics = myTopics
viewModel.updateUserTopic(topics)
}

viewModel.isAccessUniversityAlarm.observe(viewLifecycleOwner) { isChecked ->
Expand All @@ -59,44 +45,60 @@ class SettingFragment : BaseFragment<FragmentSettingBinding, SettingViewModel>()
viewModel.isAccessDepartAlarm.observe(viewLifecycleOwner) { isChecked ->
binding.switchSettingDepartmentAlarm.isChecked = isChecked
}

viewModel.isLoading.observe(viewLifecycleOwner) { isLoading ->
if (isLoading)
showLoading()
else
dismissLoading()
}
}

override fun initAfterBinding() {
/** TODO 스위치 클릭을 억제하기 위한 임시 뷰, 후에 삭제 필요 */
binding.viewSettingUniversityAlarm.setOnClickListener {
if (viewModel.isAccessUniversityAlarm.value == false) {
if (checkNotificationPermission().not()) {
showPermissionDialog()
return@setOnClickListener
}

viewModel.checkAccessAlarm()
viewModel.getUserDepartment()
viewModel.getUserTopic()
if (viewModel.myDepartment.value.isNullOrBlank())
return@setOnClickListener

binding.switchSettingUniversityAlarm.setOnCheckedChangeListener { compoundButton, _ ->
checkPermission(binding.switchSettingUniversityAlarm)
if (compoundButton.isChecked) {
if (topics.isNotEmpty()) {
viewModel.updateUserTopic(topics)
}
} else {
viewModel.removeUserTopic()
viewModel.updateUserTopic()
return@setOnClickListener
}

viewModel.removeUserTopic()
}

binding.switchSettingDepartmentAlarm.setOnCheckedChangeListener { compoundButton, _ ->
checkPermission(binding.switchSettingDepartmentAlarm)
if (compoundButton.isChecked) {
if (department.isNotEmpty()) {
viewModel.updateUserDepartment(department)
/** TODO 스위치 클릭을 억제하기 위한 임시 뷰, 후에 삭제 필요 */
binding.viewSettingDepartmentAlarm.setOnClickListener {
if (viewModel.isAccessDepartAlarm.value == false) {
if (checkNotificationPermission().not()) {
showPermissionDialog()
return@setOnClickListener
}
} else {
viewModel.removeUserDepartment()

if (viewModel.myDepartment.value.isNullOrBlank())
return@setOnClickListener

viewModel.updateUserDepartment()
return@setOnClickListener
}

viewModel.removeUserDepartment()
}

binding.btnSettingEditKeyword.setOnClickListener {
val intent = Intent(requireActivity(), KeywordActivity::class.java)
resultLauncherKeyword.launch(intent)
startActivity(intent)
}

binding.btnSettingEditDepartment.setOnClickListener {
val intent = Intent(requireActivity(), DepartActivity::class.java)
resultResultDepartment.launch(intent)
departmentActivityResultLauncher.launch(intent)
}

binding.btnSettingAppHelp.setOnClickListener {
Expand All @@ -121,51 +123,44 @@ class SettingFragment : BaseFragment<FragmentSettingBinding, SettingViewModel>()
}
}

private fun checkPermission(switch: SwitchCompat){
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
if (PackageManager.PERMISSION_DENIED == ContextCompat.checkSelfPermission(
requireContext(), Manifest.permission.POST_NOTIFICATIONS
)
) {
// 알림 권한 설정 미허용
viewModel.setIsAccessDepartAlarm(false)
viewModel.setIsAccessUniversityAlarm(false)
binding.switchSettingUniversityAlarm.isChecked = false
binding.switchSettingDepartmentAlarm.isChecked = false

val dialog = PermissionDialog(getString(R.string.dialog_permission_title), getString(R.string.dialog_permission_content), requireContext().packageName)
dialog.show(parentFragmentManager, "CustomDialog")
} else {
switch.isChecked = !switch.isChecked
}
} else {
switch.isChecked = !switch.isChecked
}
}

private fun getAppVersion(): String {
val packageManager =
requireContext().packageManager.getPackageInfo(requireContext().packageName, 0)
return packageManager.versionName
}

private fun setResultKeyword() {
resultLauncherKeyword =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
viewModel.getUserTopic().run { viewModel.updateUserTopic(topics) }
binding.switchSettingUniversityAlarm.isChecked = true
}
}
private fun initAlarmSwitchStateByNotificationPermission() {
if (!checkNotificationPermission()) {
viewModel.setIsAccessDepartAlarm(false)
viewModel.setIsAccessUniversityAlarm(false)
}
}

private fun setResultDepartment() {
resultResultDepartment =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
viewModel.getUserDepartment()
binding.switchSettingDepartmentAlarm.isChecked = true
}
}
private fun showPermissionDialog() {
val dialog = PermissionDialog(
title = getString(R.string.dialog_permission_title),
content = getString(R.string.dialog_permission_content),
pacakageName = requireContext().packageName,
cancelListener = {
viewModel.setIsAccessDepartAlarm(false)
viewModel.setIsAccessUniversityAlarm(false)
})

dialog.show(parentFragmentManager, null)
}

private fun checkNotificationPermission(): Boolean {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU)
return true

if (PackageManager.PERMISSION_DENIED == ContextCompat.checkSelfPermission(
requireContext(),
Manifest.permission.POST_NOTIFICATIONS
)
) {
return false
}

return true
}
}
}
Loading
Loading