Skip to content

Commit

Permalink
refactor: move on result action into FolderPickerPreference
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Oct 9, 2024
1 parent 7925458 commit ed113a8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import androidx.appcompat.app.AlertDialog
import androidx.preference.Preference
import com.osfans.trime.R
import com.osfans.trime.databinding.FolderPickerDialogBinding
import com.osfans.trime.util.getFileFromUri
import com.osfans.trime.util.getUriForFile
import java.io.File

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

var default = ""

var documentTreeLauncher: ActivityResultLauncher<Uri?>? = null

init {
context.theme.obtainStyledAttributes(attrs, R.styleable.FolderPickerPreferenceAttrs, 0, 0).run {
try {
Expand Down Expand Up @@ -68,7 +70,7 @@ class FolderPickerPreference
dialogView = FolderPickerDialogBinding.inflate(LayoutInflater.from(context))
dialogView.editText.setText(initValue)
dialogView.button.setOnClickListener {
documentTreeLauncher.launch(context.getUriForFile(File(initValue)))
documentTreeLauncher?.launch(context.getUriForFile(File(initValue)))
}
AlertDialog
.Builder(context)
Expand All @@ -89,4 +91,8 @@ class FolderPickerPreference
notifyChanged()
}
}

fun onResult(result: Uri) {
dialogView.editText.setText(context.getFileFromUri(result)?.absolutePath)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import com.osfans.trime.ui.main.MainViewModel
import com.osfans.trime.util.ResourceUtils
import com.osfans.trime.util.appContext
import com.osfans.trime.util.formatDateTime
import com.osfans.trime.util.getFileFromUri
import com.osfans.trime.util.rimeActionWithResultDialog
import com.osfans.trime.util.toast
import com.osfans.trime.util.withLoadingDialog
Expand All @@ -55,7 +54,7 @@ class ProfileFragment :
it,
DocumentsContract.getTreeDocumentId(it),
)
dialogView.editText.setText(context.getFileFromUri(uri)?.absolutePath)
onResult(uri)
}
}

Expand Down

0 comments on commit ed113a8

Please sign in to comment.