forked from bisq-network/bisq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Navigation.java
175 lines (145 loc) · 5.91 KB
/
Navigation.java
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/
package bisq.desktop;
import bisq.desktop.common.view.View;
import bisq.desktop.common.view.ViewPath;
import bisq.desktop.main.MainView;
import bisq.desktop.main.market.MarketView;
import bisq.common.proto.persistable.NavigationPath;
import bisq.common.proto.persistable.PersistedDataHost;
import bisq.common.storage.Storage;
import com.google.inject.Inject;
import javax.inject.Singleton;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.stream.Collectors;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.Nullable;
@Slf4j
@Singleton
public final class Navigation implements PersistedDataHost {
private static final ViewPath DEFAULT_VIEW_PATH = ViewPath.to(MainView.class, MarketView.class);
public interface Listener {
void onNavigationRequested(ViewPath path);
default void onNavigationRequested(ViewPath path, @Nullable Object data) {
}
}
// New listeners can be added during iteration so we use CopyOnWriteArrayList to
// prevent invalid array modification
private final CopyOnWriteArraySet<Listener> listeners = new CopyOnWriteArraySet<>();
private final Storage<NavigationPath> storage;
private ViewPath currentPath;
// Used for returning to the last important view. After setup is done we want to
// return to the last opened view (e.g. sell/buy)
private ViewPath returnPath;
// this string is updated just before saving to disk so it reflects the latest currentPath situation.
private final NavigationPath navigationPath = new NavigationPath();
// Persisted fields
@Getter
@Setter
private ViewPath previousPath = DEFAULT_VIEW_PATH;
@Inject
public Navigation(Storage<NavigationPath> storage) {
this.storage = storage;
storage.setNumMaxBackupFiles(3);
}
@Override
public void readPersisted() {
NavigationPath persisted = storage.initAndGetPersisted(navigationPath, "NavigationPath", 300);
if (persisted != null) {
List<Class<? extends View>> viewClasses = persisted.getPath().stream()
.map(className -> {
try {
return (Class<? extends View>) Class.forName(className).asSubclass(View.class);
} catch (ClassNotFoundException e) {
log.warn("Could not find the viewPath class {}; exception: {}", className, e);
}
return null;
})
.filter(Objects::nonNull)
.collect(Collectors.toList());
if (!viewClasses.isEmpty())
previousPath = new ViewPath(viewClasses);
}
}
@SafeVarargs
public final void navigateTo(Class<? extends View>... viewClasses) {
navigateTo(ViewPath.to(viewClasses), null);
}
@SafeVarargs
public final void navigateToWithData(Object data, Class<? extends View>... viewClasses) {
navigateTo(ViewPath.to(viewClasses), data);
}
public void navigateTo(ViewPath newPath, @Nullable Object data) {
if (newPath == null)
return;
ArrayList<Class<? extends View>> temp = new ArrayList<>();
for (int i = 0; i < newPath.size(); i++) {
Class<? extends View> viewClass = newPath.get(i);
temp.add(viewClass);
if (currentPath == null ||
(currentPath.size() > i &&
viewClass != currentPath.get(i) &&
i != newPath.size() - 1)) {
ArrayList<Class<? extends View>> temp2 = new ArrayList<>(temp);
for (int n = i + 1; n < newPath.size(); n++) {
//noinspection unchecked
Class<? extends View>[] newTemp = new Class[i + 1];
currentPath = ViewPath.to(temp2.toArray(newTemp));
navigateTo(currentPath, data);
viewClass = newPath.get(n);
temp2.add(viewClass);
}
}
}
currentPath = newPath;
previousPath = currentPath;
queueUpForSave();
listeners.forEach((e) -> e.onNavigationRequested(currentPath));
listeners.forEach((e) -> e.onNavigationRequested(currentPath, data));
}
private void queueUpForSave() {
if (currentPath.tip() != null) {
navigationPath.setPath(currentPath.stream().map(Class::getName).collect(Collectors.toList()));
}
storage.queueUpForSave(navigationPath, 1000);
}
public void navigateToPreviousVisitedView() {
if (previousPath == null || previousPath.size() == 0)
previousPath = DEFAULT_VIEW_PATH;
navigateTo(previousPath, null);
}
public void addListener(Listener listener) {
listeners.add(listener);
}
public void removeListener(Listener listener) {
listeners.remove(listener);
}
public ViewPath getReturnPath() {
return returnPath;
}
public ViewPath getCurrentPath() {
return currentPath;
}
public void setReturnPath(ViewPath returnPath) {
this.returnPath = returnPath;
}
}