Skip to content

Commit 5a1e893

Browse files
authored
Merge pull request espruino#4115 from tonykakuuu/master
Update txtreader to 0.03 - Ability to save pages
2 parents 2ff673f + 7cdeb16 commit 5a1e893

File tree

4 files changed

+88
-20
lines changed

4 files changed

+88
-20
lines changed

apps/txtreader/ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
0.01: New App!
22
0.02: Make font size selectable
3+
0.03: Save last read page and continue reading from there

apps/txtreader/README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,14 @@ Very basic text reader with an integrated file selector.
1313
Bangle.js 2
1414
- tap the bottom right corner of the screen to flip to the next page
1515
- tap the bottom left corner of the screen to flip to the previous page
16-
- tap the top left corner of the screen to toggle font size and go back to page 1
16+
- tap the top left corner of the screen to toggle font size
17+
- tap the top right corner of the screen to go back to the first page
1718
- exit by pressing the physical button
1819

20+
## TODO
21+
22+
- It is currently not possible to go backwards after continuing from a saved page. Instead, press the top right to go back to page one.
23+
1924
## Creator
2025

2126
<https://topkekker.rip/>

apps/txtreader/app.js

Lines changed: 80 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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

5757
ui.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+
5994
function 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();

apps/txtreader/metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"id": "txtreader",
33
"name": "txtreader",
44
"shortName": "txtreader",
5-
"version": "0.02",
5+
"version": "0.03",
66
"author": "tonykakuuu",
77
"description": "Basic text reader with pages and a file selector.",
88
"icon": "txtreader.png",

0 commit comments

Comments
 (0)