Skip to content

Commit

Permalink
PWA
Browse files Browse the repository at this point in the history
  • Loading branch information
strukturart committed Oct 9, 2024
1 parent dd3f6d7 commit c561ca6
Show file tree
Hide file tree
Showing 11 changed files with 8,970 additions and 4,089 deletions.
3 changes: 3 additions & 0 deletions application/assets/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,9 @@ div.nickname {

.audio-player .audio-info {
padding: 20px;
background: white;
color: black;
width: fit-content;
}
/*/ //////////////////////////
///VIEWS////////////////*/
Expand Down
59 changes: 51 additions & 8 deletions application/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ localforage
console.error("Error accessing localForage:", err);
});

let reload_data = () => {
articles = [];
side_toaster("load new content", 4000);
setTimeout(() => {
start_loading();
});
};

function add_read_article(id) {
// Add the article to the global array
let ids = [];
Expand Down Expand Up @@ -418,6 +426,8 @@ const fetchContent = async (feed_download_list) => {
}
};

let ids = [];

feed_download_list.forEach((e) => {
if (e.type === "mastodon") {
fetch(e.url)
Expand Down Expand Up @@ -518,7 +528,10 @@ const fetchContent = async (feed_download_list) => {
f.cover = f["media:thumbnail"]["@_url"];
}

articles.push(f);
if (!ids.includes(f.id)) {
articles.push(f);
ids.push(f.id); // Add the ID to the tracking array
}
} catch (e) {
console.log(e);
}
Expand Down Expand Up @@ -562,7 +575,10 @@ const fetchContent = async (feed_download_list) => {
f.cover = jObj.rss.channel.image.url || "";
}

articles.push(f);
if (!ids.includes(f.id)) {
articles.push(f);
ids.push(f.id); // Add the ID to the tracking array
}
} catch (e) {
console.log(e);
}
Expand Down Expand Up @@ -804,11 +820,6 @@ var options = {
},
};

const entries = window.performance.getEntriesByType("navigation");
if (entries.length && entries[0].type === "reload") {
m.route.set("/start");
}

let counter = -1;
let channel_filter = "";

Expand Down Expand Up @@ -848,7 +859,6 @@ var start = {
},
},
m("span", { class: "channel", oncreate: () => {} }, channel_filter),

// Loop through filteredArticles and create an article for each
filteredArticles.map((h, i) => {
const readClass = read_articles.includes(h.id) ? "read" : "";
Expand Down Expand Up @@ -2013,6 +2023,12 @@ document.addEventListener("DOMContentLoaded", function (e) {
let r = m.route.get();

let dir = e.detail.dir;

side_toaster(e.detail.fingers, 3000);

if (dir == "down") {
reload_data();
}
if (dir == "right") {
if (r.startsWith("/start")) {
counter--;
Expand Down Expand Up @@ -2312,6 +2328,33 @@ window.addEventListener("offline", () => {
status.deviceOnline = false;
});

window.addEventListener("beforeunload", (event) => {
const entries = window.performance.getEntriesByType("navigation");

// For older browsers (fallback)
const navigationType = window.performance.navigation
? window.performance.navigation.type
: null;

// Detect if the page was reloaded
const isReload =
(entries.length && entries[0].type === "reload") || // Modern check
navigationType === 1; // Fallback for older browsers: 1 means reload

if (isReload) {
// Prevent the reload or display a confirmation dialog
event.preventDefault();

articles = [];
side_toaster("load new content", 4000);
start_loading();

m.route.set("/intro");
// Most browsers ignore custom messages, so returnValue should be set
event.returnValue = "Are you sure you want to leave the page?";
}
});

//webActivity KaiOS 3

if ("serviceWorker" in navigator) {
Expand Down
55 changes: 27 additions & 28 deletions application/sw.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { status } from "./index.js";
const userAgent = navigator.userAgent || "";

const channel = new BroadcastChannel("sw-messages");

Expand Down Expand Up @@ -35,36 +35,37 @@ self.onsystemmessage = (evt) => {
evt.waitUntil(serviceHandler());
} catch (e) {}
};

if (status.notKaiOS) {
const CACHE_NAME = "pwa-cache-v0.1040";
const urlsToCache = [
"/assets/icons/link.svg",
"/assets/icons/option.svg",
"/assets/icons/back.svg",
"/assets/icons/select.svg",
"/assets/icons/play.svg",
"/assets/fonts/Roboto-Regular.ttf",
"/Roboto-Regular.31363ab6.ttf",
"/manifest.webmanifest",
"/index.html",
"/icon-112-112.d699dfa7.png",
"/icon-56-56.9b02d039.png",
"/favicon.e23550e2.ico",
"/index.0bb9b423.css",
"/index.4149820a.css",
"/index.ecd1e59e.css",
"/index.175d5a46.js",
"index.f35ba7fe.js",
"index.runtime.2f02f579.js",
"index.runtime.ca873bcc.js",
];
if (!userAgent.includes("KaiOS")) {
const CACHE_NAME = "pwa-cache-v0.1054";
const FILE_LIST_URL = "/file-list.json"; // URL of the JSON file containing the array of files

self.addEventListener("install", (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => {
console.log("Opened cache");
return cache.addAll(urlsToCache);

// Fetch the file list JSON and cache the URLs
return fetch(FILE_LIST_URL)
.then((response) => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.json(); // Parse the JSON response
})
.then((urlsToCache) => {
// Ensure urlsToCache is an array
if (Array.isArray(urlsToCache)) {
return Promise.all(
urlsToCache.map((url) =>
cache.add(url).catch((error) => {
console.error(`Failed to cache ${url}:`, error);
})
)
);
} else {
console.error("Fetched data is not an array:", urlsToCache);
}
});
})
);
});
Expand All @@ -88,8 +89,6 @@ if (status.notKaiOS) {
self.addEventListener("fetch", (event) => {
event.respondWith(
caches.match(event.request).then((response) => {
if (response) {
}
// If the request is in the cache, return it. Otherwise, fetch from the network.
return response || fetch(event.request);
})
Expand Down
77 changes: 77 additions & 0 deletions docs/file-list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
[
"/index.html",
"/index.63982eba.css",
"/index.3b283937.css",
"/index.5671dcd7.css",
"/manifest.webapp",
"/index.339e55c4.js",
"/icon-56-56.bda5227e.png",
"/Roboto-Regular.4f1a9903.ttf",
"/index.8d33ff60.js",
"/icon-112-112.185b7f3e.png",
"/index.runtime.8c6e9fd2.js",
"/file-list.json",
"/index.runtime.e9579c13.js",
"/sw.js",
"/index.f0337e49.js",
"/favicon.3da022ef.ico",
"/manifest.webmanifest",
"/assets/icons/save.svg",
"/assets/icons/intro.svg",
"/assets/icons/2605.svg",
"/assets/icons/add.svg",
"/assets/icons/E269.svg",
"/assets/icons/E1D8.svg",
"/assets/icons/call.svg",
"/assets/icons/option.svg",
"/assets/icons/record.svg",
"/assets/icons/intro.png",
"/assets/icons/icon-112-112.png",
"/assets/icons/plus.svg",
"/assets/icons/delete.svg",
"/assets/icons/2-5.svg",
"/assets/icons/online.svg",
"/assets/icons/ghost.svg",
"/assets/icons/reblog.svg",
"/assets/icons/icon-56-56.png",
"/assets/icons/E252.svg",
"/assets/icons/E25C.svg",
"/assets/icons/logo.svg",
"/assets/icons/person.svg",
"/assets/icons/no-monster.svg",
"/assets/icons/select.svg",
"/assets/icons/bell.svg",
"/assets/icons/qr.svg",
"/assets/icons/23EF.svg",
"/assets/icons/E261.svg",
"/assets/icons/record-live.svg",
"/assets/icons/25B6.svg",
"/assets/icons/cancel.svg",
"/assets/icons/minus.svg",
"/assets/icons/eye.svg",
"/assets/icons/icon-56-56.svg",
"/assets/icons/favicon.ico",
"/assets/icons/link.svg",
"/assets/icons/picture.svg",
"/assets/icons/youtube.png",
"/assets/icons/back.svg",
"/assets/icons/diamond.svg",
"/assets/icons/2B07.svg",
"/assets/icons/E253.svg",
"/assets/icons/image.svg",
"/assets/icons/monster.svg",
"/assets/icons/send.svg",
"/assets/icons/E24F.svg",
"/assets/icons/clock.svg",
"/assets/icons/pencil.svg",
"/assets/icons/play.svg",
"/assets/icons/cute-monster.png",
"/assets/icons/replies.svg",
"/assets/icons/podcast.png",
"/assets/icons/list.svg",
"/assets/icons/id.svg",
"/assets/icons/E264.svg",
"/assets/icons/offline.svg",
"/assets/fonts/Roboto-Regular.ttf",
"/assets/js/kaiads.v5.min.js"
]
6 changes: 3 additions & 3 deletions docs/index.339e55c4.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/index.63982eba.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/index.f0337e49.js

Large diffs are not rendered by default.

14 changes: 1 addition & 13 deletions docs/sw.js

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions generate_file_list.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash

# Set the output JSON file name
OUTPUT_FILE="./docs/file-list.json"
DOCS_DIR="./docs"

# Start the JSON array
echo "[" > "$OUTPUT_FILE"

# Find all files in the docs directory and its subdirectories
# Exclude directories and format the output as JSON
find "$DOCS_DIR" -type f | while read -r file; do
# Replace the leading "./docs/" with "" to keep the relative path for the JSON file
relative_path=${file#"$DOCS_DIR/"}

# Escape double quotes and backslashes in file names for JSON format
escaped_file=$(printf '%s\n' "$relative_path" | sed 's/\\/\\\\/g; s/"/\\"/g')

echo "\"/$escaped_file\"," >> "$OUTPUT_FILE"
done

# Remove the trailing comma from the last line
sed -i '$ s/,$//' "$OUTPUT_FILE"

# Close the JSON array
echo "]" >> "$OUTPUT_FILE"

echo "File list generated: $OUTPUT_FILE"

Loading

0 comments on commit c561ca6

Please sign in to comment.