Skip to content

Commit ed113a8

Browse files
committed
refactor: move on result action into FolderPickerPreference
1 parent 7925458 commit ed113a8

2 files changed

Lines changed: 10 additions & 5 deletions

File tree

app/src/main/java/com/osfans/trime/ui/components/FolderPickerPreference.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import androidx.appcompat.app.AlertDialog
1414
import androidx.preference.Preference
1515
import com.osfans.trime.R
1616
import com.osfans.trime.databinding.FolderPickerDialogBinding
17+
import com.osfans.trime.util.getFileFromUri
1718
import com.osfans.trime.util.getUriForFile
1819
import java.io.File
1920

@@ -25,11 +26,12 @@ class FolderPickerPreference
2526
defStyleAttr: Int = androidx.preference.R.attr.preferenceStyle,
2627
) : Preference(context, attrs, defStyleAttr) {
2728
private var value = ""
28-
lateinit var documentTreeLauncher: ActivityResultLauncher<Uri?>
29-
lateinit var dialogView: FolderPickerDialogBinding
29+
private lateinit var dialogView: FolderPickerDialogBinding
3030

3131
var default = ""
3232

33+
var documentTreeLauncher: ActivityResultLauncher<Uri?>? = null
34+
3335
init {
3436
context.theme.obtainStyledAttributes(attrs, R.styleable.FolderPickerPreferenceAttrs, 0, 0).run {
3537
try {
@@ -68,7 +70,7 @@ class FolderPickerPreference
6870
dialogView = FolderPickerDialogBinding.inflate(LayoutInflater.from(context))
6971
dialogView.editText.setText(initValue)
7072
dialogView.button.setOnClickListener {
71-
documentTreeLauncher.launch(context.getUriForFile(File(initValue)))
73+
documentTreeLauncher?.launch(context.getUriForFile(File(initValue)))
7274
}
7375
AlertDialog
7476
.Builder(context)
@@ -89,4 +91,8 @@ class FolderPickerPreference
8991
notifyChanged()
9092
}
9193
}
94+
95+
fun onResult(result: Uri) {
96+
dialogView.editText.setText(context.getFileFromUri(result)?.absolutePath)
97+
}
9298
}

app/src/main/java/com/osfans/trime/ui/fragments/ProfileFragment.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import com.osfans.trime.ui.main.MainViewModel
3131
import com.osfans.trime.util.ResourceUtils
3232
import com.osfans.trime.util.appContext
3333
import com.osfans.trime.util.formatDateTime
34-
import com.osfans.trime.util.getFileFromUri
3534
import com.osfans.trime.util.rimeActionWithResultDialog
3635
import com.osfans.trime.util.toast
3736
import com.osfans.trime.util.withLoadingDialog
@@ -55,7 +54,7 @@ class ProfileFragment :
5554
it,
5655
DocumentsContract.getTreeDocumentId(it),
5756
)
58-
dialogView.editText.setText(context.getFileFromUri(uri)?.absolutePath)
57+
onResult(uri)
5958
}
6059
}
6160

0 commit comments

Comments
 (0)