File tree Expand file tree Collapse file tree 3 files changed +47
-21
lines changed
mvvm4fx-core/src/main/java/com/techsenger/mvvm4fx/core Expand file tree Collapse file tree 3 files changed +47
-21
lines changed Original file line number Diff line number Diff line change @@ -51,25 +51,30 @@ public AbstractComponentViewModel() {
5151 var policy = getHistoryPolicy ();
5252 if (state .get () == ComponentState .CONSTRUCTED ) {
5353 logger .debug ("History policy on constucting: {}" , policy );
54- switch (policy ) {
55- case DATA :
56- getOrRequestHistory ().restoreData (this );
57- postHistoryRestore ();
58- break ;
59- case APPEARANCE :
60- getOrRequestHistory ().restoreAppearance (this );
61- postHistoryRestore ();
62- break ;
63- case ALL :
64- var h = getOrRequestHistory ();
65- h .restoreData (this );
66- h .restoreAppearance (this );
67- postHistoryRestore ();
68- break ;
69- case NONE :
70- break ;
71- default :
72- throw new AssertionError ();
54+ ComponentHistory history = null ;
55+ if (policy != NONE ) {
56+ history = getOrRequestHistory ();
57+ if (history .isFresh ()) {
58+ logFreshHistory ();
59+ } else {
60+ switch (policy ) {
61+ case DATA :
62+ history .restoreData (this );
63+ postHistoryRestore ();
64+ break ;
65+ case APPEARANCE :
66+ history .restoreAppearance (this );
67+ postHistoryRestore ();
68+ break ;
69+ case ALL :
70+ history .restoreData (this );
71+ history .restoreAppearance (this );
72+ postHistoryRestore ();
73+ break ;
74+ default :
75+ throw new AssertionError ();
76+ }
77+ }
7378 }
7479 } else if (state .get () == ComponentState .DEINITIALIZED ) {
7580 logger .debug ("History policy on deinitializing: {}" , policy );
@@ -155,4 +160,8 @@ private ComponentHistory getOrRequestHistory() {
155160 }
156161 return this .history ;
157162 }
163+
164+ private void logFreshHistory () {
165+ logger .debug ("History is fresh. Skipping restoration" );
166+ }
158167}
Original file line number Diff line number Diff line change 2222 */
2323public abstract class AbstractHistory <T extends AbstractComponentViewModel > implements ComponentHistory <T > {
2424
25+ private boolean fresh = true ;
26+
27+ public boolean isFresh () {
28+ return fresh ;
29+ }
2530
2631 @ Override
2732 public void postDeserialize () {
@@ -40,7 +45,7 @@ public void restoreData(T viewModel) {
4045
4146 @ Override
4247 public void saveData (T viewModel ) {
43-
48+ this . fresh = false ;
4449 }
4550
4651 @ Override
@@ -50,6 +55,6 @@ public void restoreAppearance(T viewModel) {
5055
5156 @ Override
5257 public void saveAppearance (T viewModel ) {
53-
58+ this . fresh = false ;
5459 }
5560}
Original file line number Diff line number Diff line change 4242 */
4343public interface ComponentHistory <T extends ComponentViewModel > extends Serializable {
4444
45+ /**
46+ * Returns whether this history instance is fresh, meaning it was newly created and has not yet been used to
47+ * save or restore any component state. A fresh history contains no previously stored data and should not be
48+ * passed to a component for restoration.
49+ *
50+ * <p>Once the component's state has been saved into this history, the flag becomes {@code false}, indicating that
51+ * the history now holds valid data that can be used to restore the component's state in future instances.
52+ *
53+ * @return {@code true} if this history is fresh and has not yet been used, {@code false} otherwise
54+ */
55+ boolean isFresh ();
56+
4557 /**
4658 * Method called before the component is serialized. This can be used to prepare the object's state
4759 * before saving it in a binary format.
You can’t perform that action at this time.
0 commit comments