Skip to content

Commit cec987a

Browse files
committed
Show snackbar upon crop save destination dir change
1 parent def966e commit cec987a

File tree

4 files changed

+51
-24
lines changed

4 files changed

+51
-24
lines changed

app/src/main/java/com/autocrop/activities/examination/ExaminationActivityViewModel.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import androidx.lifecycle.ViewModel
66
import com.autocrop.collections.CropBundle
77
import com.autocrop.utils.BlankFun
88
import com.autocrop.utils.Consumable
9+
import com.autocrop.utilsandroid.documentUriPathIdentifier
910
import com.autocrop.utilsandroid.externalPicturesDir
1011
import timber.log.Timber
1112

@@ -59,7 +60,7 @@ class ExaminationActivityViewModel(private val validSaveDirDocumentUri: Uri?)
5960

6061
fun cropWriteDirIdentifier(): String =
6162
validSaveDirDocumentUri?.let {
62-
it.pathSegments[1]
63+
documentUriPathIdentifier(it)
6364
} ?: externalPicturesDir.path
6465

6566
/**

app/src/main/java/com/autocrop/activities/main/fragments/flowfield/FlowFieldFragment.kt

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import android.content.Context
77
import android.content.Intent
88
import android.net.Uri
99
import android.os.Bundle
10+
import android.text.SpannableStringBuilder
1011
import android.view.View
1112
import androidx.activity.result.contract.ActivityResultContracts
13+
import androidx.core.text.color
1214
import com.autocrop.activities.IntentExtraIdentifier
1315
import com.autocrop.activities.cropping.CroppingActivity
1416
import com.autocrop.activities.main.fragments.MainActivityFragment
@@ -36,31 +38,20 @@ class FlowFieldFragment:
3638
private fun setMenuInflationButtonOnClickListener() =
3739
binding.menuInflationButton.setOnClickListener { popupMenu.show() }
3840

39-
private val popupMenu: FlowFieldFragmentMenu by lazy {
41+
private val popupMenu by lazy {
4042
FlowFieldFragmentMenu(
4143
mapOf(
42-
R.id.main_menu_item_change_save_destination_dir to { pickSaveDestinationDirContract.launch(CropFileSaveDestinationPreferences.treeUri) },
44+
R.id.main_menu_item_change_save_destination_dir to ::pickCropSaveDestinationDir,
4345
R.id.main_menu_item_rate_the_app to ::goToPlayStoreListing,
44-
R.id.main_menu_item_about_the_app to { castedActivity.replaceCurrentFragmentWith(AboutFragment(), false) }
46+
R.id.main_menu_item_about_the_app to ::invokeAboutFragment
4547
),
4648
requireView().context,
4749
binding.menuInflationButton
4850
)
4951
}
5052

51-
private fun goToPlayStoreListing() =
52-
try{
53-
startActivity(
54-
Intent(Intent.ACTION_VIEW).apply {
55-
data = Uri.parse("https://play.google.com/store/apps/details?id=${requireContext().packageName}")
56-
setPackage("com.android.vending")
57-
}
58-
)
59-
} catch (e: ActivityNotFoundException){
60-
requireActivity()
61-
.snacky("Seems like you're not signed into the Play Store, pal \uD83E\uDD14")
62-
.show()
63-
}
53+
private fun pickCropSaveDestinationDir() =
54+
pickSaveDestinationDirContract.launch(CropFileSaveDestinationPreferences.treeUri)
6455

6556
private val pickSaveDestinationDirContract = registerForActivityResult(
6657
object: ActivityResultContracts.OpenDocumentTree(){
@@ -75,14 +66,46 @@ class FlowFieldFragment:
7566
}
7667
) {
7768
it?.let { treeUri ->
78-
requireActivity().applicationContext.contentResolver.takePersistableUriPermission(
79-
treeUri,
80-
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
81-
)
82-
CropFileSaveDestinationPreferences.treeUri = treeUri
69+
if (CropFileSaveDestinationPreferences.treeUri != treeUri){
70+
CropFileSaveDestinationPreferences.treeUri = treeUri
71+
72+
with(requireActivity()){
73+
applicationContext.contentResolver.takePersistableUriPermission(
74+
treeUri,
75+
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
76+
)
77+
snacky(
78+
SpannableStringBuilder()
79+
.append("Crops will be saved to ")
80+
.color(getColorInt(NotificationColor.SUCCESS, requireContext())){
81+
append(
82+
documentUriPathIdentifier(CropFileSaveDestinationPreferences.documentUri!!)
83+
)
84+
}
85+
)
86+
.show()
87+
}
88+
}
8389
}
8490
}
8591

92+
private fun invokeAboutFragment() =
93+
castedActivity.replaceCurrentFragmentWith(AboutFragment(), false)
94+
95+
private fun goToPlayStoreListing() =
96+
try{
97+
startActivity(
98+
Intent(Intent.ACTION_VIEW).apply {
99+
data = Uri.parse("https://play.google.com/store/apps/details?id=${requireContext().packageName}")
100+
setPackage("com.android.vending")
101+
}
102+
)
103+
} catch (e: ActivityNotFoundException){
104+
requireActivity()
105+
.snacky("Seems like you're not signed into the Play Store, pal \uD83E\uDD14")
106+
.show()
107+
}
108+
86109
//$$$$$$$$$$$$$$$$$$
87110
// Image Selection $
88111
//$$$$$$$$$$$$$$$$$$

app/src/main/java/com/autocrop/global/UserPreferences.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ object CropFileSaveDestinationPreferences: UserPreferences<Uri?>(sortedMapOf(Key
8484
override fun put(key: String?, value: Uri?): Uri? {
8585
if (key == Keys.TREE_URI && value != getOrDefault(Keys.TREE_URI, null))
8686
documentUri = buildDocumentUriFromTreeUri(value!!)
87-
.also { Timber.i("Set documentUri $it") }
87+
.also { Timber.i("Set new documentUri: $it") }
8888
return super.put(key, value)
8989
}
9090

app/src/main/java/com/autocrop/utilsandroid/Uri.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,7 @@ fun Context.uriPermissionGranted(uri: Uri, permissionCode: Int): Boolean =
2121
) == PackageManager.PERMISSION_GRANTED
2222

2323
fun buildDocumentUriFromTreeUri(treeUri: Uri): Uri =
24-
DocumentsContract.buildDocumentUriUsingTree(treeUri, DocumentsContract.getTreeDocumentId(treeUri))
24+
DocumentsContract.buildDocumentUriUsingTree(treeUri, DocumentsContract.getTreeDocumentId(treeUri))
25+
26+
fun documentUriPathIdentifier(documentUri: Uri): String =
27+
documentUri.pathSegments[1]

0 commit comments

Comments
 (0)