@@ -14,13 +14,12 @@ import com.github.terrakok.cicerone.Navigator
14
14
import com.github.terrakok.cicerone.Replace
15
15
import com.github.terrakok.cicerone.androidx.ActivityScreen
16
16
import com.github.terrakok.cicerone.androidx.AppScreen
17
- import com.github.terrakok.cicerone.androidx.FragmentScreen
18
17
import com.github.terrakok.cicerone.androidx.TransactionInfo
19
- import ru.wearemad.mad_navigation.route.FragmentRoute
20
18
import ru.wearemad.mad_navigation.commands.AddScreenCommand
21
19
import ru.wearemad.mad_navigation.commands.ExitWithFragmentResult
22
20
import ru.wearemad.mad_navigation.commands.OpenDialogCommand
23
21
import ru.wearemad.mad_navigation.commands.ReplaceTabCommand
22
+ import ru.wearemad.mad_navigation.route.FragmentRoute
24
23
25
24
abstract class BaseNavigator (
26
25
private val fragmentManager : FragmentManager ,
@@ -65,10 +64,8 @@ abstract class BaseNavigator(
65
64
66
65
protected open fun forward (command : Forward ) {
67
66
when (val screen = command.screen as AppScreen ) {
68
- is ActivityScreen -> {
69
- checkAndStartActivity(screen)
70
- }
71
- is FragmentScreen -> {
67
+ is ActivityScreen -> checkAndStartActivity(screen)
68
+ is FragmentRoute -> {
72
69
val type = if (command.clearContainer) TransactionInfo .Type .REPLACE else TransactionInfo .Type .ADD
73
70
commitNewFragmentScreen(screen, type, true )
74
71
}
@@ -81,7 +78,7 @@ abstract class BaseNavigator(
81
78
checkAndStartActivity(screen)
82
79
activityBack()
83
80
}
84
- is FragmentScreen -> {
81
+ is FragmentRoute -> {
85
82
if (localStackCopy.isNotEmpty()) {
86
83
fragmentManager.popBackStack()
87
84
val removed = localStackCopy.removeAt(localStackCopy.lastIndex)
@@ -165,29 +162,32 @@ abstract class BaseNavigator(
165
162
166
163
167
164
protected open fun commitNewFragmentScreen (
168
- screen : FragmentScreen ,
165
+ screen : FragmentRoute ,
169
166
type : TransactionInfo .Type ,
170
167
addToBackStack : Boolean
171
168
) {
172
169
val fragment = screen.createFragment(fragmentFactory)
170
+
173
171
val transaction = fragmentManager.beginTransaction()
174
172
transaction.setReorderingAllowed(true )
175
- if (screen is FragmentRoute ) {
176
- screen.animation?.let {
177
- transaction
178
- .setCustomAnimations(
179
- it.enter,
180
- it.exit,
181
- it.popEnter,
182
- it.popExit
183
- )
184
- }
185
- }
173
+
186
174
setupFragmentTransaction(
187
175
transaction,
188
176
fragmentManager.findFragmentById(containerId),
189
- fragment
177
+ fragment,
178
+ screen
190
179
)
180
+
181
+ screen.animation?.let {
182
+ transaction
183
+ .setCustomAnimations(
184
+ it.enter,
185
+ it.exit,
186
+ it.popEnter,
187
+ it.popExit
188
+ )
189
+ }
190
+
191
191
when (type) {
192
192
TransactionInfo .Type .ADD -> transaction.add(containerId, fragment, screen.screenKey)
193
193
TransactionInfo .Type .REPLACE -> transaction.replace(containerId, fragment, screen.screenKey)
@@ -232,9 +232,24 @@ abstract class BaseNavigator(
232
232
protected open fun setupFragmentTransaction (
233
233
fragmentTransaction : FragmentTransaction ,
234
234
currentFragment : Fragment ? ,
235
- nextFragment : Fragment ?
235
+ nextFragment : Fragment ? ,
236
+ screen : FragmentRoute
236
237
) {
237
- // Do nothing by default
238
+ val transitionInfo = screen.sharedTransitionInfo
239
+ if (transitionInfo.hasSharedElements.not ()) {
240
+ return
241
+ }
242
+ screen.applySharedTransitionInfo(nextFragment)
243
+ fragmentTransaction.setReorderingAllowed(true )
244
+ transitionInfo
245
+ .elements
246
+ .forEach { pair ->
247
+ fragmentTransaction.addSharedElement(
248
+ pair.first,
249
+ pair.second
250
+ )
251
+ }
252
+ transitionInfo.clearResources()
238
253
}
239
254
240
255
/* *
@@ -279,7 +294,17 @@ abstract class BaseNavigator(
279
294
private fun addScreen (command : AddScreenCommand ) {
280
295
val screen = command.screen
281
296
val newFragment = screen.createFragment(fragmentFactory)
297
+
282
298
val fragmentTransaction = fragmentManager.beginTransaction()
299
+ fragmentTransaction.setReorderingAllowed(true )
300
+
301
+ setupFragmentTransaction(
302
+ fragmentTransaction,
303
+ fragmentManager.findFragmentById(containerId),
304
+ newFragment,
305
+ screen
306
+ )
307
+
283
308
command.screen.animation?.let {
284
309
fragmentTransaction
285
310
.setCustomAnimations(
0 commit comments