Skip to content

Commit

Permalink
Merge pull request #1720 from matthiasn/feat/matrix_setup_modal
Browse files Browse the repository at this point in the history
feat: add Matrix setup modal
  • Loading branch information
matthiasn authored Apr 18, 2024
2 parents 12af47a + 9dd12f1 commit dfc9126
Show file tree
Hide file tree
Showing 37 changed files with 1,628 additions and 391 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,4 @@ whisper.cpp/whisper.cpp-*/

integration_test/docker/config/jetstream/
integration_test/docker/config/logs/
integration_test/docker/config/media_store/
1 change: 1 addition & 0 deletions integration_test/build/.last_build_id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
e581f07cdf2a87cf03a4c057574d76e4
7 changes: 0 additions & 7 deletions lib/beamer/locations/settings_location.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import 'package:lotti/pages/settings/measurables/measurables_page.dart';
import 'package:lotti/pages/settings/outbox/outbox_monitor.dart';
import 'package:lotti/pages/settings/settings_page.dart';
import 'package:lotti/pages/settings/speech/speech_settings_page.dart';
import 'package:lotti/pages/settings/sync/matrix_settings_page.dart';
import 'package:lotti/pages/settings/sync/sync_assistant_page.dart';
import 'package:lotti/pages/settings/tags/create_tag_page.dart';
import 'package:lotti/pages/settings/tags/tag_edit_page.dart';
Expand Down Expand Up @@ -252,12 +251,6 @@ class SettingsLocation extends BeamLocation<BeamState> {
child: SyncAssistantPage(),
),

if (pathContains('advanced/matrix_settings'))
const BeamPage(
key: ValueKey('settings-matrix_settings'),
child: MatrixSettingsPage(),
),

if (pathContains('advanced/outbox_monitor'))
const BeamPage(
key: ValueKey('settings-outbox_monitor'),
Expand Down
1 change: 0 additions & 1 deletion lib/classes/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class MatrixConfig with _$MatrixConfig {
required String homeServer,
required String user,
required String password,
String? roomId,
}) = _MatrixConfig;

factory MatrixConfig.fromJson(Map<String, dynamic> json) =>
Expand Down
35 changes: 7 additions & 28 deletions lib/classes/config.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,6 @@ mixin _$MatrixConfig {
String get homeServer => throw _privateConstructorUsedError;
String get user => throw _privateConstructorUsedError;
String get password => throw _privateConstructorUsedError;
String? get roomId => throw _privateConstructorUsedError;

Map<String, dynamic> toJson() => throw _privateConstructorUsedError;
@JsonKey(ignore: true)
Expand All @@ -252,7 +251,7 @@ abstract class $MatrixConfigCopyWith<$Res> {
MatrixConfig value, $Res Function(MatrixConfig) then) =
_$MatrixConfigCopyWithImpl<$Res, MatrixConfig>;
@useResult
$Res call({String homeServer, String user, String password, String? roomId});
$Res call({String homeServer, String user, String password});
}

/// @nodoc
Expand All @@ -271,7 +270,6 @@ class _$MatrixConfigCopyWithImpl<$Res, $Val extends MatrixConfig>
Object? homeServer = null,
Object? user = null,
Object? password = null,
Object? roomId = freezed,
}) {
return _then(_value.copyWith(
homeServer: null == homeServer
Expand All @@ -286,10 +284,6 @@ class _$MatrixConfigCopyWithImpl<$Res, $Val extends MatrixConfig>
? _value.password
: password // ignore: cast_nullable_to_non_nullable
as String,
roomId: freezed == roomId
? _value.roomId
: roomId // ignore: cast_nullable_to_non_nullable
as String?,
) as $Val);
}
}
Expand All @@ -302,7 +296,7 @@ abstract class _$$MatrixConfigImplCopyWith<$Res>
__$$MatrixConfigImplCopyWithImpl<$Res>;
@override
@useResult
$Res call({String homeServer, String user, String password, String? roomId});
$Res call({String homeServer, String user, String password});
}

/// @nodoc
Expand All @@ -319,7 +313,6 @@ class __$$MatrixConfigImplCopyWithImpl<$Res>
Object? homeServer = null,
Object? user = null,
Object? password = null,
Object? roomId = freezed,
}) {
return _then(_$MatrixConfigImpl(
homeServer: null == homeServer
Expand All @@ -334,10 +327,6 @@ class __$$MatrixConfigImplCopyWithImpl<$Res>
? _value.password
: password // ignore: cast_nullable_to_non_nullable
as String,
roomId: freezed == roomId
? _value.roomId
: roomId // ignore: cast_nullable_to_non_nullable
as String?,
));
}
}
Expand All @@ -346,10 +335,7 @@ class __$$MatrixConfigImplCopyWithImpl<$Res>
@JsonSerializable()
class _$MatrixConfigImpl implements _MatrixConfig {
const _$MatrixConfigImpl(
{required this.homeServer,
required this.user,
required this.password,
this.roomId});
{required this.homeServer, required this.user, required this.password});

factory _$MatrixConfigImpl.fromJson(Map<String, dynamic> json) =>
_$$MatrixConfigImplFromJson(json);
Expand All @@ -360,12 +346,10 @@ class _$MatrixConfigImpl implements _MatrixConfig {
final String user;
@override
final String password;
@override
final String? roomId;

@override
String toString() {
return 'MatrixConfig(homeServer: $homeServer, user: $user, password: $password, roomId: $roomId)';
return 'MatrixConfig(homeServer: $homeServer, user: $user, password: $password)';
}

@override
Expand All @@ -377,14 +361,12 @@ class _$MatrixConfigImpl implements _MatrixConfig {
other.homeServer == homeServer) &&
(identical(other.user, user) || other.user == user) &&
(identical(other.password, password) ||
other.password == password) &&
(identical(other.roomId, roomId) || other.roomId == roomId));
other.password == password));
}

@JsonKey(ignore: true)
@override
int get hashCode =>
Object.hash(runtimeType, homeServer, user, password, roomId);
int get hashCode => Object.hash(runtimeType, homeServer, user, password);

@JsonKey(ignore: true)
@override
Expand All @@ -404,8 +386,7 @@ abstract class _MatrixConfig implements MatrixConfig {
const factory _MatrixConfig(
{required final String homeServer,
required final String user,
required final String password,
final String? roomId}) = _$MatrixConfigImpl;
required final String password}) = _$MatrixConfigImpl;

factory _MatrixConfig.fromJson(Map<String, dynamic> json) =
_$MatrixConfigImpl.fromJson;
Expand All @@ -417,8 +398,6 @@ abstract class _MatrixConfig implements MatrixConfig {
@override
String get password;
@override
String? get roomId;
@override
@JsonKey(ignore: true)
_$$MatrixConfigImplCopyWith<_$MatrixConfigImpl> get copyWith =>
throw _privateConstructorUsedError;
Expand Down
2 changes: 0 additions & 2 deletions lib/classes/config.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions lib/database/settings_db.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ class SettingsDb extends _$SettingsDb {
return into(settings).insertOnConflictUpdate(settingsItem);
}

Future<void> removeSettingsItem(String configKey) async {
final existing = await watchSettingsItemByKey(configKey).first;
if (existing.isNotEmpty) {
await delete(settings).delete(existing.first);
}
}

Stream<List<SettingsItem>> watchSettingsItemByKey(String configKey) {
return settingsItemByKey(configKey).watch();
}
Expand Down
2 changes: 1 addition & 1 deletion lib/get_it.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Future<void> registerSingletons() async {
..registerSingleton<Maintenance>(Maintenance())
..registerSingleton<NavService>(NavService());

unawaited(getIt<MatrixService>().loginAndListen());
unawaited(getIt<MatrixService>().init());

await initConfigFlags(getIt<JournalDb>(), inMemoryDatabase: false);
}
9 changes: 9 additions & 0 deletions lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,15 @@
"settingsMatrixSaveLabel": "Save",
"settingsMatrixStartVerificationLabel": "Start Verification",
"settingsMatrixTitle": "Matrix Sync Settings",
"settingsMatrixHomeserverConfigTitle": "Matrix Homeserver Setup",
"settingsMatrixRoomConfigTitle": "Matrix Sync Room Setup",
"settingsMatrixStatsTitle": "Matrix Stats",
"settingsMatrixCancel": "Cancel",
"settingsMatrixDone": "Done",
"settingsMatrixNextPage": "Next Page",
"settingsMatrixQrTextPage": "Scan this QR code to invite device to a sync room.",
"settingsMatrixPreviousPage": "Previous Page",
"settingsMatrixUnverifiedDevicesPage": "Unverified Devices",
"settingsMatrixUserLabel": "User",
"settingsMatrixVerificationCancelledLabel": "Cancelled on other device...",
"settingsMatrixVerificationSuccessConfirm": "Got it",
Expand Down
6 changes: 2 additions & 4 deletions lib/pages/settings/advanced_settings_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:lotti/pages/settings/outbox/outbox_badge.dart';
import 'package:lotti/pages/settings/sliver_box_adapter_page.dart';
import 'package:lotti/sync/ui/matrix_settings_modal.dart';
import 'package:lotti/widgets/settings/settings_card.dart';
import 'package:lotti/widgets/settings/settings_icon.dart';
import 'package:material_design_icons_flutter/material_design_icons_flutter.dart';
Expand All @@ -27,10 +28,7 @@ class AdvancedSettingsPage extends StatelessWidget {
title: localizations.settingsSyncCfgTitle,
path: '/settings/advanced/sync_settings',
),
SettingsNavCard(
title: localizations.settingsMatrixTitle,
path: '/settings/advanced/matrix_settings',
),
const MatrixSettingsCard(),
SettingsNavCard(
trailing: OutboxBadgeIcon(
icon: SettingsIcon(MdiIcons.mailboxOutline),
Expand Down
27 changes: 0 additions & 27 deletions lib/pages/settings/sync/matrix_settings_page.dart

This file was deleted.

9 changes: 6 additions & 3 deletions lib/sync/matrix/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Client createMatrixClient({
databaseBuilder: (_) async {
final docDir = getIt<Directory>();
final path = '${docDir.path}/matrix/';
// TODO(matthiasn): use MatrixSdkDatabase instead
// ignore: deprecated_member_use
final db = HiveCollectionsDatabase(hiveDbName ?? 'lotti_sync', path);
await db.open();
return db;
Expand Down Expand Up @@ -54,8 +56,9 @@ Future<String> createMatrixDeviceName() async {
return '$deviceName $dateHhMm ${uuid.v1().substring(0, 4)}';
}

Future<void> matrixLogin({
Future<void> matrixConnect({
required MatrixService service,
required bool shouldAttemptLogin,
}) async {
final loggingDb = getIt<LoggingDb>();

Expand Down Expand Up @@ -87,7 +90,7 @@ Future<void> matrixLogin({
waitUntilLoadCompletedLoaded: false,
);

if (!service.isLoggedIn()) {
if (!service.isLoggedIn() && shouldAttemptLogin) {
final initialDeviceDisplayName =
service.deviceDisplayName ?? await createMatrixDeviceName();

Expand All @@ -106,7 +109,7 @@ Future<void> matrixLogin({
);
}

final roomId = matrixConfig.roomId;
final roomId = await service.getRoom();

if (roomId != null) {
await service.joinRoom(roomId);
Expand Down
1 change: 1 addition & 0 deletions lib/sync/matrix/consts.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const configNotFound = 'Could not find Matrix Config';
const syncMessageType = 'com.lotti.sync.message';
const String matrixConfigKey = 'MATRIX_CONFIG';
const String matrixRoomKey = 'MATRIX_ROOM';
const String lastReadMatrixEventId = 'LAST_READ_MATRIX_EVENT_ID';
39 changes: 35 additions & 4 deletions lib/sync/matrix/matrix_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,45 @@ class MatrixService {
final incomingKeyVerificationController =
StreamController<KeyVerification>.broadcast();

Future<void> loginAndListen() async {
Future<void> init() async {
await loadConfig();
await login();
await connect();
if (_client.onLoginStateChanged.value == LoginState.loggedIn) {
await listen();
}
}

Future<void> listen() async {
await startKeyVerificationListener();
await listenToTimeline();
}

Future<void> loginAndListen() async {
await loadConfig();
await login();
await listen();
}

Client get client => _client;
Future<void> login() => matrixLogin(service: this);

Future<void> login() => matrixConnect(
service: this,
shouldAttemptLogin: true,
);

Future<void> connect() => matrixConnect(
service: this,
shouldAttemptLogin: false,
);

Future<String?> joinRoom(String roomId) =>
joinMatrixRoom(roomId: roomId, service: this);

Future<void> saveRoom(String roomId) => saveMatrixRoom(
roomId: roomId,
client: client,
);

bool isLoggedIn() {
// TODO(unassigned): find non-deprecated solution
// ignore: deprecated_member_use
Expand All @@ -104,10 +131,14 @@ class MatrixService {
List<String>? invite,
}) =>
createMatrixRoom(
client: _client,
service: this,
invite: invite,
);

Future<String?> getRoom() => getMatrixRoom(client: _client);

Future<void> leaveRoom() => leaveMatrixRoom(client: _client);

Future<void> inviteToSyncRoom({
required String userId,
}) =>
Expand Down
Loading

0 comments on commit dfc9126

Please sign in to comment.