Skip to content

Commit

Permalink
feat: ✨ Bridge web view saves its state.
Browse files Browse the repository at this point in the history
A reload button is used to trigger dapp reload.
  • Loading branch information
Chralu committed Dec 2, 2024
1 parent d246852 commit 1738cde
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 105 deletions.
9 changes: 7 additions & 2 deletions lib/application/dapps.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:aewallet/application/api_service.dart';
import 'package:aewallet/application/settings/settings.dart';
import 'package:aewallet/domain/models/dapp.dart';
import 'package:aewallet/infrastructure/repositories/dapps_repository.dart';
import 'package:aewallet/model/available_networks.dart';
Expand All @@ -16,11 +17,15 @@ DAppsRepositoryImpl _dAppsRepository(
@riverpod
Future<DApp?> _getDApp(
Ref ref,
AvailableNetworks network,
String code,
) async {
final apiService = ref.watch(apiServiceProvider);
return ref.watch(_dAppsRepositoryProvider).getDApp(network, code, apiService);
final networkSettings = ref.watch(
SettingsProviders.settings.select((settings) => settings.network),
);
return ref
.watch(_dAppsRepositoryProvider)
.getDApp(networkSettings.network, code, apiService);
}

@riverpod
Expand Down
21 changes: 2 additions & 19 deletions lib/application/dapps.g.dart

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

12 changes: 11 additions & 1 deletion lib/domain/models/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ import 'package:freezed_annotation/freezed_annotation.dart';

part 'settings.freezed.dart';

enum MainScreenTab { accountTab, transactionTab, swapTab, earnTab, bridgeTab }
enum MainScreenTab {
accountTab,
transactionTab,
swapTab,
earnTab,
bridgeTab,
}

@freezed
class Settings with _$Settings {
Expand Down Expand Up @@ -42,4 +48,8 @@ class Settings with _$Settings {
);

const Settings._();

MainScreenTab get mainScreenTab => MainScreenTab.values.firstWhere(
(value) => value.index == mainScreenCurrentPage,
);
}
36 changes: 34 additions & 2 deletions lib/infrastructure/rpc/awc_webview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,35 @@ enum EVMWallet {
final String scheme;
}

/// Keeps track of all alive WebviewControllers
class AWCWebviewControllers {
const AWCWebviewControllers._();

static final _logger = Logger('AWCWebviewControllers');
static final _controllers = <Uri, InAppWebViewController>{};

static InAppWebViewController? find(Uri uri) => _controllers[uri];

static void register(Uri uri, InAppWebViewController controller) {
final alreadyRegisteredController = find(uri);
if (alreadyRegisteredController != null &&
controller == alreadyRegisteredController) {
return;
}
dispose(uri);
_controllers[uri] = controller;
_logger.info('Registered controller for $uri');
}

static void dispose(Uri uri) {
final controller = find(uri);
if (controller == null) return;
controller.dispose();
_controllers.remove(uri);
_logger.info('Disposed controller for $uri');
}
}

class AWCWebview extends StatefulWidget {
const AWCWebview({super.key, required this.uri});

Expand Down Expand Up @@ -96,11 +125,13 @@ class AWCWebview extends StatefulWidget {

class _AWCWebviewState extends State<AWCWebview> with WidgetsBindingObserver {
AWCJsonRPCServer? _peerServer;
InAppWebViewController? _controller;
WebviewMessagePortStreamChannel? _channel;
bool _loaded = false;
late final FocusNode _focusNode;

InAppWebViewController? get _controller =>
AWCWebviewControllers.find(widget.uri);

@override
void initState() {
if (kDebugMode &&
Expand All @@ -119,6 +150,7 @@ class _AWCWebviewState extends State<AWCWebview> with WidgetsBindingObserver {
_peerServer?.close();
_focusNode.dispose();
WidgetsBinding.instance.removeObserver(this);
AWCWebviewControllers.dispose(widget.uri);
AWCWebview._logger.info('AWC webview disposed.');

super.dispose();
Expand Down Expand Up @@ -153,7 +185,7 @@ class _AWCWebviewState extends State<AWCWebview> with WidgetsBindingObserver {
transparentBackground: true,
),
onLoadStop: (controller, _) async {
_controller = controller;
AWCWebviewControllers.register(widget.uri, controller);
await _initMessageChannelRPC(controller);
await _maintainWebviewFocus(controller);
setState(() {
Expand Down
8 changes: 5 additions & 3 deletions lib/ui/views/main/bloc/providers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,11 @@ Future<void> homePage(Ref ref) async {

final mainTabControllerProvider =
StateNotifierProvider.autoDispose<TabControllerNotifier, TabController?>(
(ref) {
return TabControllerNotifier();
});
(ref) {
return TabControllerNotifier();
},
name: 'TabControllerNotifier',
);

class TabControllerNotifier extends StateNotifier<TabController?> {
TabControllerNotifier() : super(null);
Expand Down
Loading

0 comments on commit 1738cde

Please sign in to comment.