kind of working!

This commit is contained in:
Henry Hiles 2026-01-24 16:39:56 +00:00
commit 8be9a4f2f6
No known key found for this signature in database
3 changed files with 31 additions and 24 deletions

View file

@ -37,21 +37,25 @@ class ClientController extends Notifier<int> {
GomuksStart(
handle,
Pointer.fromFunction<
Void Function(Pointer<Char>, Int64, GomuksBorrowedBuffer)
>(gomuksCallback),
NativeCallable<
Void Function(Pointer<Char>, Int64, GomuksBorrowedBuffer)
>.listener(gomuksCallback)
.nativeFunction,
);
return handle;
}
Map<String, dynamic> sendCommand(String command, Map<String, dynamic> data) {
final bufferPointer = data.toGomuksBufferPtr();
final response = GomuksSubmitCommand(
state,
command.toNativeUtf8().cast<Char>(),
data.toGomuksBuffer(),
bufferPointer.ref,
);
calloc.free(bufferPointer);
return response.buf.toJson();
}

View file

@ -31,26 +31,27 @@ extension GomuksOwnedBufferToJson on GomuksOwnedBuffer {
Map<String, dynamic> toJson() {
final bytes = toBytes();
if (bytes.isEmpty) return {};
return jsonDecode(utf8.decode(bytes));
final json = jsonDecode(utf8.decode(bytes));
if (json is Map<String, dynamic>?) return json ?? {};
throw json;
}
}
extension JsonToGomuksBuffer on Map<String, dynamic> {
GomuksBorrowedBuffer toGomuksBuffer() {
Pointer<GomuksBorrowedBuffer> toGomuksBufferPtr() {
final jsonString = json.encode(this);
final bytes = utf8.encode(jsonString);
final dataPtr = calloc<Uint8>(bytes.length);
dataPtr.asTypedList(bytes.length).setAll(0, bytes);
final bufPtr = calloc<GomuksBorrowedBuffer>();
bufPtr.ref.base = dataPtr;
bufPtr.ref.length = bytes.length;
final ptr = calloc<GomuksBorrowedBuffer>();
final bufByValue = bufPtr.ref;
ptr.ref
..base = dataPtr
..length = bytes.length;
calloc.free(bufPtr);
return bufByValue;
return ptr;
}
}

View file

@ -97,17 +97,19 @@ class App extends ConsumerWidget {
builder: (context) => ref
.watch(SharedPrefsController.provider)
.betterWhen(
data: (_) {
final response = ref
.watch(ClientController.provider.notifier)
.sendCommand("login", {
"homeserver_url": "federated.nexus",
"username": "quadradical",
"password": "Quadmarad1!",
});
return Text(response.toString());
},
data: (_) => ElevatedButton(
onPressed: () async {
final response = ref
.watch(ClientController.provider.notifier)
.sendCommand("login", {
"homeserver_url": "https://matrix.federated.nexus",
"username": "quadradical",
"password": "Quadfnrad1!",
});
print(response);
},
child: Text("foo"),
),
// .betterWhen(
// data: (client) =>
// client.accessToken == null ? LoginPage() : ChatPage(),