about summary refs log tree commit diff stats
path: root/src/app.ts
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/app.ts58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/app.ts b/src/app.ts
index 9da16c0..a446ea3 100644
--- a/src/app.ts
+++ b/src/app.ts
@@ -254,6 +254,10 @@ class AppClass extends AppCommon {
             this.removeAll();
         });
 
+        $(".remove_read_downloads").on('click', () => {
+            this.removeRead();
+        });
+
         $(".logout_link").on('click', () => {
             this.logout();
         });
@@ -764,6 +768,60 @@ class AppClass extends AppCommon {
         }
     }
 
+    /**
+     * Remove downloaded books whose stored reading progress marks them as read
+     * (same heuristic as the rendered list: fewer than 5 pages left).
+     */
+    async removeRead(): Promise<void> {
+        if (!confirm("Remove downloaded books marked as read?")) {
+            return;
+        }
+
+        const suffixes = ["", ".book", ".cover", ".locations", ".lastread"];
+        const promises: Promise<void>[] = [];
+        const keysToRemove: string[] = [];
+
+        // Gather all individual asynchronous check operations
+        await localforage.iterate((value: unknown, key: string) => {
+            const match = key.match(/^epube-book\.(\d+)$/);
+
+            if (!match) {
+                return;
+            }
+
+            const cacheId = match[0];
+
+            // Push the pending getItem promise into our tracker array
+            const checkPromise = localforage.getItem(cacheId + ".lastread").then((raw) => {
+                if (!raw) {
+                    return;
+                }
+
+                const lastread = (typeof raw === "string" ? JSON.parse(raw) : raw) as LastRead;
+                const isRead = lastread.total > 0 && lastread.total - lastread.page < 5;
+
+                if (isRead) {
+                    console.log("offline remove read: " + cacheId);
+                    for (const suffix of suffixes) {
+                        keysToRemove.push(cacheId + suffix);
+                    }
+                }
+            });
+
+            promises.push(checkPromise);
+        });
+
+        // Wait for ALL database lookups to completely finish evaluation
+        await Promise.all(promises);
+
+        // Perform the actual removal of the compiled keys
+        if (keysToRemove.length > 0) {
+            await Promise.all(keysToRemove.map((key) => localforage.removeItem(key)));
+        }
+
+        this.populateOfflineList();
+    }
+
    populateOfflineList(): void {
         let query = this.urlParam("query");
         if (query) query = String(query).toLowerCase();