Skip to content

Commit

Permalink
Merge pull request #240 from TeamDMU/fix/setting-switch-state
Browse files Browse the repository at this point in the history
Fix : ์„ค์ • ๋‚ด ์•Œ๋žŒ ์Šค์œ„์น˜ ์ƒํƒœ ๋™๊ธฐํ™” ์ฒ˜๋ฆฌ
  • Loading branch information
hoyahozz authored Apr 21, 2024
2 parents 7221218 + 674c8b4 commit 3edb9fc
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 127 deletions.
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

0 comments on commit 3edb9fc

Please sign in to comment.