Skip to content

Commit

Permalink
Close dialog on touch outside
Browse files Browse the repository at this point in the history
  • Loading branch information
shubertm committed Jan 8, 2024
1 parent e273527 commit 3908b60
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import dev.arkbuilders.arkmemo.databinding.ActivityMainBinding
import dev.arkbuilders.arkmemo.ui.dialogs.FilePickerDialog
import dev.arkbuilders.arkmemo.preferences.MemoPreferences
import dev.arkbuilders.arkmemo.ui.fragments.EditTextNotesFragment
import dev.arkbuilders.arkmemo.ui.fragments.NotesFragment
import dev.arkbuilders.arkmemo.ui.fragments.SettingsFragment
import dev.arkbuilders.arkmemo.ui.fragments.TextNotesFragment
import javax.inject.Inject

@AndroidEntryPoint
Expand All @@ -38,7 +38,7 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) {

private var menu: Menu? = null

var fragment: Fragment = TextNotesFragment()
var fragment: Fragment = NotesFragment()

init {
FilePickerDialog.readPermLauncher =
Expand Down Expand Up @@ -73,7 +73,7 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) {
} else {
if (savedInstanceState == null)
supportFragmentManager.beginTransaction().apply {
add(fragContainer, fragment, TextNotesFragment.TAG)
add(fragContainer, fragment, NotesFragment.TAG)
commit()
}
else {
Expand All @@ -87,7 +87,7 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) {
}
}

if (memoPreferences.getPathString() == null) {
if (memoPreferences.getPath().isEmpty()) {
FilePickerDialog.show(this, supportFragmentManager)

supportFragmentManager.onArkPathPicked(this) {
Expand All @@ -101,7 +101,7 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) {
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.main_menu, menu)
this.menu = menu
if(fragment.tag != TextNotesFragment.TAG)
if(fragment.tag != NotesFragment.TAG)
showSettingsButton(false)
return true
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,42 @@
package dev.arkbuilders.arkmemo.ui.dialogs

import android.Manifest
import android.content.DialogInterface
import android.content.pm.PackageManager
import android.os.Build
import android.os.Environment
import androidx.activity.result.ActivityResultLauncher
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentManager
import dagger.hilt.android.AndroidEntryPoint
import dev.arkbuilders.arkfilepicker.ArkFilePickerConfig
import dev.arkbuilders.arkfilepicker.presentation.filepicker.ArkFilePickerFragment
import dev.arkbuilders.arkfilepicker.presentation.filepicker.ArkFilePickerMode
import dev.arkbuilders.arkmemo.BuildConfig
import dev.arkbuilders.arkmemo.R
import dev.arkbuilders.arkmemo.ui.activities.MainActivity
import dev.arkbuilders.arkmemo.preferences.MemoPreferences
import javax.inject.Inject

@AndroidEntryPoint
class FilePickerDialog: ArkFilePickerFragment() {

@Inject lateinit var memoPreferences: MemoPreferences

override fun dismiss() {
super.dismiss()
val activity = (activity as? MainActivity)
if (activity?.memoPreferences?.getPathString() == null) activity?.finish()
checkStorage()
}

override fun onCancel(dialog: DialogInterface) {
super.onCancel(dialog)
checkStorage()
}

private fun checkStorage() {
if (memoPreferences.getPath().isEmpty()) {
activity?.finish()
}
}

companion object{
Expand All @@ -30,10 +46,6 @@ class FilePickerDialog: ArkFilePickerFragment() {
var readPermLauncher: ActivityResultLauncher<String>? = null
var readPermLauncher_SDK_R: ActivityResultLauncher<String>? = null

fun newInstance(config: ArkFilePickerConfig) = FilePickerDialog().apply {
setConfig(config)
}

fun show() {
newInstance(getFilePickerConfig()).show(fragmentManager, TAG)
}
Expand All @@ -46,6 +58,10 @@ class FilePickerDialog: ArkFilePickerFragment() {
else askForReadPermissions()
}

private fun newInstance(config: ArkFilePickerConfig) = FilePickerDialog().apply {
setConfig(config)
}

private fun isReadPermissionGranted(activity: AppCompatActivity): Boolean{
return if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.R)
Environment.isExternalStorageManager()
Expand Down

0 comments on commit 3908b60

Please sign in to comment.