Switch prefs to dexie

pull/149/head
Philipp Heckel 2022-03-01 22:01:51 -05:00
parent 23d275acec
commit effc1f42eb
5 changed files with 61 additions and 44 deletions

View File

@ -10,7 +10,6 @@ class ConnectionManager {
return;
}
console.log(`[ConnectionManager] Refreshing connections`);
console.log(users);
const subscriptionIds = subscriptions.ids();
const deletedIds = Array.from(this.connections.keys()).filter(id => !subscriptionIds.includes(id));

View File

@ -1,5 +1,6 @@
import Subscription from "./Subscription";
import Subscriptions from "./Subscriptions";
import db from "./db";
class Repository {
loadSubscriptions() {
@ -41,33 +42,41 @@ class Repository {
localStorage.setItem('subscriptions', serialized);
}
loadSelectedSubscriptionId() {
console.log(`[Repository] Loading selected subscription ID from localStorage`);
const selectedSubscriptionId = localStorage.getItem('selectedSubscriptionId');
return (selectedSubscriptionId) ? selectedSubscriptionId : "";
async setSelectedSubscriptionId(selectedSubscriptionId) {
console.log(`[Repository] Saving selected subscription ${selectedSubscriptionId}`);
db.prefs.put({key: 'selectedSubscriptionId', value: selectedSubscriptionId});
}
saveSelectedSubscriptionId(selectedSubscriptionId) {
console.log(`[Repository] Saving selected subscription ${selectedSubscriptionId} to localStorage`);
localStorage.setItem('selectedSubscriptionId', selectedSubscriptionId);
async getSelectedSubscriptionId() {
console.log(`[Repository] Loading selected subscription ID`);
const selectedSubscriptionId = await db.prefs.get('selectedSubscriptionId');
return (selectedSubscriptionId) ? selectedSubscriptionId.value : "";
}
setMinPriority(minPriority) {
localStorage.setItem('minPriority', minPriority.toString());
async setMinPriority(minPriority) {
db.prefs.put({key: 'minPriority', value: minPriority.toString()});
}
getMinPriority() {
const minPriority = localStorage.getItem('minPriority');
return (minPriority) ? Number(minPriority) : 1;
async getMinPriority() {
const minPriority = await db.prefs.get('minPriority');
return (minPriority) ? Number(minPriority.value) : 1;
}
setDeleteAfter(deleteAfter) {
localStorage.setItem('deleteAfter', deleteAfter.toString());
minPriority() {
return db.prefs.get('minPriority');
}
getDeleteAfter() {
const deleteAfter = localStorage.getItem('deleteAfter');
return (deleteAfter) ? Number(deleteAfter) : 604800; // Default is one week
async setDeleteAfter(deleteAfter) {
db.prefs.put({key:'deleteAfter', value: deleteAfter.toString()});
}
async getDeleteAfter() {
const deleteAfter = await db.prefs.get('deleteAfter');
return (deleteAfter) ? Number(deleteAfter.value) : 604800; // Default is one week
}
deleteAfter() {
return db.prefs.get('deleteAfter');
}
}

View File

@ -9,7 +9,10 @@ import Dexie from 'dexie';
const db = new Dexie('ntfy');
db.version(1).stores({
users: '&baseUrl, username',
subscriptions: '&id',
notifications: '&id,subscriptionId',
users: '&baseUrl,username',
prefs: '&key'
});
export default db;

View File

@ -92,9 +92,10 @@ const App = () => {
// Define hooks: Note that the order of the hooks is important. The "loading" hooks
// must be before the "saving" hooks.
useEffect(() => {
const load = async () => {
// Load subscriptions
const subscriptions = repository.loadSubscriptions();
const selectedSubscriptionId = repository.loadSelectedSubscriptionId();
const selectedSubscriptionId = await repository.getSelectedSubscriptionId();
setSubscriptions(subscriptions);
// Set selected subscription
@ -107,6 +108,8 @@ const App = () => {
subscriptions.forEach((subscriptionId, subscription) => {
poll(subscription);
});
};
load();
}, [/* initial render */]);
useEffect(() => {
const notificationClickFallback = (subscription) => setSelectedSubscription(subscription);
@ -124,7 +127,7 @@ const App = () => {
useEffect(() => repository.saveSubscriptions(subscriptions), [subscriptions]);
useEffect(() => {
const subscriptionId = (selectedSubscription) ? selectedSubscription.id : "";
repository.saveSelectedSubscriptionId(subscriptionId)
repository.setSelectedSubscriptionId(subscriptionId)
}, [selectedSubscription]);
return (

View File

@ -45,7 +45,7 @@ const Preferences = (props) => {
);
};
const Notifications = (props) => {
const Notifications = () => {
return (
<Card sx={{p: 3}}>
<Typography variant="h5">
@ -60,10 +60,12 @@ const Notifications = (props) => {
};
const MinPriority = () => {
const [minPriority, setMinPriority] = useState(repository.getMinPriority());
const handleChange = (ev) => {
setMinPriority(ev.target.value);
repository.setMinPriority(ev.target.value);
const minPriority = useLiveQuery(() => repository.getMinPriority());
const handleChange = async (ev) => {
await repository.setMinPriority(ev.target.value);
}
if (!minPriority) {
return null; // While loading
}
return (
<Pref title="Minimum priority">
@ -81,10 +83,12 @@ const MinPriority = () => {
};
const DeleteAfter = () => {
const [deleteAfter, setDeleteAfter] = useState(repository.getDeleteAfter());
const handleChange = (ev) => {
setDeleteAfter(ev.target.value);
repository.setDeleteAfter(ev.target.value);
const deleteAfter = useLiveQuery(async () => repository.getDeleteAfter());
const handleChange = async (ev) => {
await repository.setDeleteAfter(ev.target.value);
}
if (!deleteAfter) {
return null; // While loading
}
return (
<Pref title="Delete notifications">
@ -101,7 +105,6 @@ const DeleteAfter = () => {
)
};
const PrefGroup = (props) => {
return (
<div style={{
@ -159,7 +162,7 @@ const DefaultServer = (props) => {
);
};
const Users = (props) => {
const Users = () => {
const [dialogKey, setDialogKey] = useState(0);
const [dialogOpen, setDialogOpen] = useState(false);
const users = useLiveQuery(() => db.users.toArray());