Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mobileapps-1169 #109

Merged
merged 5 commits into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ data class ActionMoveFilesFolders(

override suspend fun execute(context: Context): Entry {

val result = ActionMoveFragment.moveItem(context)
val result = ActionMoveFragment.moveItem(context, entry)
if (!result.isNullOrEmpty()) {
withContext(Dispatchers.IO) {
BrowseRepository().moveNode(entry.id, result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@ import android.content.Context
import android.os.Bundle
import androidx.activity.result.ActivityResultLauncher
import androidx.fragment.app.Fragment
import com.alfresco.content.withFragment
import com.alfresco.content.data.Entry
import com.alfresco.content.withNewFragment
import kotlinx.coroutines.CancellableContinuation
import kotlinx.coroutines.suspendCancellableCoroutine

/**
* Mark as ActionMoveFragment
*/
class ActionMoveFragment : Fragment() {
class ActionMoveFragment(private val entryObj: Entry?) : Fragment() {
private lateinit var requestLauncher: ActivityResultLauncher<Unit>
private var onResult: CancellableContinuation<String?>? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

requestLauncher = registerForActivityResult(MoveResultContract()) {
requestLauncher = registerForActivityResult(MoveResultContract(entryObj)) {
onResult?.resume(it, null)
}
}
Expand All @@ -36,13 +37,14 @@ class ActionMoveFragment : Fragment() {
* Generating ActionMoveFragment
*/
suspend fun moveItem(
context: Context
context: Context,
entry: Entry
): String? =
withFragment(
withNewFragment(
context,
TAG,
{ it.moveItems() },
{ ActionMoveFragment() }
{ ActionMoveFragment(entry) }
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import android.content.Context
import android.content.Intent
import androidx.activity.result.contract.ActivityResultContract
import androidx.annotation.CallSuper
import com.alfresco.content.data.Entry

/**
* Mark as MoveResultContract
*/
class MoveResultContract : ActivityResultContract<Unit, String?>() {
class MoveResultContract(private val entryObj: Entry?) : ActivityResultContract<Unit, String?>() {
@CallSuper
override fun createIntent(context: Context, input: Unit): Intent {
intent.putExtra(ENTRY_OBJ_KEY, entryObj)
return intent
}

Expand All @@ -22,6 +24,8 @@ class MoveResultContract : ActivityResultContract<Unit, String?>() {

companion object {
const val OUTPUT_KEY = "targetParentId"
const val ENTRY_OBJ_KEY = "entryObj"
const val MOVE_ID_KEY = "moveId"
lateinit var intent: Intent

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import com.airbnb.mvrx.MavericksView
import com.airbnb.mvrx.withState
import com.alfresco.auth.activity.LoginViewModel
import com.alfresco.content.actions.Action
import com.alfresco.content.actions.MoveResultContract.Companion.ENTRY_OBJ_KEY
import com.alfresco.content.activityViewModel
import com.alfresco.content.app.R
import com.alfresco.content.app.widget.ActionBarController
import com.alfresco.content.data.Entry
import com.alfresco.content.session.SessionManager
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import java.lang.ref.WeakReference
Expand All @@ -28,18 +30,24 @@ class MoveActivity : AppCompatActivity(), MavericksView {
private val bottomView by lazy { findViewById<View>(R.id.bottom_view) }
private lateinit var actionBarController: ActionBarController
private var signedOutDialog = WeakReference<AlertDialog>(null)
private var entryObj: Entry? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_move)

if (intent.extras != null) {
entryObj = intent.getParcelableExtra(ENTRY_OBJ_KEY) as Entry?
}

configure()
}

private fun configure() {
val graph = navController.navInflater.inflate(R.navigation.nav_move_paths)
graph.startDestination = R.id.nav_move
val bundle = Bundle().apply {
putParcelable(ENTRY_OBJ_KEY, entryObj)
}
navController.setGraph(graph, bundle)
setupActionToasts()
Expand Down
26 changes: 16 additions & 10 deletions app/src/main/res/navigation/nav_move_paths.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,12 @@
android:label="">

<argument
android:name="id"
app:argType="string" />

<argument
android:name="title"
android:name="path"
app:argType="string" />

<argument
android:name="path"
app:argType="string" />
android:name="entryObj"
app:argType="com.alfresco.content.data.Entry" />

</fragment>

Expand All @@ -34,12 +30,15 @@
<argument
android:name="id"
app:argType="string" />
<argument
android:name="moveId"
app:argType="string" />
<argument
android:name="title"
app:argType="string"
android:defaultValue=""/>

<deepLink app:uri="alfresco://content/browse_child/{path}/remote/{id}?title={title}" />
<deepLink app:uri="alfresco://content/browse_child/{path}/remote/{id}/{moveId}?title={title}" />

</fragment>

Expand All @@ -54,12 +53,15 @@
<argument
android:name="id"
app:argType="string" />
<argument
android:name="moveId"
app:argType="string" />
<argument
android:name="title"
app:argType="string"
android:defaultValue=""/>

<deepLink app:uri="alfresco://content/browse_move_parent/{path}/remote/{id}?title={title}" />
<deepLink app:uri="alfresco://content/browse_move_parent/{path}/remote/{id}/{moveId}?title={title}" />

</fragment>

Expand All @@ -81,7 +83,11 @@
app:argType="boolean"
android:defaultValue="false"/>

<deepLink app:uri="alfresco://content/search/folder/{id}/{extension}?title={title}" />
<argument
android:name="moveId"
app:argType="string" />

<deepLink app:uri="alfresco://content/search/folder/{id}/{extension}/{moveId}?title={title}" />
</fragment>

</navigation>
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import androidx.transition.TransitionManager
import androidx.transition.TransitionSet
import com.airbnb.mvrx.withState
import com.alfresco.content.actions.CreateActionsSheet
import com.alfresco.content.actions.MoveResultContract.Companion.MOVE_ID_KEY
import com.alfresco.content.data.Entry
import com.alfresco.content.fragmentViewModelWithArgs
import com.alfresco.content.listview.ListFragment
Expand All @@ -34,6 +35,7 @@ import kotlinx.parcelize.Parcelize
data class BrowseArgs(
val path: String,
val id: String?,
val moveId: String,
val title: String?
) : Parcelable {
companion object {
Expand All @@ -45,6 +47,7 @@ data class BrowseArgs(
return BrowseArgs(
args.getString(PATH_KEY, ""),
args.getString(ID_KEY, null),
args.getString(MOVE_ID_KEY, ""),
args.getString(TITLE_KEY, null)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import java.time.temporal.ChronoField
data class BrowseViewState(
val path: String,
val nodeId: String?,
val moveId: String,
val parent: Entry? = null,
override val entries: List<Entry> = emptyList(),
override val hasMoreItems: Boolean = false,
Expand All @@ -26,7 +27,7 @@ data class BrowseViewState(
val totalTransfersSize: Int = 0
) : ListViewState {

constructor(args: BrowseArgs) : this(args.path, args.id)
constructor(args: BrowseArgs) : this(args.path, args.id, args.moveId)

override val isCompact: Boolean
get() = when (path) {
Expand All @@ -40,7 +41,7 @@ data class BrowseViewState(
if (response == null) return this

val nextPage = response.pagination.skipCount > 0
val pageEntries = response.entries
val pageEntries = response.entries.filter { it.id != moveId }
val newEntries = if (nextPage) {
baseEntries + pageEntries
} else {
Expand Down
44 changes: 43 additions & 1 deletion common/src/main/kotlin/com/alfresco/content/FragmentExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.alfresco.content
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import java.lang.ClassCastException
import kotlinx.coroutines.CancellableContinuation
import kotlinx.coroutines.suspendCancellableCoroutine

Expand All @@ -21,6 +20,20 @@ suspend fun <F : Fragment, R> withFragment(
findFragmentAndResume(context, tag, continuation, factory)
})

/**
* Calls the specified [lambda] with the a [F] fragment as the receiver and returns its result.
* creates [F] fragment new instance using [factory].
*/
suspend fun <F : Fragment, R> withNewFragment(
context: Context,
tag: String,
lambda: suspend (F) -> R,
factory: () -> F
): R =
lambda(suspendCancellableCoroutine { continuation ->
findNewInstance(context, tag, continuation, factory)
})

private fun <F : Fragment> findFragmentAndResume(
context: Context,
tag: String,
Expand Down Expand Up @@ -52,3 +65,32 @@ private fun <F : Fragment> findFragmentAndResume(
}.commit()
}
}

/**
* finding new instance of fragment
*/
private fun <F : Fragment> findNewInstance(
context: Context,
tag: String,
continuation: CancellableContinuation<F>,
factory: () -> F
) {
val fragmentManager = when (context) {
is AppCompatActivity -> context.supportFragmentManager
is Fragment -> context.childFragmentManager
else -> null
}

if (fragmentManager == null) {
continuation.cancel(ClassCastException())
return
}

val fragment = factory()
fragmentManager.beginTransaction().add(
fragment,
tag
).runOnCommit {
continuation.resume(fragment, null)
}.commit()
}
24 changes: 17 additions & 7 deletions common/src/main/kotlin/com/alfresco/content/NavControllerExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,17 @@ fun NavController.navigateTo(entry: Entry) {

private fun NavController.navigateToFolder(entry: Entry) {
if (entry.isExtension)
navigateToChildFolder(entry.id, entry.name, modeFor(entry))
navigateToChildFolder(entry.id, entry.name, mode = modeFor(entry))
else navigateToFolder(entry.id, entry.name, modeFor(entry))
}

/**
* navigate to move screen
*/
fun NavController.navigateToFolder(entry: Entry, moveId: String) {
navigateToChildFolder(entry.id, entry.name, moveId, modeFor(entry))
}

private fun modeFor(entry: Entry) =
if (entry.hasOfflineStatus) {
"local"
Expand All @@ -48,8 +55,11 @@ private fun NavController.navigateFileLink(entry: Entry) =
private fun NavController.navigateFolderLink(entry: Entry) =
navigate(Uri.parse("$BASE_URI/browse/folder/$REMOTE/${entry.otherId}?title=${Uri.encode(entry.name)}"))

fun NavController.navigateToContextualSearch(id: String, title: String, isExtension: Boolean) =
navigate(Uri.parse("$BASE_URI/search/folder/$id/$isExtension?title=${Uri.encode(title)}"))
/**
* navigate to contextual search
*/
fun NavController.navigateToContextualSearch(id: String, title: String, isExtension: Boolean, moveId: String = "") =
navigate(Uri.parse("$BASE_URI/search/folder/$id/$isExtension/$moveId?title=${Uri.encode(title)}"))

/**
* navigate to browse parent folder
Expand All @@ -61,15 +71,15 @@ fun NavController.navigateToParent(id: String, title: String, mode: String = REM
/**
* navigate to browse move parent folder
*/
fun NavController.navigateToMoveParent(id: String, title: String, mode: String = REMOTE) {
navigate(Uri.parse("$BASE_URI/browse_move_parent/move/$mode/$id?title=${Uri.encode(title)}"))
fun NavController.navigateToMoveParent(id: String, moveId: String, title: String, mode: String = REMOTE) {
navigate(Uri.parse("$BASE_URI/browse_move_parent/move/$mode/$id/$moveId?title=${Uri.encode(title)}"))
}

/**
* navigate to browse child folder
*/
fun NavController.navigateToChildFolder(id: String, title: String, mode: String = REMOTE) {
navigate(Uri.parse("$BASE_URI/browse_child/extension/$mode/$id?title=${Uri.encode(title)}"))
fun NavController.navigateToChildFolder(id: String, title: String, moveId: String = "", mode: String = REMOTE) {
navigate(Uri.parse("$BASE_URI/browse_child/extension/$mode/$id/$moveId?title=${Uri.encode(title)}"))
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import com.alfresco.content.browse.BrowseViewState
import com.alfresco.content.data.Entry
import com.alfresco.content.fragmentViewModelWithArgs
import com.alfresco.content.listview.ListFragment
import com.alfresco.content.navigateTo
import com.alfresco.content.navigateToContextualSearch
import com.alfresco.content.navigateToFolder

/**
* Mark as BrowseMoveFragment
Expand Down Expand Up @@ -62,7 +62,9 @@ class BrowseMoveFragment : ListFragment<BrowseViewModel, BrowseViewState>(R.layo
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.search -> {
findNavController().navigateToContextualSearch(args.id ?: "", args.title ?: "", true)
withState(viewModel) { state ->
findNavController().navigateToContextualSearch(args.id ?: "", args.title ?: "", true, state.moveId)
}
true
}
R.id.new_folder -> {
Expand All @@ -86,7 +88,8 @@ class BrowseMoveFragment : ListFragment<BrowseViewModel, BrowseViewState>(R.layo
*/
override fun onItemClicked(entry: Entry) {
if (!entry.isFolder) return

findNavController().navigateTo(entry)
withState(viewModel) { state ->
findNavController().navigateToFolder(entry, state.moveId)
}
}
}
Loading