Skip to content

Commit

Permalink
handle single or multiple selection
Browse files Browse the repository at this point in the history
  • Loading branch information
aman-alfresco committed Apr 7, 2024
1 parent e5baae6 commit 8f89208
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ data class ActionUploadFiles(
private val repository = OfflineRepository()

override suspend fun execute(context: Context): Entry {
val result = ContentPickerFragment.pickItems(context, MIME_TYPES)
val result = ContentPickerFragment.pickItems(context, MIME_TYPES, entry.isMultiple)
if (result.isNotEmpty()) {
when (entry.uploadServer) {
UploadServerType.UPLOAD_TO_TASK, UploadServerType.UPLOAD_TO_PROCESS -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ data class ActionUploadMedia(
private val repository = OfflineRepository()

override suspend fun execute(context: Context): Entry {
val result = ContentPickerFragment.pickItems(context, MIME_TYPES)
val result = ContentPickerFragment.pickItems(context, MIME_TYPES, entry.isMultiple)
if (result.isNotEmpty()) {
when (entry.uploadServer) {
UploadServerType.UPLOAD_TO_TASK, UploadServerType.UPLOAD_TO_PROCESS -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import kotlinx.coroutines.suspendCancellableCoroutine

class ContentPickerFragment : Fragment() {
private lateinit var requestLauncher: ActivityResultLauncher<Array<String>>
private lateinit var requestLauncherSingle: ActivityResultLauncher<Array<String>>
private var onResult: CancellableContinuation<List<Uri>>? = null

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -18,12 +19,20 @@ class ContentPickerFragment : Fragment() {
requestLauncher = registerForActivityResult(GetMultipleContents()) {
onResult?.resume(it, null)
}

requestLauncherSingle = registerForActivityResult(GetSingleContent()) {
onResult?.resume(it, null)
}
}

private suspend fun pickItems(mimeTypes: Array<String>): List<Uri> =
private suspend fun pickItems(mimeTypes: Array<String>, isMultiple: Boolean): List<Uri> =
suspendCancellableCoroutine { continuation ->
onResult = continuation
requestLauncher.launch(mimeTypes)
if (!isMultiple) {
requestLauncherSingle.launch(mimeTypes)
} else {
requestLauncher.launch(mimeTypes)
}
}

companion object {
Expand All @@ -32,11 +41,12 @@ class ContentPickerFragment : Fragment() {
suspend fun pickItems(
context: Context,
mimeTypes: Array<String>,
isMultiple: Boolean = false,
): List<Uri> =
withFragment(
context,
TAG,
{ it.pickItems(mimeTypes) },
{ it.pickItems(mimeTypes, isMultiple) },
{ ContentPickerFragment() },
)
}
Expand Down
69 changes: 69 additions & 0 deletions common/src/main/kotlin/com/alfresco/content/GetSingleContent.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.alfresco.content

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.net.Uri
import androidx.activity.result.contract.ActivityResultContract
import androidx.annotation.CallSuper

/**
* An ActivityResultContract similar to
* [androidx.activity.result.contract.ActivityResultContracts.GetSingleContents]
* that allows specifying multiple mimeTypes.
*/
class GetSingleContent : ActivityResultContract<Array<String>, List<Uri>>() {

@CallSuper
override fun createIntent(context: Context, input: Array<String>): Intent {
return Intent(Intent.ACTION_GET_CONTENT)
.addCategory(Intent.CATEGORY_OPENABLE)
.setType("*/*")
.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, false)
.putExtra(Intent.EXTRA_MIME_TYPES, input)
}

override fun parseResult(resultCode: Int, intent: Intent?): List<Uri> {
return if (intent == null || resultCode != Activity.RESULT_OK) {
emptyList()
} else {
getClipDataUris(intent)
}
}

companion object {
const val MAX_FILE_SIZE = 100

/**
* returns true if file exceed the 100mb length otherwise false
*/
fun isFileSizeExceed(length: Long): Boolean {
val fileLength = length.div(1024L).div(1024L)
return fileLength > MAX_FILE_SIZE.minus(1).toLong()
}

fun getClipDataUris(intent: Intent): List<Uri> {
// Use a LinkedHashSet to maintain any ordering that may be
// present in the ClipData
val resultSet = LinkedHashSet<Uri>()

val intentData = intent.data
if (intentData != null) {
resultSet.add(intentData)
}

val clipData = intent.clipData
if (clipData == null && resultSet.isEmpty()) {
return emptyList()
} else if (clipData != null) {
for (i in 0 until clipData.itemCount) {
val uri = clipData.getItemAt(i).uri
if (uri != null) {
resultSet.add(uri)
}
}
}
return ArrayList(resultSet)
}
}
}
7 changes: 6 additions & 1 deletion data/objectbox-models/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"entities": [
{
"id": "1:3882697123829827748",
"lastPropertyId": "33:8784310188115192790",
"lastPropertyId": "34:9044184444890640140",
"name": "Entry",
"properties": [
{
Expand Down Expand Up @@ -158,6 +158,11 @@
"id": "33:8784310188115192790",
"name": "observerID",
"type": 9
},
{
"id": "34:9044184444890640140",
"name": "isMultiple",
"type": 1
}
],
"relations": []
Expand Down
7 changes: 6 additions & 1 deletion data/objectbox-models/default.json.bak
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"entities": [
{
"id": "1:3882697123829827748",
"lastPropertyId": "32:675229831295035009",
"lastPropertyId": "33:8784310188115192790",
"name": "Entry",
"properties": [
{
Expand Down Expand Up @@ -153,6 +153,11 @@
"id": "32:675229831295035009",
"name": "canDelete",
"type": 1
},
{
"id": "33:8784310188115192790",
"name": "observerID",
"type": 9
}
],
"relations": []
Expand Down
5 changes: 3 additions & 2 deletions data/src/main/kotlin/com/alfresco/content/data/Entry.kt
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ data class Entry(
val isReadOnly: Boolean = false,
var isSelectedForMultiSelection: Boolean = false,
var observerID: String = "",
var isMultiple: Boolean = false,
) : ParentEntry(), Parcelable {

val isSynced: Boolean
Expand Down Expand Up @@ -448,8 +449,8 @@ data class Entry(
/**
* return the default Workflow content entry obj
*/
fun defaultWorkflowEntry(id: String?, fieldId: String = ""): Entry {
return Entry(uploadServer = UploadServerType.UPLOAD_TO_PROCESS, parentId = id, observerID = fieldId)
fun defaultWorkflowEntry(id: String?, fieldId: String = "", isMultiple: Boolean = false): Entry {
return Entry(uploadServer = UploadServerType.UPLOAD_TO_PROCESS, parentId = id, observerID = fieldId, isMultiple = isMultiple)
}

fun withSelectedEntries(entries: List<Entry>): Entry {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ abstract class ProcessBaseFragment : Fragment(), DeleteContentListener {

internal fun showCreateSheet(state: ProcessAttachFilesViewState, observerID: String) {
AnalyticsManager().taskEvent(EventName.UploadProcessAttachment)
CreateActionsSheet.with(Entry.defaultWorkflowEntry(observerID, state.parent.field.id)).show(childFragmentManager, null)
val field = state.parent.field
CreateActionsSheet.with(Entry.defaultWorkflowEntry(observerID, field.id, field.params?.multiple ?: false)).show(childFragmentManager, null)
}

/**
Expand Down

0 comments on commit 8f89208

Please sign in to comment.