Skip to content

Commit

Permalink
Merge pull request #208 from EDLuke/master
Browse files Browse the repository at this point in the history
Adding reordering option to FragNavTransactionOptions
  • Loading branch information
ncapdevi authored Jun 25, 2019
2 parents 9f0adfa + 798fd04 commit 456e804
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.fragment.app.Fragment
import androidx.appcompat.app.AppCompatActivity
import android.util.Log
import android.view.MenuItem
import android.view.View
import com.ncapdevi.fragnav.FragNavController
import com.ncapdevi.fragnav.FragNavLogger
import com.ncapdevi.fragnav.FragNavSwitchController
Expand Down Expand Up @@ -79,8 +80,15 @@ class BottomTabsActivity : AppCompatActivity(), BaseFragment.FragmentNavigation,

}

override fun pushFragment(fragment: Fragment) {
fragNavController.pushFragment(fragment)
override fun pushFragment(fragment: Fragment, sharedElementList: List<Pair<View, String>>?) {
val options = FragNavTransactionOptions.newBuilder()
options.reordering = true
sharedElementList?.let {
it.forEach { pair ->
options.addSharedElement(pair)
}
}
fragNavController.pushFragment(fragment, options.build())

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.appcompat.app.ActionBarDrawerToggle
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import android.view.MenuItem
import android.view.View
import com.ncapdevi.fragnav.FragNavController
import com.ncapdevi.fragnav.FragNavTransactionOptions
import com.ncapdevi.sample.R
Expand Down Expand Up @@ -95,7 +96,7 @@ class NavDrawerActivity : AppCompatActivity(), NavigationView.OnNavigationItemSe
return true
}

override fun pushFragment(fragment: Fragment) {
override fun pushFragment(fragment: Fragment, sharedList: List<Pair<View, String>>?) {
fragNavController.pushFragment(fragment)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ open class BaseFragment : Fragment() {
}

interface FragmentNavigation {
fun pushFragment(fragment: Fragment)
fun pushFragment(fragment: Fragment, sharedElementList: List<Pair<View, String>>?= null)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package com.ncapdevi.sample.fragments

import android.annotation.SuppressLint
import android.os.Build
import android.os.Bundle
import android.transition.TransitionInflater
import android.view.View
import android.widget.CheckBox
import android.widget.EditText
import com.ncapdevi.sample.R

/**
* Created by niccapdevila on 3/26/16.
Expand All @@ -12,8 +17,29 @@ class FavoritesFragment : BaseFragment() {
@SuppressLint("SetTextI18n")
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
sharedElementEnterTransition = TransitionInflater.from(context).inflateTransition(
android.R.transition.fade
)
}
}

val et = view.findViewById<EditText>(R.id.edit_text)
val cb = view.findViewById<CheckBox>(R.id.checkbox)

val list = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
listOf<Pair<View, String>>(
Pair(et, et.transitionName),
Pair(cb, cb.transitionName)
)
} else {
listOf()
}

btn.setOnClickListener {
mFragmentNavigation.pushFragment(FavoritesFragment.newInstance(mInt + 1))
mFragmentNavigation.pushFragment(newInstance(mInt + 1), list)
}
btn.text = """${javaClass.simpleName} $mInt"""
}
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/layout/fragment_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@
android:layout_height="wrap_content" />

<EditText
android:id="@+id/edit_text"
android:transitionName="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="LabelFor"
android:inputType="text" />
<CheckBox
android:id="@+id/checkbox"
android:transitionName="2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Example to show restored state"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,8 @@ class FragNavController constructor(private val fragmentManger: FragmentManager,
options.breadCrumbTitle != null -> setBreadCrumbTitle(options.breadCrumbTitle)
options.breadCrumbShortTitle != null -> setBreadCrumbShortTitle(options.breadCrumbShortTitle)
}

setReorderingAllowed(options.reordering)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class FragNavTransactionOptions private constructor(builder: Builder) {
val breadCrumbTitle: String? = builder.breadCrumbTitle
val breadCrumbShortTitle: String? = builder.breadCrumbShortTitle
val allowStateLoss: Boolean = builder.allowStateLoss

val reordering: Boolean = builder.reordering

class Builder {
var sharedElements: MutableList<Pair<View, String>> = mutableListOf()
Expand All @@ -36,6 +36,7 @@ class FragNavTransactionOptions private constructor(builder: Builder) {
var breadCrumbTitle: String? = null
var breadCrumbShortTitle: String? = null
var allowStateLoss = false
var reordering = false

fun addSharedElement(element: Pair<View, String>): Builder {
sharedElements.add(element)
Expand Down Expand Up @@ -85,6 +86,11 @@ class FragNavTransactionOptions private constructor(builder: Builder) {
return this
}

fun allowReordering(reorder: Boolean): Builder {
reordering = reorder
return this
}

fun build(): FragNavTransactionOptions {
return FragNavTransactionOptions(this)
}
Expand Down

0 comments on commit 456e804

Please sign in to comment.