From 880f9cce4b1c3df0fef5e29603c7cd0867f92f86 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Fri, 15 Jan 2021 19:59:30 +0100 Subject: [PATCH] refactor: Use Provider --- lib/components/matrix.dart | 32 +++++--------------------------- lib/views/chat_list.dart | 15 +++++---------- 2 files changed, 10 insertions(+), 37 deletions(-) diff --git a/lib/components/matrix.dart b/lib/components/matrix.dart index 2d527b7b..686c937d 100644 --- a/lib/components/matrix.dart +++ b/lib/components/matrix.dart @@ -15,6 +15,7 @@ import 'package:flushbar/flushbar.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart'; +import 'package:provider/provider.dart'; import 'package:universal_html/prefer_universal/html.dart' as html; import 'package:url_launcher/url_launcher.dart'; import 'package:path_provider/path_provider.dart'; @@ -43,12 +44,8 @@ class Matrix extends StatefulWidget { MatrixState createState() => MatrixState(); /// Returns the (nearest) Client instance of your application. - static MatrixState of(BuildContext context) { - var newState = - (context.dependOnInheritedWidgetOfExactType<_InheritedMatrix>()).data; - newState.context = FirebaseController.context = context; - return newState; - } + static MatrixState of(BuildContext context) => + Provider.of(context, listen: false); } class MatrixState extends State { @@ -57,8 +54,6 @@ class MatrixState extends State { @override BuildContext context; - static const String userStatusesType = 'chat.fluffy.user_statuses'; - Map get shareContent => _shareContent; set shareContent(Map content) { _shareContent = content; @@ -438,26 +433,9 @@ class MatrixState extends State { @override Widget build(BuildContext context) { - return _InheritedMatrix( - data: this, + return Provider( + create: (_) => this, child: widget.child, ); } } - -class _InheritedMatrix extends InheritedWidget { - final MatrixState data; - - _InheritedMatrix({Key key, this.data, Widget child}) - : super(key: key, child: child); - - @override - bool updateShouldNotify(_InheritedMatrix old) { - var update = old.data.client.accessToken != data.client.accessToken || - old.data.client.userID != data.client.userID || - old.data.client.deviceID != data.client.deviceID || - old.data.client.deviceName != data.client.deviceName || - old.data.client.homeserver != data.client.homeserver; - return update; - } -} diff --git a/lib/views/chat_list.dart b/lib/views/chat_list.dart index 85cedbca..b907efba 100644 --- a/lib/views/chat_list.dart +++ b/lib/views/chat_list.dart @@ -300,16 +300,11 @@ class _ChatListState extends State { ConnectionStatusHeader(), Expanded( child: StreamBuilder( - stream: - Matrix.of(context).client.onSync.stream.where( - (s) => - s.hasRoomUpdate || - s.accountData - .where((a) => - a.type == - MatrixState.userStatusesType) - .isNotEmpty, - ), + stream: Matrix.of(context) + .client + .onSync + .stream + .where((s) => s.hasRoomUpdate), builder: (context, snapshot) { return FutureBuilder( future: waitForFirstSync(context),