55import  QtQuick  2.15 
66import  QtQuick.Controls  2.15 
77import  QtQuick.Layouts  1.15 
8+ import  QtQuick.Dialogs  1.3 
89
910import  "../controls" 
1011
12+ // This QML component manages the snapshot loading process in the GUI. 
13+ // It provides visual feedback to the user about the snapshot's loading state. 
14+ 
1115ColumnLayout  {
16+     //  The snapshotLoading property indicates if the snapshot is currently being loaded.
17+     //  When true, the UI will show a loading indicator.
18+     property bool snapshotLoading:  nodeModel .snapshotLoading 
1219    signal snapshotImportCompleted ()
1320    property int snapshotVerificationCycles:  0 
1421    property real snapshotVerificationProgress:  0 
15-     property bool snapshotVerified:  false 
22+     property bool onboarding:  false 
23+ 
24+     //  The snapshotVerified property indicates if the snapshot has been successfully loaded and verified.
25+     //  When true, the UI will transition to the "Snapshot Loaded" page.
26+     property bool snapshotVerified:  onboarding ?  false  :  chainModel .isSnapshotActive 
27+ 
28+     //  The snapshotFileName property holds the name of the snapshot file being loaded.
29+     //  It is set when a file is selected in the FileDialog.
30+     property string snapshotFileName:  " " 
31+ 
32+     //  The snapshotInfo property holds information about the loaded snapshot.
33+     //  It is updated when the snapshot is loaded and verified.
34+     property var  snapshotInfo:  ({})
1635
1736    id:  columnLayout
1837    width:  Math .min (parent .width , 450 )
1938    anchors .horizontalCenter :  parent .horizontalCenter 
2039
21- 
40+     //  The Timer component simulates snapshot verification progress for testing purposes.
41+     //  It updates the snapshotVerificationProgress property, which can be used to display a progress bar.
2242    Timer {
2343        id:  snapshotSimulationTimer
2444        interval:  50  //  Update every 50ms
@@ -29,7 +49,7 @@ ColumnLayout {
2949                snapshotVerificationProgress +=  0.01 
3050            } else  {
3151                snapshotVerificationCycles++ 
32-                 if  (snapshotVerificationCycles <  1 ) {
52+                 if  (snapshotVerificationCycles <  3 ) {
3353                    snapshotVerificationProgress =  0 
3454                } else  {
3555                    running =  false 
@@ -40,9 +60,11 @@ ColumnLayout {
4060        }
4161    }
4262
63+     //  The StackLayout component manages the different pages of the snapshot settings UI.
64+     //  It determines which page to display based on the snapshotLoading and snapshotVerified properties.
4365    StackLayout {
4466        id:  settingsStack
45-         currentIndex:  0 
67+         currentIndex:  onboarding  ?   0   :  snapshotVerified  ?   2   :  snapshotLoading  ?   1   :   0 
4668
4769        ColumnLayout {
4870            Layout .alignment :  Qt .AlignHCenter 
@@ -69,6 +91,8 @@ ColumnLayout {
6991                "  It will be automatically verified in the background." 
7092            }
7193
94+             //  The ContinueButton component is used to trigger the snapshot file selection process.
95+             //  When clicked, it opens a FileDialog for the user to choose a snapshot file.
7296            ContinueButton {
7397                Layout .preferredWidth :  Math .min (300 , columnLayout .width  -  2  *  Layout .leftMargin )
7498                Layout .topMargin :  40 
@@ -78,8 +102,25 @@ ColumnLayout {
78102                Layout .alignment :  Qt .AlignCenter 
79103                text:  qsTr (" Choose snapshot file" 
80104                onClicked:  {
81-                     settingsStack .currentIndex  =  1 
82-                     snapshotSimulationTimer .start ()
105+                     fileDialog .open ()
106+                 }
107+             }
108+ 
109+             //  The FileDialog component is used to allow the user to select a snapshot file from their system.
110+             FileDialog {
111+                 id:  fileDialog
112+                 folder:  shortcuts .home 
113+                 selectMultiple:  false 
114+                 onAccepted:  {
115+                     console .log (" File chosen:" fileDialog .fileUrls )
116+                     snapshotFileName =  fileDialog .fileUrl .toString ()
117+                     console .log (" Snapshot file name:" 
118+                     if  (snapshotFileName .endsWith (" .dat" 
119+                         nodeModel .initializeSnapshot (true , snapshotFileName)
120+                         //  nodeModel.presyncProgress
121+                     } else  {
122+                         console .error (" Snapshot loading failed" 
123+                     }
83124                }
84125            }
85126        }
@@ -102,17 +143,40 @@ ColumnLayout {
102143                Layout .leftMargin :  20 
103144                Layout .rightMargin :  20 
104145                header:  qsTr (" Loading Snapshot" 
146+                 description:  qsTr (" This might take a while..." 
105147            }
106148
149+             //  The ProgressIndicator component displays the progress of the snapshot verification process.
107150            ProgressIndicator {
108151                id:  progressIndicator
109152                Layout .topMargin :  20 
110153                width:  200 
111154                height:  20 
112-                 progress:  snapshotVerificationProgress 
155+                 progress:  nodeModel . snapshotProgress 
113156                Layout .alignment :  Qt .AlignCenter 
114157                progressColor:  Theme .color .blue 
115158            }
159+ 
160+             //  The Connections component listens for signals from the nodeModel
161+             //  to update the UI based on snapshot loading progress.
162+             Connections {
163+                 target:  nodeModel
164+                 function  onSnapshotProgressChanged () {
165+                     progressIndicator .progress  =  nodeModel .snapshotProgress 
166+                 }
167+ 
168+                 function  onSnapshotLoaded (success ) {
169+                     if  (success) {
170+                         chainModel .isSnapshotActiveChanged ()
171+                         snapshotVerified =  chainModel .isSnapshotActive 
172+                         snapshotInfo =  chainModel .getSnapshotInfo ()
173+                         settingsStack .currentIndex  =  2   //  Move to the "Snapshot Loaded" page
174+                     } else  {
175+                         //  Handle snapshot loading failure
176+                         console .error (" Snapshot loading failed" 
177+                     }
178+                 }
179+             }
116180        }
117181
118182        ColumnLayout {
@@ -137,8 +201,11 @@ ColumnLayout {
137201                descriptionColor:  Theme .color .neutral6 
138202                descriptionSize:  17 
139203                descriptionLineHeight:  1.1 
140-                 description:  qsTr (" It contains transactions up to January 12, 2024. Newer transactions still need to be downloaded." + 
141-                 "  The data will be verified in the background." 
204+                 description:  snapshotInfo &&  snapshotInfo[" date" ? 
205+                     qsTr (" It contains transactions up to %1. Newer transactions still need to be downloaded." + 
206+                     "  The data will be verified in the background." arg (snapshotInfo[" date" : 
207+                     qsTr (" It contains transactions up to DEBUG. Newer transactions still need to be downloaded." + 
208+                     "  The data will be verified in the background." 
142209            }
143210
144211            ContinueButton {
@@ -153,6 +220,7 @@ ColumnLayout {
153220                }
154221            }
155222
223+             //  The Setting component provides a toggleable view for detailed snapshot information.
156224            Setting {
157225                id:  viewDetails
158226                Layout .alignment :  Qt .AlignCenter 
@@ -188,16 +256,26 @@ ColumnLayout {
188256                        font .pixelSize :  14 
189257                    }
190258                    CoreText {
191-                         text:  qsTr (" 200,000" 
259+                         text:  snapshotInfo &&  snapshotInfo[" height" ? 
260+                             snapshotInfo[" height" :  qsTr (" DEBUG" 
192261                        Layout .alignment :  Qt .AlignRight 
193262                        font .pixelSize :  14 
194263                    }
195264                }
196265                Separator { Layout .fillWidth :  true  }
197266                CoreText {
198-                     text:  qsTr (" Hash: 0x1234567890abcdef..." 
267+                     //  The CoreText component displays the hash of the loaded snapshot.
268+                     text:  snapshotInfo &&  snapshotInfo[" hashSerialized" ? 
269+                         qsTr (" Hash: %1" arg (snapshotInfo[" hashSerialized" substring (0 , 13 ) +  " ..." : 
270+                         qsTr (" Hash: DEBUG" 
199271                    font .pixelSize :  14 
200272                }
273+ 
274+                 Component .onCompleted :  {
275+                     if  (snapshotVerified) {
276+                         snapshotInfo =  chainModel .getSnapshotInfo ()
277+                     }
278+                 }
201279            }
202280        }
203281    }
0 commit comments