Skip to content

Commit

Permalink
add pull down refresh web socket
Browse files Browse the repository at this point in the history
resolves #71
  • Loading branch information
maxisme committed May 22, 2021
1 parent 78a7f94 commit 0bab078
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 8 deletions.
31 changes: 30 additions & 1 deletion lib/screens/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ import 'package:notifi/notifications/notifications_table.dart';
import 'package:notifi/notifications/notifis.dart';
import 'package:notifi/screens/utils/alert.dart';
import 'package:notifi/screens/utils/appbar_title.dart';
import 'package:notifi/user.dart';
import 'package:notifi/utils/icons.dart';
import 'package:provider/provider.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class HomeScreen extends StatelessWidget {
final RefreshController _refreshController = RefreshController();

@override
Widget build(BuildContext context) {
return Scaffold(
Expand All @@ -22,7 +26,32 @@ class HomeScreen extends StatelessWidget {
Navigator.pushNamed(context, '/settings');
}),
),
body: const NotificationTable(),
body: SmartRefresher(
header: CustomHeader(
builder: (_, RefreshStatus mode) {
// if (mode == RefreshStatus.completed) {
// _refreshController.refreshToIdle();
// }
return const SizedBox(
height: 40.0,
width: 40.0,
child: Center(
child: SizedBox(
height: 20.0,
width: 20.0,
child: CircularProgressIndicator(
strokeWidth: 1,
)),
),
);
},
),
controller: _refreshController,
onRefresh: () async {
_refreshController.refreshCompleted();
await Provider.of<User>(context, listen: false).initWSS();
},
child: const NotificationTable()),
bottomNavigationBar: BottomAppBar(
// ignore: prefer_const_literals_to_create_immutables
child: SizedBox(
Expand Down
14 changes: 8 additions & 6 deletions lib/user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class User with ChangeNotifier {
}

if (hadUser) {
await _initWSS();
await initWSS();
}
}

Expand All @@ -86,7 +86,7 @@ class User with ChangeNotifier {
if (await newUser.store()) {
_user = newUser;

await _initWSS();
await initWSS();

notifyListeners();
}
Expand All @@ -97,7 +97,7 @@ class User with ChangeNotifier {
////////
// ws //
////////
Future<void> _initWSS() async {
Future<void> initWSS() async {
if (_ws != null) {
L.i('Closing already open WS...');
await _ws.sink.close(status.normalClosure, 'new code!');
Expand Down Expand Up @@ -193,9 +193,11 @@ class User with ChangeNotifier {
}

Future<List<String>> _handleMessage(dynamic msg) async {
setErr(false);

if (msg == '.') return <String>[];
if (msg == '.') {
L.i('Connected to ws.');
setErr(false);
return <String>[];
}

// json decode incoming ws message
List<dynamic> notifications = <dynamic>[];
Expand Down
9 changes: 8 additions & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.8"
pull_to_refresh:
dependency: "direct main"
description:
name: pull_to_refresh
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
quiver:
dependency: transitive
description:
Expand Down Expand Up @@ -837,4 +844,4 @@ packages:
version: "2.2.1"
sdks:
dart: ">=2.13.0 <3.0.0"
flutter: ">=1.22.2"
flutter: ">=2.0.0"
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ dependencies:
package_info: ^2.0.0
path_provider: ^1.6.27
provider: ^5.0.0
pull_to_refresh: ^2.0.0
share: ^2.0.1
shared_preferences: ^0.5.12+4
sqflite: ^1.3.2+3
Expand Down

0 comments on commit 0bab078

Please sign in to comment.