diff --git a/app/src/main/java/dev/arkbuilders/arkmemo/ui/activities/MainActivity.kt b/app/src/main/java/dev/arkbuilders/arkmemo/ui/activities/MainActivity.kt index 750f66c1..64313611 100644 --- a/app/src/main/java/dev/arkbuilders/arkmemo/ui/activities/MainActivity.kt +++ b/app/src/main/java/dev/arkbuilders/arkmemo/ui/activities/MainActivity.kt @@ -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 @@ -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 @@ -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() +} diff --git a/app/src/main/java/dev/arkbuilders/arkmemo/ui/dialogs/FilePickerDialog.kt b/app/src/main/java/dev/arkbuilders/arkmemo/ui/dialogs/FilePickerDialog.kt index 3f3e752e..c723bef7 100644 --- a/app/src/main/java/dev/arkbuilders/arkmemo/ui/dialogs/FilePickerDialog.kt +++ b/app/src/main/java/dev/arkbuilders/arkmemo/ui/dialogs/FilePickerDialog.kt @@ -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? = null var readPermLauncher_SDK_R: ActivityResultLauncher? = null fun show() { - ArkFilePickerFragment.newInstance(getFilePickerConfig()).show(fragmentManager!!, TAG) + newInstance(getFilePickerConfig()).show(fragmentManager, TAG) } fun show(activity: AppCompatActivity, fragmentManager: FragmentManager){ @@ -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() @@ -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,