add time to chron

This commit is contained in:
marlon-erler
2026-09-25 19:05:36 +02:00
parent 78f8ff9943
commit 58d64858db
3 changed files with 75 additions and 65 deletions
+64 -59
View File
@@ -18,6 +18,11 @@ export function createTimestamp(): string {
return new Date().toISOString();
}
export function formatTime(date: Date): string {
const time = [date.getHours(), date.getMinutes()];
return time.join(":");
}
// filters
export type StringEntryObject = { [key: string]: Stringifiable | undefined };
@@ -29,11 +34,11 @@ export function filterObjectsByStringEntries<T>(
const matches: Set<T> = new Set();
for (const object of objects) {
const doesMatch: boolean = checkDoesObjectMatchReference(
reference,
converter(object),
);
if (doesMatch) matches.add(object);
const doesMatch: boolean = checkDoesObjectMatchReference(
reference,
converter(object),
);
if (doesMatch) matches.add(object);
}
return matches;
@@ -47,12 +52,12 @@ export function filterObjectsByWords<T>(
const matches: Set<T> = new Set();
for (const object of objects) {
const doesMatch: boolean = checkDoesObjectMatchSearch(
query,
getStringsOfObject,
object,
);
if (doesMatch) matches.add(object);
const doesMatch: boolean = checkDoesObjectMatchSearch(
query,
getStringsOfObject,
object,
);
if (doesMatch) matches.add(object);
}
return matches;
@@ -68,30 +73,30 @@ export function checkDoesObjectMatchSearch<T>(
const stringsInObject: string[] = getStringsOfObject(object);
const wordsInObject: string[] = [];
for (const string of stringsInObject) {
const lowercaseWordsInString = string
.toString()
.toLowerCase()
.split(" ")
.filter((word) => word != "");
wordsInObject.push(...lowercaseWordsInString);
const lowercaseWordsInString = string
.toString()
.toLowerCase()
.split(" ")
.filter((word) => word != "");
wordsInObject.push(...lowercaseWordsInString);
}
const lowercaseWordsInQuery = query
.toLowerCase()
.split(" ")
.filter((word) => word != "");
.toLowerCase()
.split(" ")
.filter((word) => word != "");
for (const queryWord of lowercaseWordsInQuery) {
if (queryWord[0] == "-") {
// exclusion
const wordContent = queryWord.substring(1);
if (wordsInObject.includes(wordContent)) {
return false;
}
} else {
if (wordsInObject.includes(queryWord) == false) {
return false;
}
}
if (queryWord[0] == "-") {
// exclusion
const wordContent = queryWord.substring(1);
if (wordsInObject.includes(wordContent)) {
return false;
}
} else {
if (wordsInObject.includes(queryWord) == false) {
return false;
}
}
}
return true;
@@ -104,14 +109,14 @@ export default function implementFilter<T>(
itemToString: (item: T) => string,
) {
function update() {
matches.clear();
allItems.value.forEach((item: T) => {
if (!itemToString(item).toLowerCase().includes(query.value.toLowerCase())) return;
matches.add(item);
allItems.handleRemoval(item, () => {
matches.remove(item);
});
});
matches.clear();
allItems.value.forEach((item: T) => {
if (!itemToString(item).toLowerCase().includes(query.value.toLowerCase())) return;
matches.add(item);
allItems.handleRemoval(item, () => {
matches.remove(item);
});
});
}
query.subscribe(update);
allItems.handleAddition(update);
@@ -125,16 +130,16 @@ export class HandlerManager<T> {
// manage
setHandler = (id: string, handler: Handler<T>): void => {
this.handlers.set(id, handler);
this.handlers.set(id, handler);
};
deleteHandler = (id: string): void => {
this.handlers.delete(id);
this.handlers.delete(id);
};
// trigger
trigger = (item: T): void => {
[...this.handlers.values()].forEach((handler) => handler(item));
[...this.handlers.values()].forEach((handler) => handler(item));
};
}
@@ -146,26 +151,26 @@ export class IndexManager<T> {
// methods
update = (items: T[]): void => {
this.sortedStrings = [];
this.sortedStrings = [];
let strings: string[] = [];
for (const item of items) {
const string: string = this.itemToString(item);
strings.push(string);
}
let strings: string[] = [];
for (const item of items) {
const string: string = this.itemToString(item);
strings.push(string);
}
this.sortedStrings = strings.sort(localeCompare);
this.sortedStrings = strings.sort(localeCompare);
};
getIndex = (item: T): number => {
const string: string = this.itemToString(item);
const index: number = this.sortedStrings.indexOf(string);
return index;
const string: string = this.itemToString(item);
const index: number = this.sortedStrings.indexOf(string);
return index;
};
// init
constructor(itemToString: (item: T) => string) {
this.itemToString = itemToString;
this.itemToString = itemToString;
}
}
@@ -192,9 +197,9 @@ export function padZero(string: string | undefined, length: number): string {
export function parse(string: string): any {
try {
return JSON.parse(string);
return JSON.parse(string);
} catch {
return {};
return {};
}
}
@@ -203,8 +208,8 @@ export function parseValidObject<T>(string: string, reference: T): T | null {
if (checkIsValidObject(parsed) == false) return null;
const doesMatchReference: boolean = checkMatchesObjectStructure(
parsed,
reference,
parsed,
reference,
);
if (doesMatchReference == false) return null;
@@ -213,9 +218,9 @@ export function parseValidObject<T>(string: string, reference: T): T | null {
export function parseOrFallback(inputString: string): any {
try {
return JSON.parse(inputString);
return JSON.parse(inputString);
} catch {
return inputString;
return inputString;
}
}
Binary file not shown.
+11 -6
View File
@@ -1,7 +1,7 @@
import { v4 } from "uuid";
import * as React from "../../react";
import { allTranslations, Translations } from "../../View/translations";
import { HandlerManager } from "../../Model/Utility/utility";
import { formatTime, HandlerManager } from "../../Model/Utility/utility";
import StorageModel from "../../Model/Global/storageModel";
import SettingsModel from "../../Model/Global/settingsModel";
import FileTransferModel from "../../Model/Global/fileTransferModel";
@@ -73,19 +73,24 @@ export default class CoreViewModel {
// CHRON
chronHandlerManager = new HandlerManager<void>();
todayDate = new React.State<Date>(new Date());
get unwrappedTodayDate() {
time = new React.State<[number, number]>([0, 0]);
get unwrappedTodayDate(): Date {
return new Date(this.todayDate.value);
}
get timeString(): string {
return this.time.value.join(":");
}
startChron = (): void => {
setInterval(this.chronHandlerManager.trigger, 2000);
};
handleChron = (): void => {
const newDate = new Date();
if (this.unwrappedTodayDate.toDateString() == newDate.toDateString())
return;
this.todayDate.value = new Date();
const newDate = new Date();
if (this.unwrappedTodayDate.toDateString() != newDate.toDateString())
this.todayDate.value = new Date();
if (formatTime(newDate) != this.timeString)
this.time.value = [newDate.getHours(), newDate.getMinutes()];
};
// DRAG & DROP