Skip to content

Commit

Permalink
Support navigation back with result
Browse files Browse the repository at this point in the history
  • Loading branch information
rustamnavoyan committed Sep 20, 2019
1 parent 46b4f8d commit df618ec
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.pandora.bottomnavigator

import android.os.Bundle
import android.view.MenuItem
import androidx.annotation.IdRes
import androidx.annotation.VisibleForTesting
Expand All @@ -28,6 +29,7 @@ import com.pandora.bottomnavigator.FragmentTransactionCommand.Clear
import com.pandora.bottomnavigator.FragmentTransactionCommand.RemoveAllAndAdd
import com.pandora.bottomnavigator.FragmentTransactionCommand.RemoveAllAndShowExisting
import com.pandora.bottomnavigator.FragmentTransactionCommand.ShowAndRemove
import com.pandora.bottomnavigator.FragmentTransactionCommand.ShowAndRemoveWithResult
import com.pandora.bottomnavigator.FragmentTransactionCommand.ShowExisting
import hu.akarnokd.rxjava2.subjects.UnicastWorkSubject
import io.reactivex.Observable
Expand Down Expand Up @@ -176,6 +178,19 @@ open class BottomNavigator internal constructor() : ViewModel() {
}
}

open fun popWithResult(bundle: Bundle?): Boolean {
val popped = tabStackMap.pop()!!
val peek = tabStackMap.peek()
return if (peek == null) {
false
} else {
val (tab, nextFragment) = peek
if (currentTab != tab) currentTab = tab
fragmentCommand(ShowAndRemoveWithResult(nextFragment, popped, bundle))
true
}
}

/**
* Clears backstacks on all tabs, resets everything back to the default tab with its default root fragment.
*/
Expand Down Expand Up @@ -276,6 +291,12 @@ open class BottomNavigator internal constructor() : ViewModel() {
command.removeTag.className, command.showTag.className
)
)
is ShowAndRemoveWithResult ->
listOf(
NavigatorAction.FragmentRemovedWithResult(
command.removeTag.className, command.showTag.className, command.bundle
)
)
is RemoveAllAndShowExisting -> {
command.remove
.map {
Expand Down Expand Up @@ -440,6 +461,9 @@ sealed class NavigatorAction {
data class FragmentRemoved(
val removedFragmentClassName: String?, val newShownFragmentClassName: String?
) : NavigatorAction()
data class FragmentRemovedWithResult(
val removedFragmentClassName: String?, val newShownFragmentClassName: String?, val bundle: Bundle?
) : NavigatorAction()
}

data class FragmentInfo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.pandora.bottomnavigator

import android.os.Bundle
import androidx.annotation.IdRes
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
Expand All @@ -24,6 +25,7 @@ import com.pandora.bottomnavigator.FragmentTransactionCommand.Clear
import com.pandora.bottomnavigator.FragmentTransactionCommand.RemoveAllAndAdd
import com.pandora.bottomnavigator.FragmentTransactionCommand.RemoveAllAndShowExisting
import com.pandora.bottomnavigator.FragmentTransactionCommand.ShowAndRemove
import com.pandora.bottomnavigator.FragmentTransactionCommand.ShowAndRemoveWithResult
import com.pandora.bottomnavigator.FragmentTransactionCommand.ShowExisting
import java.util.UUID

Expand All @@ -36,6 +38,7 @@ internal sealed class FragmentTransactionCommand {
data class AddAndShow(val fragment: Fragment, val tag: TagStructure) : FragmentTransactionCommand()
data class ShowExisting(val tag: TagStructure) : FragmentTransactionCommand()
data class ShowAndRemove(val showTag: TagStructure, val removeTag: TagStructure) : FragmentTransactionCommand()
data class ShowAndRemoveWithResult(val showTag: TagStructure, val removeTag: TagStructure, val bundle: Bundle?) : FragmentTransactionCommand()
data class Clear(val allCurrentTags: List<TagStructure>) : FragmentTransactionCommand()
data class RemoveAllAndAdd(val remove: List<TagStructure>, val add: AddAndShow) : FragmentTransactionCommand()
data class RemoveAllAndShowExisting(val remove: List<TagStructure>, val show: ShowExisting) : FragmentTransactionCommand()
Expand All @@ -58,6 +61,11 @@ internal class FragmentTransactionHandler(
command.removeTag,
runnable
)
is ShowAndRemoveWithResult -> showAndRemoveFragment(
command.showTag,
command.removeTag,
runnable
)
is Clear -> clear(runnable)
is RemoveAllAndAdd -> removeAllAndAdd(command.remove, command.add.fragment, command.add.tag, runnable)
is RemoveAllAndShowExisting -> removeAllAndShow(command.remove, command.show.tag, runnable)
Expand Down
14 changes: 14 additions & 0 deletions sample/src/main/java/com/example/bottomnavigator/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ import androidx.appcompat.widget.Toolbar
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import com.pandora.bottomnavigator.BottomNavigator
import com.pandora.bottomnavigator.NavigatorAction
import io.reactivex.disposables.CompositeDisposable

class MainActivity : AppCompatActivity() {
private lateinit var navigator: BottomNavigator
private var disposables: CompositeDisposable = CompositeDisposable()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -46,6 +49,17 @@ class MainActivity : AppCompatActivity() {
defaultTab = R.id.tab2,
activity = this
)

disposables.add(navigator.infoStream().subscribe {
navigator.currentFragment()?.let { it1 ->
when (it) {
is NavigatorAction.FragmentRemovedWithResult -> {
val result = it.bundle
// Send result to current fragment
}
}
}
})
}

override fun onBackPressed() {
Expand Down

0 comments on commit df618ec

Please sign in to comment.