scriptable-testlab
Version:
A lightweight, efficient tool designed to manage and update scripts for Scriptable.
121 lines (105 loc) • 3.09 kB
text/typescript
import {AbsUITable} from 'scriptable-abstract';
import {MockUITableRow} from './ui-table-row';
interface UITableState {
showSeparators: boolean;
rows: MockUITableRow[];
isPresented: boolean;
isFullscreen: boolean;
}
/**
* Mock implementation of Scriptable's UITable
* Used to display data in a table view
*/
export class MockUITable extends AbsUITable<UITableState> {
constructor() {
super({
showSeparators: true, // Default iOS behavior: show separators
rows: [],
isPresented: false,
isFullscreen: false,
});
}
/**
* Whether to show separators between rows
*/
get showSeparators(): boolean {
return this.state.showSeparators;
}
/**
* Sets whether to show separators between rows
*/
set showSeparators(value: boolean) {
if (typeof value !== 'boolean') {
throw new Error('ShowSeparators must be a boolean');
}
this.setState({showSeparators: value});
}
/**
* Adds a row to the table
* @param row Row to add to the table. If not provided, a new row will be created.
* @returns The added row
*/
addRow(row?: MockUITableRow): MockUITableRow {
const newRow = row || new MockUITableRow();
this.setState({rows: [...this.state.rows, newRow]});
return newRow;
}
/**
* Removes a specific row from the table
* @param row Row to remove from the table
*/
removeRow(row: MockUITableRow): void {
// Prevent row removal while table is being presented
if (this.state.isPresented) {
throw new Error('Cannot remove rows while table is being presented');
}
const index = this.state.rows.indexOf(row);
if (index === -1) {
throw new Error('Row not found in table');
}
const rows = [...this.state.rows];
rows.splice(index, 1);
this.setState({rows});
}
/**
* Removes all rows from the table
*/
removeAllRows(): void {
// Prevent row removal while table is being presented
if (this.state.isPresented) {
throw new Error('Cannot remove rows while table is being presented');
}
this.setState({rows: []});
}
/**
* Reloads the table view
* Must be called after modifying rows while the table is being presented
*/
reload(): void {
// In a real environment, this would trigger a UI refresh
// In mock environment, we just ensure state consistency
this.setState({...this.state});
}
/**
* Presents the table modally
* @param fullscreen Whether to present in fullscreen mode. Only has effect when used within the app.
*/
async present(fullscreen = false): Promise<void> {
// Prevent presenting an already presented table
if (this.state.isPresented) {
throw new Error('Table is already being presented');
}
// Simulate table presentation
this.setState({
isPresented: true,
isFullscreen: fullscreen,
});
// Simulate async operation
await new Promise(resolve => setTimeout(resolve, 0));
// After table is dismissed
this.setState({
isPresented: false,
isFullscreen: false,
});
}
}