@@ -27,12 +27,12 @@ let ui = {
2727 touchHandler : function ( d ) {
2828 let x = Math . floor ( d . x ) ;
2929 let y = Math . floor ( d . y ) ;
30-
30+
3131 if ( d . b != 1 || this . last_b != 0 ) {
3232 this . last_b = d . b ;
3333 return ;
3434 }
35-
35+
3636 print ( "touch" , x , y , this . h , this . w ) ;
3737
3838 if ( ( x < this . w / 2 ) && ( y < this . y2 / 2 ) )
@@ -56,6 +56,41 @@ let ui = {
5656
5757ui . init ( ) ;
5858
59+ function savePagePosition ( file , offset , page ) {
60+ let config = { } ;
61+ try {
62+ let configData = require ( "Storage" ) . read ( "txtreader.config.json" ) ;
63+ if ( configData ) {
64+ config = JSON . parse ( configData ) ;
65+ }
66+ } catch ( e ) {
67+ config = { } ;
68+ }
69+
70+ config [ file ] = {
71+ offset : offset ,
72+ page : page ,
73+ timestamp : Date . now ( )
74+ } ;
75+
76+ require ( "Storage" ) . write ( "txtreader.config.json" , JSON . stringify ( config ) ) ;
77+ }
78+
79+ function loadPagePosition ( file ) {
80+ try {
81+ let configData = require ( "Storage" ) . read ( "txtreader.config.json" ) ;
82+ if ( configData ) {
83+ let config = JSON . parse ( configData ) ;
84+ if ( config [ file ] ) {
85+ return config [ file ] ;
86+ }
87+ }
88+ } catch ( e ) {
89+ print ( "No config found or invalid config" ) ;
90+ }
91+ return null ;
92+ }
93+
5994function showFileSelector ( ) {
6095 let files = require ( "Storage" ) . list ( ) . filter ( f => f . endsWith ( '.txt' ) ) ;
6196
@@ -84,6 +119,13 @@ function onFileSelected(file) {
84119 let currentPage = 1 ;
85120 let history = [ ] ;
86121
122+ let savedPosition = loadPagePosition ( file ) ;
123+ if ( savedPosition ) {
124+ currentOffset = savedPosition . offset ;
125+ currentPage = savedPosition . page ;
126+ print ( `Loading saved position: page ${ currentPage } , offset ${ currentOffset } ` ) ;
127+ }
128+
87129 function displayText ( offset , pageNumber ) {
88130 let border = 10 ;
89131 let char_h = 10 ;
@@ -96,7 +138,7 @@ function onFileSelected(file) {
96138 }
97139 char_h = g . getFontHeight ( ) ;
98140 char_w = g . stringWidth ( "w" ) ;
99-
141+
100142 g . setColor ( g . theme . fg ) ;
101143 g . drawString ( "Page " + pageNumber , border , 2 ) ;
102144 //g.drawString("Offset " + offset, 60, 2);
@@ -142,38 +184,58 @@ function onFileSelected(file) {
142184 }
143185
144186 function prevPage ( ) {
145- if ( currentPage > 1 ) {
146- history . pop ( ) ; // Remove current page from history
147- var previousPage = history [ history . length - 1 ] ;
148- currentOffset = previousPage . offset ;
149- currentPage -- ;
150- displayText ( currentOffset , currentPage ) ;
151- }
187+ if ( currentPage > 1 && history . length > 1 ) {
188+ history . pop ( ) ;
189+ var previousPage = history [ history . length - 1 ] ;
190+ currentOffset = previousPage . offset ;
191+ currentPage -- ;
192+ displayText ( currentOffset , currentPage ) ;
193+ }
194+ // It may be possible to elegantly go back beyond the first saved offset but this is a problem for future me
152195 }
153196
154197 function zoom ( ) {
155198 g . clear ( ) ;
156199 big = ! big ;
157200 firstDraw ( ) ;
158201 }
159-
202+
203+ function goToBeginning ( ) {
204+ E . showPrompt ( "Return to beginning?" , {
205+ title : "Confirm" ,
206+ buttons : { "Yes" : true , "No" : false }
207+ } ) . then ( function ( confirm ) {
208+ if ( confirm ) {
209+ currentOffset = 0 ;
210+ currentPage = 1 ;
211+ history = [ ] ;
212+ var result = displayText ( currentOffset , currentPage ) ;
213+ history . push ( { offset : currentOffset , linesDisplayed : result . linesDisplayed } ) ;
214+ } else {
215+ displayText ( currentOffset , currentPage ) ;
216+ }
217+ } ) ;
218+ }
219+
160220 function firstDraw ( ) {
161- currentOffset = 0 ;
162- currentPage = 1 ;
163221 history = [ ] ;
164-
165- // Initial display
166222 var result = displayText ( currentOffset , currentPage ) ;
167223 history . push ( { offset : currentOffset , linesDisplayed : result . linesDisplayed } ) ;
224+ savePagePosition ( file , currentOffset , currentPage ) ;
168225 }
169-
226+
170227 ui . init ( ) ;
171228 ui . prevScreen = prevPage ;
172229 ui . nextScreen = nextPage ;
173230 ui . topLeft = zoom ;
231+ ui . topRight = goToBeginning ; // Assign the new function to topRight
174232 firstDraw ( ) ;
175-
233+
176234 Bangle . on ( "drag" , ( b ) => ui . touchHandler ( b ) ) ;
235+
236+ E . on ( 'kill' , ( ) => {
237+ savePagePosition ( file , currentOffset , currentPage ) ;
238+ } ) ;
177239}
178240
179- showFileSelector ( ) ;
241+ showFileSelector ( ) ;
0 commit comments