Skip to content

Commit

Permalink
update js api
Browse files Browse the repository at this point in the history
  • Loading branch information
DerGoogler committed Jan 26, 2025
1 parent 8af3037 commit a3a48cd
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,17 @@ class VersionInterface(
}
}

@JavascriptInterface
fun setGravity(gravity: Int, xOffset: Int, yOffset: Int) {
toast.setGravity(gravity, xOffset, yOffset)
}

@JavascriptInterface
fun setDuration(duration: Int) {
toast.duration = duration
}
}

@JavascriptInterface
fun toastBuilder(): ToastBuilderInterface {
return ToastBuilderInterface()
Expand Down
28 changes: 28 additions & 0 deletions js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ This is a JavaScript library designed to provide an interface for interacting wi
- [MMRLObjectAccessor](#mmrlobjectaccessor)
- [MMRLInterface](#mmrlinterface)
- [FileSystem](#filesystem)
- [VersionInterface](#versioninterface)
- [Toast](#toast)

## Installation

Expand Down Expand Up @@ -80,4 +82,30 @@ fs.write("example.txt", "Hello, MMRL!");
const content = fs.read("example.txt");
console.log(content);
fs.delete("example.txt");
```

### VersionInterface

The `VersionInterface` class provides access to version information about the MMRL environment. It includes properties for application and root configuration details.

Example usage:

```typescript
import { mmrl } from "mmrl";

console.log(mmrl.app.versionName);
console.log(mmrl.root.platform);
```

### Toast

The `Toast` class provides methods for creating and displaying native toast notifications within the MMRL environment. It includes methods for setting text, duration, gravity, and showing or canceling the toast.

Example usage:

```typescript
import { Toast } from "mmrl";

const toast = Toast.makeText("Hello, MMRL!", Toast.LENGTH_LONG);
toast.show();
```
2 changes: 1 addition & 1 deletion js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mmrl",
"version": "0.1.17",
"version": "0.2.17",
"description": "A library to make your life easier when working with MMRL's WebUI",
"source": "src/index.ts",
"main": "dist/index.cjs.js",
Expand Down
143 changes: 137 additions & 6 deletions js/src/classes/Toast.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,142 @@
import { NativeMethod } from "../decorators/NativeMethod";
import { ToastImpl } from "../types";
import { MMRLObjectAccessor } from "./MMRLObjectAccessor";

interface ToastImpl {
const requireVersion = 33049;

}
/**
* Class representing a native Toast notification.
* @extends MMRLObjectAccessor
*/
export class Toast extends MMRLObjectAccessor<MMRL> {
/**
* @private
* @readonly
* @type {ToastImpl}
*/
private readonly toastBuilder: ToastImpl;

/**
* Short duration for the toast.
* @type {number}
* @readonly
*/
public static readonly LENGTH_SHORT: number = 0;

/**
* Long duration for the toast.
* @type {number}
* @readonly
*/
public static readonly LENGTH_LONG: number = 1;

/**
* Duration of the toast.
* @private
* @type {number}
*/
private duration: number = Toast.LENGTH_SHORT;

/**
* Creates an instance of Toast.
*/
public constructor() {
super(window["mmrl"] as object);
this.toastBuilder = this.interface.toastBuilder.bind(this.interface)();
}

/**
* Sets the text for the toast.
* @param {string} text - The text to display. @throws If text is not a string.
* @requires MMRL version `33049` or higher.
*/
@NativeMethod({ requireVersion })
public setText(text: string): void {
if (typeof text !== "string") {
throw new TypeError("Text must be a string");
}

this.toastBuilder.setText(text);
}

/**
* Sets the duration for the toast.
* @param {number} duration - The duration of the toast.
* @throws If the duration is not a number.
* @throws If the duration is invalid.
* @requires MMRL version `33049` or higher.
*/
@NativeMethod({ requireVersion })
public setDuration(duration: number): void {
if (typeof duration !== "number") {
throw new TypeError("Duration must be a number");
}

if (duration !== Toast.LENGTH_SHORT && duration !== Toast.LENGTH_LONG) {
throw new Error("Invalid duration");
}

this.duration = duration;
this.toastBuilder.setDuration(this.duration);
}

/**
* Sets the gravity for the toast.
* @param {number} gravity - The gravity of the toast. @throws If gravity is not a valid number.
* @param {number} xOffset - The x offset of the toast. @throws If xOffset is not a valid number.
* @param {number} yOffset - The y offset of the toast. @throws If yOffset is not a valid number.
* @requires MMRL version `33049` or higher.
*/
@NativeMethod({ requireVersion })
public setGravity(gravity: number, xOffset: number, yOffset: number): void {
if (typeof gravity !== "number") {
throw new TypeError("Gravity must be a number");
}

if (typeof xOffset !== "number") {
throw new TypeError("X offset must be a number");
}

class Toast extends MMRLObjectAccessor<ToastImpl> {
public constructor() {
super(window["mmrl"] as object);
if (typeof yOffset !== "number") {
throw new TypeError("Y offset must be a number");
}
}

this.toastBuilder.setGravity(gravity, xOffset, yOffset);
}

/**
* Shows the toast.
* @requires MMRL version `33049` or higher.
*/
@NativeMethod({ requireVersion })
public show(): void {
this.toastBuilder.show();
}

/**
* Cancels the toast.
* @requires MMRL version `33049` or higher.
*/
@NativeMethod({ requireVersion })
public cancel(): void {
this.toastBuilder.cancel();
}

/**
* Creates a new Toast instance with the specified text and duration.
* @param {string} text - The text to display.
* @param {number} [duration=Toast.LENGTH_SHORT] - The duration of the toast.
* @requires MMRL version `33049` or higher.
* @returns {Toast} The created Toast instance.
*/
@NativeMethod({ requireVersion })
public static makeText(
text: string,
duration: number = Toast.LENGTH_SHORT
): Toast {
const toast = new Toast();
toast.setText(text);
toast.setDuration(duration);
return toast;
}
}
6 changes: 5 additions & 1 deletion js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ export type { FileSystemImpl } from "./classes/FileSystem";
export { AccessorScope } from "./util/AccessorScope";
export { MMRLObjectAccessor } from "./classes/MMRLObjectAccessor";
export { MMRLInterface, MMRLInterfaceFactory } from "./classes/MMRLInterface";
export { FileSystem, FileSystemFactory } from "./classes/FileSystem";
export { FileSystem, FileSystemFactory } from "./classes/FileSystem";
export { Toast } from "./classes/Toast";
export { NativeMethod } from "./decorators/NativeMethod";
export { NativeProperty } from "./decorators/NativeProperty";
export { mmrl } from "./classes/VersionInterface";
9 changes: 9 additions & 0 deletions js/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ export interface Manager {
versionCode: number;
}

export interface ToastImpl {
setText(text: string): void;
setDuration(duration: number): void;
setGravity(gravity: number, xOffset: number, yOffset: number): void;
show(): void;
cancel(): void;
}

export {};

declare global {
Expand All @@ -29,6 +37,7 @@ declare global {
interface MMRL {
getBuildConfig(): BuildConfigDetails;
getRootConfig(): RootConfigDetails;
toastBuilder(): ToastImpl;
}

interface Window {
Expand Down

0 comments on commit a3a48cd

Please sign in to comment.