@@ -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);
@@ -133,47 +175,68 @@ function onFileSelected(file) {
133175 currentPage ++ ;
134176 history . push ( { offset : currentOffset , linesDisplayed : nextOffset . linesDisplayed } ) ;
135177 displayText ( currentOffset , currentPage ) ;
178+ savePagePosition ( file , currentOffset , currentPage ) ;
136179 } else {
137180 currentOffset = 0 ;
138181 currentPage = 1 ;
139182 let result = displayText ( currentOffset , currentPage ) ;
140183 history = [ { offset : currentOffset , linesDisplayed : result . linesDisplayed } ] ;
184+ savePagePosition ( file , currentOffset , currentPage ) ;
141185 }
142186 }
143187
144188 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- }
189+ if ( currentPage > 1 && history . length > 1 ) {
190+ history . pop ( ) ;
191+ var previousPage = history [ history . length - 1 ] ;
192+ currentOffset = previousPage . offset ;
193+ currentPage -- ;
194+ displayText ( currentOffset , currentPage ) ;
195+ savePagePosition ( file , currentOffset , currentPage ) ;
196+ }
197+ // It may be possible to elegantly go back beyond the first saved offset but this is a problem for future me
152198 }
153199
154200 function zoom ( ) {
155201 g . clear ( ) ;
156202 big = ! big ;
203+ savePagePosition ( file , currentOffset , currentPage ) ;
157204 firstDraw ( ) ;
158205 }
159-
206+
207+ function goToBeginning ( ) {
208+ E . showPrompt ( "Return to beginning?" , {
209+ title : "Confirm" ,
210+ buttons : { "Yes" : true , "No" : false }
211+ } ) . then ( function ( confirm ) {
212+ if ( confirm ) {
213+ currentOffset = 0 ;
214+ currentPage = 1 ;
215+ history = [ ] ;
216+ var result = displayText ( currentOffset , currentPage ) ;
217+ history . push ( { offset : currentOffset , linesDisplayed : result . linesDisplayed } ) ;
218+ savePagePosition ( file , currentOffset , currentPage ) ;
219+ } else {
220+ displayText ( currentOffset , currentPage ) ;
221+ }
222+ } ) ;
223+ }
224+
160225 function firstDraw ( ) {
161- currentOffset = 0 ;
162- currentPage = 1 ;
163226 history = [ ] ;
164-
165- // Initial display
166227 var result = displayText ( currentOffset , currentPage ) ;
167228 history . push ( { offset : currentOffset , linesDisplayed : result . linesDisplayed } ) ;
229+ savePagePosition ( file , currentOffset , currentPage ) ;
168230 }
169-
231+
170232 ui . init ( ) ;
171233 ui . prevScreen = prevPage ;
172234 ui . nextScreen = nextPage ;
173235 ui . topLeft = zoom ;
236+ ui . topRight = goToBeginning ; // Assign the new function to topRight
174237 firstDraw ( ) ;
175-
238+
176239 Bangle . on ( "drag" , ( b ) => ui . touchHandler ( b ) ) ;
177240}
178241
179- showFileSelector ( ) ;
242+ showFileSelector ( ) ;
0 commit comments