chore: Merge upstream changes --------- Signed-off-by: Krille <c.kussowski@famedly.com> Co-authored-by: krille-chan <christian-kussowski@posteo.de> Co-authored-by: Krille <c.kussowski@famedly.com> Co-authored-by: Linerly <linerly@proton.me> Co-authored-by: Priit Jõerüüt <hwlate@joeruut.com> Co-authored-by: 大王叫我来巡山 <hamburger2048@users.noreply.hosted.weblate.org> Co-authored-by: fadelkon <fadelkon@posteo.net> Co-authored-by: Aindriú Mac Giolla Eoin <aindriu80@gmail.com> Co-authored-by: Edgars Andersons <Edgars+Weblate@gaitenis.id.lv> Co-authored-by: josé m <correoxm@disroot.org> Co-authored-by: Bezruchenko Simon <worcposj44@gmail.com> Co-authored-by: Christian <christian-pauly@posteo.de> Co-authored-by: - <hitekex@yandex.ru> Co-authored-by: Angelo Schirinzi <Odi-3@users.noreply.hosted.weblate.org> Co-authored-by: xabirequejo <xabi.rn@gmail.com> Co-authored-by: Piotr Orzechowski <piotr@orzechowski.tech> Co-authored-by: Rex_sa <rex.sa@pm.me> Co-authored-by: Tewuzij <tenajeza@outlook.com> Co-authored-by: goknarbahceli <goknarbahceli@proton.me> Co-authored-by: தமிழ்நேரம் <anishprabu.t@gmail.com> Co-authored-by: Erin <erin@erindesu.cz> Co-authored-by: EpicKiwi <me@epickiwi.fr> Co-authored-by: Christian Tietze <me@christiantietze.de> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
77 lines
2.2 KiB
Dart
77 lines
2.2 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
|
import 'package:matrix/matrix.dart';
|
|
|
|
import 'package:fluffychat/pangea/common/controllers/pangea_controller.dart';
|
|
import 'package:fluffychat/widgets/adaptive_dialogs/show_text_input_dialog.dart';
|
|
|
|
class SpaceCodeUtil {
|
|
static const codeLength = 7;
|
|
|
|
static bool isValidCode(String? spacecode) {
|
|
if (spacecode == null) return false;
|
|
return spacecode.length == codeLength && spacecode.contains(r'[0-9]');
|
|
}
|
|
|
|
static Future<String> generateSpaceCode(Client client) async {
|
|
final response = await client.httpClient.get(
|
|
Uri.parse(
|
|
'${client.homeserver}/_synapse/client/pangea/v1/request_room_code',
|
|
),
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'Bearer ${client.accessToken}',
|
|
},
|
|
);
|
|
if (response.statusCode != 200) {
|
|
throw Exception('Failed to generate room code: $response');
|
|
}
|
|
final roomCodeResult = jsonDecode(response.body);
|
|
if (roomCodeResult['access_code'] is String) {
|
|
return roomCodeResult['access_code'] as String;
|
|
} else {
|
|
throw Exception('Invalid response, access_code not found $response');
|
|
}
|
|
}
|
|
|
|
static Future<void> joinWithSpaceCodeDialog(
|
|
BuildContext context,
|
|
PangeaController pangeaController,
|
|
) async {
|
|
final String? spaceCode = await showTextInputDialog(
|
|
context: context,
|
|
title: L10n.of(context).joinWithClassCode,
|
|
okLabel: L10n.of(context).ok,
|
|
cancelLabel: L10n.of(context).cancel,
|
|
hintText: L10n.of(context).joinWithClassCodeHint,
|
|
autoSubmit: true,
|
|
);
|
|
if (spaceCode == null || spaceCode.isEmpty) return;
|
|
await pangeaController.classController.joinClasswithCode(
|
|
context,
|
|
spaceCode,
|
|
);
|
|
}
|
|
|
|
static messageDialog(
|
|
BuildContext context,
|
|
String title,
|
|
void Function()? action,
|
|
) =>
|
|
showDialog(
|
|
context: context,
|
|
useRootNavigator: false,
|
|
builder: (context) => AlertDialog(
|
|
content: Text(title),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: action,
|
|
child: Text(L10n.of(context).ok),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|