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

Support navigation back with result #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -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.result
)
)
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 result: 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 result: 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.result
// Send result to current fragment
}
}
}
})
}

override fun onBackPressed() {
Expand Down