forked from Blendman974/faceclaw
Continued UI redesign
This commit is contained in:
1 parent
68ab599856
commit
5f82981b92
20 files changed
+872
-161
No files matched your search
@@ -0,0 +1,91 @@
|
||||
/**
|
||||
* Filesystem access for the on-glasses file browser. Arbitrary files outside
|
||||
* app-specific directories require the "All files access" special permission
|
||||
* on modern Android (MANAGE_EXTERNAL_STORAGE, granted via a Settings page,
|
||||
* reasonable for a sideloaded personal tool).
|
||||
*/
|
||||
import { Utils } from "@nativescript/core";
|
||||
|
||||
declare const android: any;
|
||||
declare const java: any;
|
||||
declare const global: any;
|
||||
|
||||
const MAX_TEXT_FILE_BYTES = 2_000_000;
|
||||
const MAX_TEXT_CHARS = 500_000;
|
||||
|
||||
export type DirectoryEntry = {
|
||||
name: string;
|
||||
path: string;
|
||||
isDirectory: boolean;
|
||||
sizeBytes: number;
|
||||
};
|
||||
|
||||
export function hasAllFilesAccess(): boolean {
|
||||
if (!global.isAndroid) return false;
|
||||
try {
|
||||
return Boolean(android.os.Environment.isExternalStorageManager());
|
||||
} catch {
|
||||
// Pre-R devices have no such concept; the legacy permission suffices.
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/** Open the system Settings page where the user grants All files access. */
|
||||
export function requestAllFilesAccess(): void {
|
||||
const context = Utils.android.getApplicationContext();
|
||||
const intent = new android.content.Intent(
|
||||
android.provider.Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION,
|
||||
android.net.Uri.parse(`package:${context.getPackageName()}`),
|
||||
);
|
||||
intent.addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
context.startActivity(intent);
|
||||
}
|
||||
|
||||
export function externalStorageRootPath(): string {
|
||||
return String(android.os.Environment.getExternalStorageDirectory().getAbsolutePath());
|
||||
}
|
||||
|
||||
/** List a directory, directories first then by name; null when unreadable. */
|
||||
export function listDirectory(path: string): DirectoryEntry[] | null {
|
||||
try {
|
||||
const dir = new java.io.File(path);
|
||||
if (!dir.isDirectory()) return null;
|
||||
const files = dir.listFiles();
|
||||
if (files === null) return null;
|
||||
const entries: DirectoryEntry[] = [];
|
||||
for (let index = 0; index < files.length; index++) {
|
||||
const file = files[index];
|
||||
entries.push({
|
||||
name: String(file.getName()),
|
||||
path: String(file.getAbsolutePath()),
|
||||
isDirectory: Boolean(file.isDirectory()),
|
||||
sizeBytes: Number(file.length()),
|
||||
});
|
||||
}
|
||||
entries.sort((a, b) => {
|
||||
if (a.isDirectory !== b.isDirectory) return a.isDirectory ? -1 : 1;
|
||||
return a.name.toLowerCase() < b.name.toLowerCase() ? -1 : 1;
|
||||
});
|
||||
return entries;
|
||||
} catch (error) {
|
||||
console.warn(`listDirectory failed for ${path}: ${error}`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** Read a UTF-8 text file (size-capped); null when unreadable or too large. */
|
||||
export function readTextFile(path: string): string | null {
|
||||
try {
|
||||
const file = new java.io.File(path);
|
||||
if (!file.isFile() || file.length() > MAX_TEXT_FILE_BYTES) return null;
|
||||
// File.toPath() rather than Paths.get(path): NativeScript resolves the
|
||||
// single-string Paths.get call to the (java.net.URI) overload, which
|
||||
// aborts in JNI.
|
||||
const bytes = java.nio.file.Files.readAllBytes(file.toPath());
|
||||
const text = String(new java.lang.String(bytes, "UTF-8"));
|
||||
return text.length > MAX_TEXT_CHARS ? text.slice(0, MAX_TEXT_CHARS) : text;
|
||||
} catch (error) {
|
||||
console.warn(`readTextFile failed for ${path}: ${error}`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -121,7 +121,9 @@ const DEFAULT_NIGHTSCOUT_STATE: NightscoutState = {
|
||||
boluses: [],
|
||||
basal: [],
|
||||
lastUpdatedMs: null,
|
||||
configurationMissing: true,
|
||||
// Never assume unconfigured before a refresh has actually checked; UI that
|
||||
// needs a live answer uses isNightscoutSettingsConfigured().
|
||||
configurationMissing: false,
|
||||
};
|
||||
|
||||
export class NightscoutBridge {
|
||||
@@ -261,10 +263,15 @@ export class NightscoutBridge {
|
||||
this.emit();
|
||||
} catch (error) {
|
||||
const message = (error as Error)?.message ?? String(error);
|
||||
console.warn(`nightscout refresh failed: ${message}`);
|
||||
this.state = {
|
||||
...this.state,
|
||||
available: this.state.latest !== null,
|
||||
status: `Nightscout refresh failed: ${message}`,
|
||||
// A fetch failure is not a configuration problem: the settings guard
|
||||
// above already handled missing config, so a latched stale value here
|
||||
// must not keep the UI on "Needs setup" (which hides the real error).
|
||||
configurationMissing: false,
|
||||
};
|
||||
this.emit();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user