Skip to content

Commit

Permalink
refactor: Migrate routes to go router
Browse files Browse the repository at this point in the history
  • Loading branch information
krille-chan committed Aug 10, 2023
1 parent 739edde commit ee957ab
Show file tree
Hide file tree
Showing 52 changed files with 584 additions and 612 deletions.
601 changes: 327 additions & 274 deletions lib/config/routes.dart

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions lib/config/themes.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'package:go_router/go_router.dart';

import 'package:fluffychat/utils/platform_infos.dart';
import 'app_config.dart';

Expand All @@ -17,8 +15,8 @@ abstract class FluffyThemes {
static bool isColumnMode(BuildContext context) =>
isColumnModeByWidth(MediaQuery.of(context).size.width);

static bool getDisplayNavigationRail(BuildContext context) =>
!VRouter.of(context).path.startsWith('/settings');
static bool isThreeColumnMode(BuildContext context) =>
MediaQuery.of(context).size.width > FluffyThemes.columnWidth * 3.5;

static const fallbackTextStyle = TextStyle(
fontFamily: 'Roboto',
Expand Down
31 changes: 14 additions & 17 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import 'package:collection/collection.dart';
import 'package:flutter_app_lock/flutter_app_lock.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:matrix/matrix.dart';
import 'package:universal_html/html.dart' as html;

import 'package:fluffychat/utils/client_manager.dart';
import 'package:fluffychat/utils/platform_infos.dart';
import 'config/setting_keys.dart';
import 'utils/background_push.dart';
import 'widgets/fluffy_chat_app.dart';
import 'widgets/lock_screen.dart';

void main() async {
Logs().i('Welcome to FluffyChat');

// Our background push shared isolate accesses flutter-internal things very early in the startup proccess
// To make sure that the parts of flutter needed are started up already, we need to ensure that the
// widget bindings are initialized already.
Expand All @@ -26,29 +28,24 @@ void main() async {
await firstClient?.roomsLoading;
await firstClient?.accountDataLoading;

String? pin;
if (PlatformInfos.isMobile) {
BackgroundPush.clientOnly(clients.first);
}

final queryParameters = <String, String>{};
if (kIsWeb) {
queryParameters
.addAll(Uri.parse(html.window.location.href).queryParameters);
try {
pin =
await const FlutterSecureStorage().read(key: SettingKeys.appLockKey);
} catch (e, s) {
Logs().d('Unable to read PIN from Secure storage', e, s);
}
}

runApp(
PlatformInfos.isMobile
? AppLock(
builder: (args) => FluffyChatApp(
clients: clients,
queryParameters: queryParameters,
),
builder: (args) => FluffyChatApp(clients: clients),
lockScreen: const LockScreen(),
enabled: false,
enabled: pin?.isNotEmpty ?? false,
)
: FluffyChatApp(
clients: clients,
queryParameters: queryParameters,
),
: FluffyChatApp(clients: clients),
);
}
2 changes: 1 addition & 1 deletion lib/pages/add_story/add_story.dart
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ class AddStoryController extends State<AddStoryPage> {
},
);
if (postResult.error == null) {
VRouter.of(context).pop();
context.pop();
}
}

Expand Down
29 changes: 18 additions & 11 deletions lib/pages/chat/chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,17 @@ import 'sticker_picker_dialog.dart';

class ChatPage extends StatelessWidget {
final Widget? sideView;
final String roomId;

const ChatPage({Key? key, this.sideView}) : super(key: key);
const ChatPage({
Key? key,
this.sideView,
required this.roomId,
}) : super(key: key);

@override
Widget build(BuildContext context) {
final roomId = context.vRouter.pathParameters['roomid'];
final room =
roomId == null ? null : Matrix.of(context).client.getRoomById(roomId);
final room = Matrix.of(context).client.getRoomById(roomId);
if (room == null) {
return Scaffold(
appBar: AppBar(title: Text(L10n.of(context)!.oopsSomethingWentWrong)),
Expand All @@ -58,7 +61,11 @@ class ChatPage extends StatelessWidget {
),
);
}
return ChatPageWithRoom(sideView: sideView, room: room);
return ChatPageWithRoom(
key: Key('chat_page_$roomId'),
sideView: sideView,
room: room,
);
}
}

Expand Down Expand Up @@ -188,7 +195,7 @@ class ChatController extends State<ChatPageWithRoom> {
);
final roomId = success.result;
if (roomId == null) return;
VRouter.of(context).toSegments(['rooms', roomId]);
context.go(['', 'rooms', roomId].join('/'));
}

void leaveChat() async {
Expand All @@ -197,7 +204,7 @@ class ChatController extends State<ChatPageWithRoom> {
future: room.leave,
);
if (success.error != null) return;
VRouter.of(context).to('/rooms');
context.go('/rooms');
}

EmojiPickerType emojiPickerType = EmojiPickerType.keyboard;
Expand Down Expand Up @@ -329,7 +336,7 @@ class ChatController extends State<ChatPageWithRoom> {
// "load more" button is visible on the screen
SchedulerBinding.instance.addPostFrameCallback((_) async {
if (mounted) {
final event = VRouter.of(context).queryParameters['event'];
final event = GoRouterState.of(context).uri.queryParameters['event'];
if (event != null) {
scrollToEventId(event);
}
Expand Down Expand Up @@ -803,7 +810,7 @@ class ChatController extends State<ChatPageWithRoom> {
};
}
setState(() => selectedEvents.clear());
VRouter.of(context).to('/rooms');
context.go('/rooms');
}

void sendAgainAction() {
Expand Down Expand Up @@ -901,7 +908,7 @@ class ChatController extends State<ChatPageWithRoom> {
future: room.forget,
);
if (result.error != null) return;
VRouter.of(context).to('/archive');
context.go('/rooms/archive');
}

void typeEmoji(Emoji? emoji) {
Expand Down Expand Up @@ -1017,7 +1024,7 @@ class ChatController extends State<ChatPageWithRoom> {
future: room.leave,
);
if (result.error == null) {
VRouter.of(context).toSegments(['rooms', result.result!]);
context.go(['', 'rooms', result.result!].join('/'));
}
}

Expand Down
3 changes: 1 addition & 2 deletions lib/pages/chat/chat_app_bar_title.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ class ChatAppBarTitle extends StatelessWidget {
)
: controller.isArchived
? null
: () =>
VRouter.of(context).toSegments(['rooms', room.id, 'details']),
: () => context.go(['', 'rooms', room.id, 'details'].join('/')),
child: Row(
children: [
Hero(
Expand Down
10 changes: 5 additions & 5 deletions lib/pages/chat/chat_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:badges/badges.dart';
import 'package:desktop_drop/desktop_drop.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:future_loading_dialog/future_loading_dialog.dart';
import 'package:go_router/go_router.dart';
import 'package:matrix/matrix.dart';

import 'package:fluffychat/config/app_config.dart';
Expand Down Expand Up @@ -147,15 +146,16 @@ class ChatView extends StatelessWidget {
}
final bottomSheetPadding = FluffyThemes.isColumnMode(context) ? 16.0 : 8.0;

return VWidgetGuard(
onSystemPop: (redirector) async {
return WillPopScope(
onWillPop: () async {
if (controller.selectedEvents.isNotEmpty) {
controller.clearSelectedEvents();
redirector.stopRedirection();
return false;
} else if (controller.showEmojiPicker) {
controller.emojiPickerAction();
redirector.stopRedirection();
return false;
}
return true;
},
child: GestureDetector(
onTapDown: (_) => controller.setReadMarker(),
Expand Down
4 changes: 2 additions & 2 deletions lib/pages/chat/encryption_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class EncryptionButton extends StatelessWidget {
? Colors.orange
: null,
),
onPressed: () => VRouter.of(context)
.toSegments(['rooms', room.id, 'encryption']),
onPressed: () =>
context.go(['', 'rooms', room.id, 'encryption'].join('/')),
),
);
},
Expand Down
13 changes: 9 additions & 4 deletions lib/pages/chat_details/chat_details.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ import 'package:fluffychat/widgets/matrix.dart';
enum AliasActions { copy, delete, setCanonical }

class ChatDetails extends StatefulWidget {
const ChatDetails({Key? key}) : super(key: key);
final String roomId;

const ChatDetails({
Key? key,
required this.roomId,
}) : super(key: key);

@override
ChatDetailsController createState() => ChatDetailsController();
Expand All @@ -32,7 +37,7 @@ class ChatDetailsController extends State<ChatDetails> {
void toggleDisplaySettings() =>
setState(() => displaySettings = !displaySettings);

String? get roomId => VRouter.of(context).pathParameters['roomid'];
String? get roomId => widget.roomId;

void setDisplaynameAction() async {
final room = Matrix.of(context).client.getRoomById(roomId!)!;
Expand Down Expand Up @@ -257,9 +262,9 @@ class ChatDetailsController extends State<ChatDetails> {
if ((room.states['im.ponies.room_emotes'] ?? <String, Event>{})
.keys
.any((String s) => s.isNotEmpty)) {
VRouter.of(context).to('multiple_emotes');
context.go('/rooms/${room.id}/details/multiple_emotes');
} else {
VRouter.of(context).to('emotes');
context.go('/rooms/${room.id}/details/emotes');
}
}

Expand Down
16 changes: 9 additions & 7 deletions lib/pages/chat_details/chat_details_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ class ChatDetailsView extends StatelessWidget {
leading: IconButton(
icon: const Icon(Icons.close_outlined),
onPressed: () =>
VRouter.of(context).path.startsWith('/spaces/')
? VRouter.of(context).pop()
: VRouter.of(context)
.toSegments(['rooms', controller.roomId!]),
GoRouterState.of(context).uri.path.startsWith('/spaces/')
? context.pop()
: context.go(
['', 'rooms', controller.roomId!].join('/'),
),
),
elevation: Theme.of(context).appBarTheme.elevation,
expandedHeight: 300.0,
Expand Down Expand Up @@ -380,8 +381,8 @@ class ChatDetailsView extends StatelessWidget {
Icons.edit_attributes_outlined,
),
),
onTap: () =>
VRouter.of(context).to('permissions'),
onTap: () => context
.go('/rooms/${room.id}/details/permissions'),
),
],
const Divider(height: 1),
Expand All @@ -408,7 +409,8 @@ class ChatDetailsView extends StatelessWidget {
radius: Avatar.defaultSize / 2,
child: const Icon(Icons.add_outlined),
),
onTap: () => VRouter.of(context).to('invite'),
onTap: () =>
context.go('/rooms/${room.id}/invite'),
)
: const SizedBox.shrink(),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ChatEncryptionSettings extends StatefulWidget {
}

class ChatEncryptionSettingsController extends State<ChatEncryptionSettings> {
String? get roomId => VRouter.of(context).pathParameters['roomid'];
String? get roomId => GoRouterState.of(context).pathParameters['roomid'];

Room get room => Matrix.of(context).client.getRoomById(roomId!)!;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ChatEncryptionSettingsView extends StatelessWidget {
leading: IconButton(
icon: const Icon(Icons.close_outlined),
onPressed: () =>
VRouter.of(context).toSegments(['rooms', controller.roomId!]),
context.go(['', 'rooms', controller.roomId!].join('/')),
),
title: Text(L10n.of(context)!.encryption),
actions: [
Expand Down
Loading

0 comments on commit ee957ab

Please sign in to comment.