Commit ce44512
feat(Android, Stack v5): handle preloading screens (#3576)
## Description
Adds support for preloading screens. Allows for multiple operations in
one React transition.
Closes
software-mansion/react-native-screens-labs#819.
### Details
#### Preload
As described in the description of
#3531,
previous approach to handling detached screens did not work due to how
`react-native` works when children views are reordered and inserted
again.
To mitigate this problem, we decided to change the approach. Now,
`StackScreen`s will be rendered inside `StackHost` in the following
order:
1. First, all attached screens are rendered in the order that reflects
current stack state (so ABC always matches to stack with A at the bottom
and C at the top)
2. Then, all detached screens are rendered.
This change has 2 main advantages:
1. We will never move a screen in `attached` state therefore we will
avoid React removing the screen from parent view when trying to reinsert
it.
2. In result of the above, there will never be a situation where an
attached screen will be popped and pushed in one transaction (unless
someone explicitly does so) therefore we don't need to implement a
mechanism for cancelling out those operations.
There is one drawback: this approach will not help with fixing the
inspector, it needs to be handled separately.
#### Multiple operations in one React transaction.
When multiple operations are performed in one transaction, we can't rely
on the order of updates from React.
For example, if we have `ABCdefg` state (lowercase means detached state)
and we perform at the same time:
```
pop(C)
pop(B)
push(G)
push(E)
```
we end up with `AGEbcdf` state. This information is passed to native
side via those operations:
```
remove(G)
remove(E)
insert(G, 1)
insert(E, 2)
update(B, 'detached')
update(C, 'detached')
update(E, 'attached')
update(G, 'attached')
```
If we tried to translate those operations directly without any
reordering, we would get:
```
pop(B)
pop(C)
push(E)
push(G)
```
1. Order of pops is incorrect - we need to pop the top screen (C) first.
2. Order of pushes is incorrect - we would get `AEG...` instead of
desired `AGE...`.
Therefore we need a mechanism to synchronize the order of operations
with the state of the stack. We can use `StackHost`'s children to
determine this - if we know that our children are `AGEbcdf`, we know
that C needs to be popped before B and G needs to be pushed before E.
## Changes
- add `StackContainerUpdateCoordinator` to ensure correct order of push
& pop operations
- add separate push & pop queues to `StackContainer` to ensure order
(first pops, then pushes) + in the future, allow for simple detection of
necessity of using replace animation
- add batch action to `StackContainer`
- create `Test3576` to test batch operations in new stack implementation
## Before & after - visual documentation
### Preload on TestScreenStack
| Before | After |
| --- | --- |
| <video
src="https://github.com/user-attachments/assets/52ab058b-7bec-4141-b1d7-939e0ba0619c"
/> | <video
src="https://github.com/user-attachments/assets/75641233-0bf7-4e2b-a430-be3d71371c90"
/> |
### Multiple operations in one transaction
https://github.com/user-attachments/assets/00c61bde-3277-4953-9a06-222953c320bb
## Test plan
1. Run `TestScreenStack`. Preload B, push A, push B. Hierarchy should
contain screens ABA.
4. Run `Test3576`. Run scenario 1.
## Checklist
- [x] Included code example that can be used to test this change.
- [x] For visual changes, included screenshots / GIFs / recordings
documenting the change.
- [ ] Ensured that CI passes
---------
Co-authored-by: Kacper Kafara <kacperkafara@gmail.com>1 parent 0577b68 commit ce44512
34 files changed
Lines changed: 491 additions & 142 deletions
File tree
- android/src/main/java/com/swmansion/rnscreens/gamma
- common/event
- helpers
- stack
- host
- screen
- event
- tabs
- event
- apps/src
- tests
- issue-tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| |||
Lines changed: 11 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
Lines changed: 44 additions & 67 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
8 | 7 | | |
9 | 8 | | |
10 | 9 | | |
| |||
13 | 12 | | |
14 | 13 | | |
15 | 14 | | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | 15 | | |
27 | 16 | | |
28 | 17 | | |
| |||
31 | 20 | | |
32 | 21 | | |
33 | 22 | | |
34 | | - | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
35 | 28 | | |
36 | 29 | | |
37 | 30 | | |
| |||
58 | 51 | | |
59 | 52 | | |
60 | 53 | | |
61 | | - | |
| 54 | + | |
62 | 55 | | |
63 | 56 | | |
64 | | - | |
| 57 | + | |
65 | 58 | | |
66 | 59 | | |
67 | 60 | | |
68 | | - | |
69 | | - | |
| 61 | + | |
| 62 | + | |
70 | 63 | | |
71 | 64 | | |
72 | 65 | | |
73 | | - | |
| 66 | + | |
74 | 67 | | |
75 | 68 | | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
82 | 72 | | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
| 73 | + | |
| 74 | + | |
109 | 75 | | |
110 | 76 | | |
111 | | - | |
| 77 | + | |
112 | 78 | | |
113 | | - | |
114 | | - | |
| 79 | + | |
115 | 80 | | |
116 | | - | |
117 | | - | |
118 | | - | |
119 | | - | |
120 | | - | |
| 81 | + | |
121 | 82 | | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | 83 | | |
127 | 84 | | |
| 85 | + | |
128 | 86 | | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
129 | 94 | | |
130 | 95 | | |
131 | 96 | | |
132 | 97 | | |
133 | 98 | | |
134 | 99 | | |
135 | | - | |
136 | | - | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
137 | 104 | | |
138 | | - | |
139 | | - | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
140 | 117 | | |
141 | | - | |
| 118 | + | |
142 | 119 | | |
143 | 120 | | |
144 | 121 | | |
145 | | - | |
| 122 | + | |
146 | 123 | | |
147 | 124 | | |
148 | 125 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
7 | 7 | | |
Lines changed: 43 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
Lines changed: 14 additions & 16 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| 24 | + | |
24 | 25 | | |
25 | 26 | | |
26 | 27 | | |
| |||
44 | 45 | | |
45 | 46 | | |
46 | 47 | | |
47 | | - | |
| 48 | + | |
48 | 49 | | |
49 | 50 | | |
50 | 51 | | |
51 | 52 | | |
52 | | - | |
| 53 | + | |
53 | 54 | | |
54 | 55 | | |
55 | 56 | | |
56 | 57 | | |
57 | | - | |
| 58 | + | |
58 | 59 | | |
59 | 60 | | |
60 | 61 | | |
61 | 62 | | |
62 | | - | |
| 63 | + | |
63 | 64 | | |
64 | 65 | | |
65 | 66 | | |
66 | 67 | | |
67 | | - | |
| 68 | + | |
68 | 69 | | |
69 | | - | |
| 70 | + | |
70 | 71 | | |
71 | 72 | | |
72 | 73 | | |
73 | | - | |
| 74 | + | |
74 | 75 | | |
75 | | - | |
| 76 | + | |
| 77 | + | |
76 | 78 | | |
77 | 79 | | |
78 | 80 | | |
79 | 81 | | |
80 | 82 | | |
81 | | - | |
82 | | - | |
| 83 | + | |
| 84 | + | |
83 | 85 | | |
84 | 86 | | |
85 | 87 | | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
| 88 | + | |
91 | 89 | | |
92 | 90 | | |
93 | 91 | | |
| |||
108 | 106 | | |
109 | 107 | | |
110 | 108 | | |
111 | | - | |
| 109 | + | |
112 | 110 | | |
113 | 111 | | |
114 | 112 | | |
| |||
Lines changed: 13 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
Lines changed: 11 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
| |||
47 | 48 | | |
48 | 49 | | |
49 | 50 | | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
50 | 61 | | |
51 | 62 | | |
52 | 63 | | |
| |||
0 commit comments