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

#21: The app becomes unusable if user closes folder selection dialog #32

Merged
merged 10 commits into from
Jan 11, 2024

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import dev.arkbuilders.arkfilepicker.presentation.onArkPathPicked
import dev.arkbuilders.arkmemo.R
import dev.arkbuilders.arkmemo.contracts.PermissionContract
import dev.arkbuilders.arkmemo.databinding.ActivityMainBinding
import dev.arkbuilders.arkmemo.files.FilePicker
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.SettingsFragment
Expand All @@ -41,15 +41,15 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) {
var fragment: Fragment = TextNotesFragment()

init {
FilePicker.readPermLauncher =
FilePickerDialog.readPermLauncher =
registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->
if (isGranted) FilePicker.show()
if (isGranted) FilePickerDialog.show()
else finish()
}

FilePicker.readPermLauncher_SDK_R =
FilePickerDialog.readPermLauncher_SDK_R =
registerForActivityResult(PermissionContract()) { isGranted ->
if (isGranted) FilePicker.show()
if (isGranted) FilePickerDialog.show()
else finish()
}
}
Expand Down Expand Up @@ -85,11 +85,10 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) {
}
}
}

}

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

supportFragmentManager.onArkPathPicked(this) {
memoPreferences.storePath(it.toString())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package dev.arkbuilders.arkmemo.files
package dev.arkbuilders.arkmemo.ui.dialogs

import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import android.os.Environment
import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
Expand All @@ -15,21 +13,33 @@ import dev.arkbuilders.arkfilepicker.presentation.filepicker.ArkFilePickerFragme
import dev.arkbuilders.arkfilepicker.presentation.filepicker.ArkFilePickerMode
import dev.arkbuilders.arkmemo.BuildConfig
import dev.arkbuilders.arkmemo.R
import dev.arkbuilders.arkmemo.ui.activities.MainActivity

class FilePickerDialog: ArkFilePickerFragment() {

override fun dismiss() {
super.dismiss()
val activity = (requireActivity() as MainActivity)
if (activity.memoPreferences.getPathString() == null) activity.finish()
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming FilePickerDialog might probably be used in other activity in the future, rather than MainActivity, this casting will cause a crash.
What do you think about below?

Suggested change
val activity = (requireActivity() as MainActivity)
if (activity.memoPreferences.getPathString() == null) activity.finish()
}
val activity = (activity as? MainActivity)
if (activity?.memoPreferences?.getPathString() == null) activity?.finish()
}


class FilePicker private constructor(){
companion object{

private const val TAG = "file_picker"
private var fragmentManager: FragmentManager? = null
var readPermLauncher: ActivityResultLauncher<String>? = null
var readPermLauncher_SDK_R: ActivityResultLauncher<String>? = null

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

fun show() {
ArkFilePickerFragment.newInstance(getFilePickerConfig()).show(fragmentManager!!, TAG)
newInstance(getFilePickerConfig()).show(fragmentManager!!, TAG)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
newInstance(getFilePickerConfig()).show(fragmentManager!!, TAG)
fragmentManager?.let { newInstance(getFilePickerConfig()).show(it, TAG) }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think fragmentManager can not really be null here. We can just make it non-nullable

}

fun show(activity: AppCompatActivity, fragmentManager: FragmentManager){
this.fragmentManager = fragmentManager
Companion.fragmentManager = fragmentManager
if(isReadPermissionGranted(activity)){
show()
}
Expand All @@ -55,10 +65,6 @@ class FilePicker private constructor(){
}
}

fun permissionDeniedError(context: Context){
Toast.makeText(context, context.getString(R.string.no_file_access), Toast.LENGTH_SHORT).show()
}

private fun getFilePickerConfig() = ArkFilePickerConfig(
mode = ArkFilePickerMode.FOLDER,
titleStringId = R.string.file_picker_title,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import androidx.preference.PreferenceFragmentCompat
import dagger.hilt.android.AndroidEntryPoint
import dev.arkbuilders.arkfilepicker.presentation.onArkPathPicked
import dev.arkbuilders.arkmemo.R
import dev.arkbuilders.arkmemo.files.FilePicker
import dev.arkbuilders.arkmemo.ui.dialogs.FilePickerDialog
import dev.arkbuilders.arkmemo.preferences.MemoPreferences
import dev.arkbuilders.arkmemo.ui.activities.MainActivity
import dev.arkbuilders.arkmemo.ui.views.PathPreference
Expand Down Expand Up @@ -40,7 +40,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
activity.supportActionBar?.setDisplayHomeAsUpEnabled(true)

pathPref?.setOnPreferenceClickListener {
FilePicker.show(activity, parentFragmentManager)
FilePickerDialog.show(activity, parentFragmentManager)
true
}

Expand Down