forked from bisq-network/bisq
/
PhasesView.java
146 lines (118 loc) · 5.16 KB
/
PhasesView.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
/*
* 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.main.dao.governance;
import bisq.desktop.components.SeparatedPhaseBars;
import bisq.desktop.util.Layout;
import bisq.core.dao.DaoFacade;
import bisq.core.dao.state.DaoStateListener;
import bisq.core.dao.state.blockchain.Block;
import bisq.core.dao.state.period.DaoPhase;
import bisq.core.locale.Res;
import javax.inject.Inject;
import javafx.scene.layout.GridPane;
import javafx.geometry.Insets;
import org.fxmisc.easybind.EasyBind;
import org.fxmisc.easybind.Subscription;
import java.util.Arrays;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import static bisq.desktop.util.FormBuilder.addTitledGroupBg;
@Slf4j
public class PhasesView implements DaoStateListener {
private final DaoFacade daoFacade;
private SeparatedPhaseBars separatedPhaseBars;
private List<SeparatedPhaseBars.SeparatedPhaseBarsItem> phaseBarsItems;
private Subscription phaseSubscription;
@Inject
private PhasesView(DaoFacade daoFacade) {
this.daoFacade = daoFacade;
}
public int addGroup(GridPane gridPane, int gridRow) {
addTitledGroupBg(gridPane, gridRow, 1, Res.get("dao.cycle.headline"));
separatedPhaseBars = createSeparatedPhaseBars();
GridPane.setColumnSpan(separatedPhaseBars, 2);
GridPane.setColumnIndex(separatedPhaseBars, 0);
GridPane.setMargin(separatedPhaseBars, new Insets(Layout.FIRST_ROW_DISTANCE - 6, 0, 0, 0));
GridPane.setRowIndex(separatedPhaseBars, gridRow);
gridPane.getChildren().add(separatedPhaseBars);
return gridRow;
}
public void activate() {
daoFacade.addBsqStateListener(this);
phaseSubscription = EasyBind.subscribe(daoFacade.phaseProperty(), phase -> {
phaseBarsItems.forEach(item -> {
if (item.getPhase() == phase) {
item.setActive();
} else {
item.setInActive();
}
});
});
applyData(daoFacade.getChainHeight());
}
public void deactivate() {
daoFacade.removeBsqStateListener(this);
phaseSubscription.unsubscribe();
}
///////////////////////////////////////////////////////////////////////////////////////////
// DaoStateListener
///////////////////////////////////////////////////////////////////////////////////////////
@Override
public void onNewBlockHeight(int height) {
applyData(height);
}
@Override
public void onParseTxsComplete(Block block) {
}
@Override
public void onParseBlockChainComplete() {
}
///////////////////////////////////////////////////////////////////////////////////////////
// Private
///////////////////////////////////////////////////////////////////////////////////////////
private SeparatedPhaseBars createSeparatedPhaseBars() {
phaseBarsItems = Arrays.asList(
new SeparatedPhaseBars.SeparatedPhaseBarsItem(DaoPhase.Phase.PROPOSAL, true),
new SeparatedPhaseBars.SeparatedPhaseBarsItem(DaoPhase.Phase.BREAK1, false),
new SeparatedPhaseBars.SeparatedPhaseBarsItem(DaoPhase.Phase.BLIND_VOTE, true),
new SeparatedPhaseBars.SeparatedPhaseBarsItem(DaoPhase.Phase.BREAK2, false),
new SeparatedPhaseBars.SeparatedPhaseBarsItem(DaoPhase.Phase.VOTE_REVEAL, true),
new SeparatedPhaseBars.SeparatedPhaseBarsItem(DaoPhase.Phase.BREAK3, false),
new SeparatedPhaseBars.SeparatedPhaseBarsItem(DaoPhase.Phase.RESULT, false));
return new SeparatedPhaseBars(phaseBarsItems);
}
private void applyData(int height) {
if (height > 0) {
phaseBarsItems.forEach(item -> {
int firstBlock = daoFacade.getFirstBlockOfPhase(height, item.getPhase());
int lastBlock = daoFacade.getLastBlockOfPhase(height, item.getPhase());
final int duration = daoFacade.getDurationForPhase(item.getPhase());
item.setPeriodRange(firstBlock, lastBlock, duration);
double progress = 0;
if (height >= firstBlock && height <= lastBlock) {
progress = (double) (height - firstBlock + 1) / (double) duration;
} else if (height < firstBlock) {
progress = 0;
} else if (height > lastBlock) {
progress = 1;
}
item.getProgressProperty().set(progress);
});
separatedPhaseBars.updateWidth();
}
}
}