Skip to content

Commit

Permalink
Handle Record Audio permission more gracefully: #66
Browse files Browse the repository at this point in the history
  • Loading branch information
tuancoltech committed Jul 23, 2024
1 parent cac528d commit 2de4877
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.arkbuilders.arkmemo.ui.activities

import android.Manifest
import android.content.Intent
import android.os.Bundle
import android.view.Menu
Expand Down Expand Up @@ -42,12 +41,6 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) {

var fragment: Fragment = NotesFragment()

private var shouldRecord = false
private val audioRecordingPermissionLauncher =
registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->
shouldRecord = isGranted
}

init {
FilePickerDialog.readPermLauncher =
registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->
Expand All @@ -70,7 +63,6 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) {
binding.toolbar.setNavigationOnClickListener {
onBackPressedDispatcher.onBackPressed()
}
audioRecordingPermissionLauncher.launch(Manifest.permission.RECORD_AUDIO)

fun showFragment() {
val textDataFromIntent = intent?.getStringExtra(Intent.EXTRA_TEXT)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dev.arkbuilders.arkmemo.ui.fragments

import android.Manifest
import android.content.pm.PackageManager
import android.content.res.ColorStateList
import android.os.Bundle
import android.text.Editable
Expand Down Expand Up @@ -33,6 +32,7 @@ import dev.arkbuilders.arkmemo.ui.viewmodels.RecorderState
import dev.arkbuilders.arkmemo.ui.viewmodels.ArkRecorderViewModel
import dev.arkbuilders.arkmemo.ui.views.WaveView
import dev.arkbuilders.arkmemo.ui.views.toast
import dev.arkbuilders.arkmemo.utils.Permission
import dev.arkbuilders.arkmemo.utils.gone
import dev.arkbuilders.arkmemo.utils.millisToString
import dev.arkbuilders.arkmemo.utils.observeSaveResult
Expand All @@ -52,7 +52,12 @@ class ArkRecorderFragment: BaseEditNoteFragment() {
private val audioRecordingPermissionLauncher =
registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->
shouldRecord = isGranted
if (!shouldRecord) activity.onBackPressedDispatcher.onBackPressed()
if (!shouldRecord) {
activity.onBackPressedDispatcher.onBackPressed()
} else {
observeDataStates()
onRecordButtonClick()
}
}

private val arkRecorderViewModel: ArkRecorderViewModel by viewModels()
Expand All @@ -72,24 +77,28 @@ class ArkRecorderFragment: BaseEditNoteFragment() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
shouldRecord = context?.let {
it.checkSelfPermission(Manifest.permission.RECORD_AUDIO) ==
PackageManager.PERMISSION_GRANTED
} ?: false
shouldRecord = Permission.hasPermission(activity, Manifest.permission.RECORD_AUDIO)
if (shouldRecord) {
notesViewModel.init {}
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.CREATED) {
arkRecorderViewModel.collect(
stateToUI = {
showState(it)
},
handleSideEffect = { handleSideEffect(it) }
)
}
observeDataStates()
}
}

private fun observeRecordingState() {
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.CREATED) {
arkRecorderViewModel.collect(
stateToUI = {
showState(it)
},
handleSideEffect = { handleSideEffect(it) }
)
}
observeSaveResult(notesViewModel.getSaveNoteResultLiveData())
} else audioRecordingPermissionLauncher.launch(Manifest.permission.RECORD_AUDIO)
}
}

private fun observeDataStates() {
observeRecordingState()
observeSaveResult(notesViewModel.getSaveNoteResultLiveData())
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -151,23 +160,12 @@ class ArkRecorderFragment: BaseEditNoteFragment() {
etTitle.addTextChangedListener(etTitleWatcher)

ivRecord.setOnClickListener {
if (!arkRecorderViewModel.isRecording()
&& (arkRecorderViewModel.isRecordExisting() ||
File(getCurrentRecordingPath()).length() > 0L)) {

CommonActionDialog(title = R.string.dialog_replace_recording_title,
message = R.string.dialog_replace_recording_message,
positiveText = R.string.dialog_replace_recording_positive_text,
negativeText = R.string.discard,
onPositiveClick = {
binding.layoutAudioView.root.gone()
arkRecorderViewModel.onStartStopClick() },

onNegativeClicked = { }
).show(parentFragmentManager, CommonActionDialog.TAG)
} else {
arkRecorderViewModel.onStartStopClick()
if (!Permission.hasPermission(activity, Manifest.permission.RECORD_AUDIO)) {
audioRecordingPermissionLauncher.launch(Manifest.permission.RECORD_AUDIO)
return@setOnClickListener
}

onRecordButtonClick()
}

ivPauseResume.setOnClickListener {
Expand Down Expand Up @@ -425,6 +423,26 @@ class ArkRecorderFragment: BaseEditNoteFragment() {
ViewCompat.setOnApplyWindowInsetsListener(view, null)
}

private fun onRecordButtonClick() {
if (!arkRecorderViewModel.isRecording()
&& (arkRecorderViewModel.isRecordExisting() ||
File(getCurrentRecordingPath()).length() > 0L)) {

CommonActionDialog(title = R.string.dialog_replace_recording_title,
message = R.string.dialog_replace_recording_message,
positiveText = R.string.dialog_replace_recording_positive_text,
negativeText = R.string.discard,
onPositiveClick = {
binding.layoutAudioView.root.gone()
arkRecorderViewModel.onStartStopClick() },

onNegativeClicked = { }
).show(parentFragmentManager, CommonActionDialog.TAG)
} else {
arkRecorderViewModel.onStartStopClick()
}
}

companion object {

const val TAG = "voice-notes-fragment"
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/dev/arkbuilders/arkmemo/utils/Permission.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package dev.arkbuilders.arkmemo.utils

import android.content.Context
import android.content.pm.PackageManager

object Permission {
fun hasPermission(context: Context, permission: String): Boolean {
return context.checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED
}
}

0 comments on commit 2de4877

Please sign in to comment.