-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from Atwa/refactor/file_picker_processor
Refactor/file picker processor
- Loading branch information
Showing
18 changed files
with
311 additions
and
490 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
filepicker/src/main/java/com/atwa/filepicker/core/ActivityConfiguration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.atwa.filepicker.core | ||
|
||
import android.content.Intent | ||
import android.net.Uri | ||
import android.os.Handler | ||
import android.os.Looper | ||
import androidx.activity.result.contract.ActivityResultContracts | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.lifecycle.lifecycleScope | ||
import com.atwa.filepicker.decoder.Decoder | ||
import com.atwa.filepicker.decoder.UriDecoder | ||
import java.lang.ref.WeakReference | ||
|
||
class ActivityConfiguration( | ||
private val activity: WeakReference<AppCompatActivity>, | ||
) : PickerConfiguration { | ||
|
||
private var callback: ((uri: Uri?) -> Unit)? = null | ||
private val launcher = activity.get() | ||
?.registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> | ||
callback?.invoke(result?.data?.data) | ||
} | ||
|
||
override val lifecycle = activity.get()?.lifecycle | ||
override val lifecycleScope = activity.get()?.lifecycleScope | ||
override val decoder: Decoder by lazy { | ||
UriDecoder(activity.get()?.applicationContext, Handler(Looper.getMainLooper())) | ||
} | ||
|
||
override fun Intent.onPick(callback: (uri: Uri?) -> Unit) { | ||
this@ActivityConfiguration.callback = callback | ||
launcher?.launch(this) | ||
} | ||
|
||
override fun clear() { | ||
activity.clear() | ||
} | ||
} |
116 changes: 0 additions & 116 deletions
116
filepicker/src/main/java/com/atwa/filepicker/core/ActivityFilePicker.kt
This file was deleted.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
filepicker/src/main/java/com/atwa/filepicker/core/Executors.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.atwa.filepicker.core | ||
|
||
import java.util.concurrent.Executor | ||
import java.util.concurrent.Executors | ||
|
||
object Executors { | ||
val executor: Executor by lazy { Executors.newFixedThreadPool(4) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
filepicker/src/main/java/com/atwa/filepicker/core/FilePickerProcessor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package com.atwa.filepicker.core | ||
|
||
import android.provider.MediaStore | ||
import androidx.core.net.toUri | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.lifecycle.LifecycleEventObserver | ||
import androidx.lifecycle.LifecycleOwner | ||
import com.atwa.filepicker.core.PickIntent.captureIntent | ||
import com.atwa.filepicker.core.PickIntent.fileIntent | ||
import com.atwa.filepicker.core.PickIntent.imageIntent | ||
import com.atwa.filepicker.core.PickIntent.pdfIntent | ||
import com.atwa.filepicker.core.PickIntent.videoIntent | ||
import com.atwa.filepicker.result.FileMeta | ||
import com.atwa.filepicker.result.ImageMeta | ||
import com.atwa.filepicker.result.VideoMeta | ||
|
||
class FilePickerProcessor(private val configuration: PickerConfiguration) : FilePicker { | ||
|
||
init { | ||
observeLifeCycle() | ||
} | ||
|
||
|
||
private fun observeLifeCycle() { | ||
configuration.lifecycle?.addObserver(object : LifecycleEventObserver { | ||
override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) { | ||
if (event == Lifecycle.Event.ON_DESTROY) { | ||
configuration.lifecycle?.removeObserver(this) | ||
configuration.clear() | ||
} | ||
} | ||
}) | ||
} | ||
|
||
override fun pickImage(onImagePicked: (ImageMeta?) -> Unit) { | ||
with(configuration) { | ||
imageIntent.onPick { uri -> | ||
decoder.getStorageImage(uri, onImagePicked) | ||
} | ||
} | ||
} | ||
|
||
override fun captureCameraImage(onImagePicked: (ImageMeta?) -> Unit) { | ||
with(configuration) { | ||
val photoURI = decoder.createFile() | ||
captureIntent.apply { putExtra(MediaStore.EXTRA_OUTPUT, photoURI) }.onPick { | ||
decoder.getCameraImage(photoURI, onImagePicked) | ||
} | ||
} | ||
} | ||
|
||
override fun pickPdf(onPdfPicked: (FileMeta?) -> Unit) { | ||
with(configuration) { | ||
pdfIntent.onPick { uri -> | ||
decoder.getStoragePDF(uri, onPdfPicked) | ||
} | ||
} | ||
} | ||
|
||
override fun pickVideo(onVideoPicked: (VideoMeta?) -> Unit) { | ||
with(configuration) { | ||
videoIntent.onPick { uri -> | ||
decoder.getStorageVideo(uri, onVideoPicked) | ||
} | ||
} | ||
} | ||
|
||
override fun pickFile(onFilePicked: (FileMeta?) -> Unit) { | ||
pickFile("", onFilePicked) | ||
} | ||
|
||
override fun pickFile(initialDirectoryPath: String, onFilePicked: (FileMeta?) -> Unit) { | ||
with(configuration) { | ||
fileIntent.apply { | ||
if (initialDirectoryPath.isNotBlank()) | ||
data = initialDirectoryPath.toUri() | ||
type = "*/*" | ||
}.onPick { uri -> | ||
decoder.getStorageFile(uri, onFilePicked) | ||
} | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
filepicker/src/main/java/com/atwa/filepicker/core/FragmentConfiguration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.atwa.filepicker.core | ||
|
||
import android.content.Intent | ||
import android.net.Uri | ||
import android.os.Handler | ||
import android.os.Looper | ||
import androidx.activity.result.contract.ActivityResultContracts | ||
import androidx.fragment.app.Fragment | ||
import androidx.lifecycle.lifecycleScope | ||
import com.atwa.filepicker.decoder.Decoder | ||
import com.atwa.filepicker.decoder.UriDecoder | ||
import java.lang.ref.WeakReference | ||
|
||
class FragmentConfiguration(private val fragment: WeakReference<Fragment>) : | ||
PickerConfiguration { | ||
|
||
var callback: ((uri: Uri?) -> Unit)? = null | ||
private val launcher = fragment.get() | ||
?.registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> | ||
callback?.invoke(result?.data?.data) | ||
} | ||
|
||
override val lifecycle = fragment.get()?.lifecycle | ||
override val lifecycleScope = fragment.get()?.lifecycleScope | ||
override val decoder: Decoder by lazy { | ||
UriDecoder( | ||
fragment.get()?.requireActivity()?.applicationContext, | ||
Handler(Looper.getMainLooper()) | ||
) | ||
} | ||
|
||
override fun Intent.onPick(callback: (uri: Uri?) -> Unit) { | ||
this@FragmentConfiguration.callback = callback | ||
launcher?.launch(this) | ||
} | ||
|
||
override fun clear() { | ||
fragment.clear() | ||
} | ||
} |
Oops, something went wrong.