Skip to content

Commit

Permalink
feat: Support for fragment
Browse files Browse the repository at this point in the history
  • Loading branch information
XayahSuSuSu committed Feb 26, 2022
1 parent 69bc6a5 commit 1d129e2
Showing 1 changed file with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.xayah.materialyoufileexplorer

import android.content.Context
import android.content.Intent
import androidx.activity.ComponentActivity
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.fragment.app.Fragment

class MaterialYouFileExplorer {
private lateinit var activityResultLauncher: ActivityResultLauncher<Intent>
Expand All @@ -14,10 +16,17 @@ class MaterialYouFileExplorer {
}
}

fun toExplorer(activity: ComponentActivity, isFile: Boolean) {
fun initialize(fragment: Fragment, callback: (path: String) -> Unit) {
activityResultLauncher =
fragment.registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
callback(it.data?.getStringExtra("path") ?: "")
}
}

fun toExplorer(context: Context, isFile: Boolean) {
activityResultLauncher.launch(
Intent(
activity,
context,
ExplorerActivity::class.java
).putExtra("isFile", isFile)
)
Expand Down

0 comments on commit 1d129e2

Please sign in to comment.