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
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package dev.arkbuilders.arkmemo.ui.activities

import android.content.ClipboardManager
import android.content.Context
import android.content.Context.CLIPBOARD_SERVICE
import android.content.Intent
import android.os.Bundle
import android.view.Menu
Expand All @@ -18,10 +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.SettingsFragment
import dev.arkbuilders.arkmemo.ui.fragments.NotesFragment
import dev.arkbuilders.arkmemo.utils.replaceFragment
import dev.arkbuilders.arkmemo.utils.resumeFragment
import dev.arkbuilders.arkmemo.ui.fragments.SettingsFragment
import javax.inject.Inject

@AndroidEntryPoint
Expand Down Expand Up @@ -130,8 +131,33 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) {
fun showProgressBar(show: Boolean) {
binding.progressBar.isVisible = show
}

companion object{
private const val CURRENT_FRAGMENT_TAG = "current fragment tag"
}
}

fun AppCompatActivity.replaceFragment(fragment: Fragment, tag: String) {
supportFragmentManager.beginTransaction().apply {
val backStackName = fragment.javaClass.name
val popBackStack = supportFragmentManager.popBackStackImmediate(backStackName, 0)
if (!popBackStack) {
replace(R.id.container, fragment, tag)
addToBackStack(backStackName)
} else {
show(fragment)
}
commit()
}
}

fun AppCompatActivity.resumeFragment(fragment: Fragment){
supportFragmentManager.beginTransaction().apply{
show(fragment)
commit()
}
}

fun Context.getTextFromClipBoard(): String?{
val clipboardManager = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
return clipboardManager.primaryClip?.getItemAt(0)?.text?.toString()
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,49 @@
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.Bundle
import android.os.Environment
import android.widget.Toast
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.preferences.MemoPreferences
import javax.inject.Inject

@AndroidEntryPoint
class FilePickerDialog: ArkFilePickerFragment() {

@Inject lateinit var memoPreferences: MemoPreferences

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (storageNotAvailable()) { isCancelable = false }
}

override fun dismiss() {
super.dismiss()
if (storageNotAvailable()) { activity?.finish() }
}

private fun storageNotAvailable(): Boolean = memoPreferences.getPath().isEmpty()

class FilePickerDialog private constructor(){
companion object{

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

fun show() {
ArkFilePickerFragment.newInstance(getFilePickerConfig()).show(fragmentManager!!, TAG)
newInstance(getFilePickerConfig()).show(fragmentManager, TAG)
}

fun show(activity: AppCompatActivity, fragmentManager: FragmentManager){
Expand All @@ -36,6 +54,10 @@ class FilePickerDialog private constructor(){
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 All @@ -55,10 +77,6 @@ class FilePickerDialog 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