add time to chron
This commit is contained in:
@@ -18,6 +18,11 @@ export function createTimestamp(): string {
|
|||||||
return new Date().toISOString();
|
return new Date().toISOString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function formatTime(date: Date): string {
|
||||||
|
const time = [date.getHours(), date.getMinutes()];
|
||||||
|
return time.join(":");
|
||||||
|
}
|
||||||
|
|
||||||
// filters
|
// filters
|
||||||
export type StringEntryObject = { [key: string]: Stringifiable | undefined };
|
export type StringEntryObject = { [key: string]: Stringifiable | undefined };
|
||||||
|
|
||||||
@@ -29,11 +34,11 @@ export function filterObjectsByStringEntries<T>(
|
|||||||
const matches: Set<T> = new Set();
|
const matches: Set<T> = new Set();
|
||||||
|
|
||||||
for (const object of objects) {
|
for (const object of objects) {
|
||||||
const doesMatch: boolean = checkDoesObjectMatchReference(
|
const doesMatch: boolean = checkDoesObjectMatchReference(
|
||||||
reference,
|
reference,
|
||||||
converter(object),
|
converter(object),
|
||||||
);
|
);
|
||||||
if (doesMatch) matches.add(object);
|
if (doesMatch) matches.add(object);
|
||||||
}
|
}
|
||||||
|
|
||||||
return matches;
|
return matches;
|
||||||
@@ -47,12 +52,12 @@ export function filterObjectsByWords<T>(
|
|||||||
const matches: Set<T> = new Set();
|
const matches: Set<T> = new Set();
|
||||||
|
|
||||||
for (const object of objects) {
|
for (const object of objects) {
|
||||||
const doesMatch: boolean = checkDoesObjectMatchSearch(
|
const doesMatch: boolean = checkDoesObjectMatchSearch(
|
||||||
query,
|
query,
|
||||||
getStringsOfObject,
|
getStringsOfObject,
|
||||||
object,
|
object,
|
||||||
);
|
);
|
||||||
if (doesMatch) matches.add(object);
|
if (doesMatch) matches.add(object);
|
||||||
}
|
}
|
||||||
|
|
||||||
return matches;
|
return matches;
|
||||||
@@ -68,30 +73,30 @@ export function checkDoesObjectMatchSearch<T>(
|
|||||||
const stringsInObject: string[] = getStringsOfObject(object);
|
const stringsInObject: string[] = getStringsOfObject(object);
|
||||||
const wordsInObject: string[] = [];
|
const wordsInObject: string[] = [];
|
||||||
for (const string of stringsInObject) {
|
for (const string of stringsInObject) {
|
||||||
const lowercaseWordsInString = string
|
const lowercaseWordsInString = string
|
||||||
.toString()
|
.toString()
|
||||||
.toLowerCase()
|
.toLowerCase()
|
||||||
.split(" ")
|
.split(" ")
|
||||||
.filter((word) => word != "");
|
.filter((word) => word != "");
|
||||||
wordsInObject.push(...lowercaseWordsInString);
|
wordsInObject.push(...lowercaseWordsInString);
|
||||||
}
|
}
|
||||||
|
|
||||||
const lowercaseWordsInQuery = query
|
const lowercaseWordsInQuery = query
|
||||||
.toLowerCase()
|
.toLowerCase()
|
||||||
.split(" ")
|
.split(" ")
|
||||||
.filter((word) => word != "");
|
.filter((word) => word != "");
|
||||||
for (const queryWord of lowercaseWordsInQuery) {
|
for (const queryWord of lowercaseWordsInQuery) {
|
||||||
if (queryWord[0] == "-") {
|
if (queryWord[0] == "-") {
|
||||||
// exclusion
|
// exclusion
|
||||||
const wordContent = queryWord.substring(1);
|
const wordContent = queryWord.substring(1);
|
||||||
if (wordsInObject.includes(wordContent)) {
|
if (wordsInObject.includes(wordContent)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (wordsInObject.includes(queryWord) == false) {
|
if (wordsInObject.includes(queryWord) == false) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@@ -104,14 +109,14 @@ export default function implementFilter<T>(
|
|||||||
itemToString: (item: T) => string,
|
itemToString: (item: T) => string,
|
||||||
) {
|
) {
|
||||||
function update() {
|
function update() {
|
||||||
matches.clear();
|
matches.clear();
|
||||||
allItems.value.forEach((item: T) => {
|
allItems.value.forEach((item: T) => {
|
||||||
if (!itemToString(item).toLowerCase().includes(query.value.toLowerCase())) return;
|
if (!itemToString(item).toLowerCase().includes(query.value.toLowerCase())) return;
|
||||||
matches.add(item);
|
matches.add(item);
|
||||||
allItems.handleRemoval(item, () => {
|
allItems.handleRemoval(item, () => {
|
||||||
matches.remove(item);
|
matches.remove(item);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
query.subscribe(update);
|
query.subscribe(update);
|
||||||
allItems.handleAddition(update);
|
allItems.handleAddition(update);
|
||||||
@@ -125,16 +130,16 @@ export class HandlerManager<T> {
|
|||||||
|
|
||||||
// manage
|
// manage
|
||||||
setHandler = (id: string, handler: Handler<T>): void => {
|
setHandler = (id: string, handler: Handler<T>): void => {
|
||||||
this.handlers.set(id, handler);
|
this.handlers.set(id, handler);
|
||||||
};
|
};
|
||||||
|
|
||||||
deleteHandler = (id: string): void => {
|
deleteHandler = (id: string): void => {
|
||||||
this.handlers.delete(id);
|
this.handlers.delete(id);
|
||||||
};
|
};
|
||||||
|
|
||||||
// trigger
|
// trigger
|
||||||
trigger = (item: T): void => {
|
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
|
// methods
|
||||||
update = (items: T[]): void => {
|
update = (items: T[]): void => {
|
||||||
this.sortedStrings = [];
|
this.sortedStrings = [];
|
||||||
|
|
||||||
let strings: string[] = [];
|
let strings: string[] = [];
|
||||||
for (const item of items) {
|
for (const item of items) {
|
||||||
const string: string = this.itemToString(item);
|
const string: string = this.itemToString(item);
|
||||||
strings.push(string);
|
strings.push(string);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.sortedStrings = strings.sort(localeCompare);
|
this.sortedStrings = strings.sort(localeCompare);
|
||||||
};
|
};
|
||||||
|
|
||||||
getIndex = (item: T): number => {
|
getIndex = (item: T): number => {
|
||||||
const string: string = this.itemToString(item);
|
const string: string = this.itemToString(item);
|
||||||
const index: number = this.sortedStrings.indexOf(string);
|
const index: number = this.sortedStrings.indexOf(string);
|
||||||
return index;
|
return index;
|
||||||
};
|
};
|
||||||
|
|
||||||
// init
|
// init
|
||||||
constructor(itemToString: (item: T) => string) {
|
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 {
|
export function parse(string: string): any {
|
||||||
try {
|
try {
|
||||||
return JSON.parse(string);
|
return JSON.parse(string);
|
||||||
} catch {
|
} catch {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -203,8 +208,8 @@ export function parseValidObject<T>(string: string, reference: T): T | null {
|
|||||||
if (checkIsValidObject(parsed) == false) return null;
|
if (checkIsValidObject(parsed) == false) return null;
|
||||||
|
|
||||||
const doesMatchReference: boolean = checkMatchesObjectStructure(
|
const doesMatchReference: boolean = checkMatchesObjectStructure(
|
||||||
parsed,
|
parsed,
|
||||||
reference,
|
reference,
|
||||||
);
|
);
|
||||||
if (doesMatchReference == false) return null;
|
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 {
|
export function parseOrFallback(inputString: string): any {
|
||||||
try {
|
try {
|
||||||
return JSON.parse(inputString);
|
return JSON.parse(inputString);
|
||||||
} catch {
|
} catch {
|
||||||
return inputString;
|
return inputString;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
@@ -1,7 +1,7 @@
|
|||||||
import { v4 } from "uuid";
|
import { v4 } from "uuid";
|
||||||
import * as React from "../../react";
|
import * as React from "../../react";
|
||||||
import { allTranslations, Translations } from "../../View/translations";
|
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 StorageModel from "../../Model/Global/storageModel";
|
||||||
import SettingsModel from "../../Model/Global/settingsModel";
|
import SettingsModel from "../../Model/Global/settingsModel";
|
||||||
import FileTransferModel from "../../Model/Global/fileTransferModel";
|
import FileTransferModel from "../../Model/Global/fileTransferModel";
|
||||||
@@ -73,19 +73,24 @@ export default class CoreViewModel {
|
|||||||
// CHRON
|
// CHRON
|
||||||
chronHandlerManager = new HandlerManager<void>();
|
chronHandlerManager = new HandlerManager<void>();
|
||||||
todayDate = new React.State<Date>(new Date());
|
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);
|
return new Date(this.todayDate.value);
|
||||||
}
|
}
|
||||||
|
get timeString(): string {
|
||||||
|
return this.time.value.join(":");
|
||||||
|
}
|
||||||
|
|
||||||
startChron = (): void => {
|
startChron = (): void => {
|
||||||
setInterval(this.chronHandlerManager.trigger, 2000);
|
setInterval(this.chronHandlerManager.trigger, 2000);
|
||||||
};
|
};
|
||||||
|
|
||||||
handleChron = (): void => {
|
handleChron = (): void => {
|
||||||
const newDate = new Date();
|
const newDate = new Date();
|
||||||
if (this.unwrappedTodayDate.toDateString() == newDate.toDateString())
|
if (this.unwrappedTodayDate.toDateString() != newDate.toDateString())
|
||||||
return;
|
this.todayDate.value = new Date();
|
||||||
this.todayDate.value = new Date();
|
if (formatTime(newDate) != this.timeString)
|
||||||
|
this.time.value = [newDate.getHours(), newDate.getMinutes()];
|
||||||
};
|
};
|
||||||
|
|
||||||
// DRAG & DROP
|
// DRAG & DROP
|
||||||
|
|||||||
Reference in New Issue
Block a user