From 73ad66c97aa69ca3f48d23202bc7df20aa56f2bb Mon Sep 17 00:00:00 2001 From: Ray Jasson <48130098+rayjasson98@users.noreply.github.com> Date: Tue, 5 Jan 2021 04:25:42 +0800 Subject: [PATCH] Add code for message handling and storing device token --- .../user/repositories/user_repository.dart | 12 +++- lib/ui/home_page.dart | 66 ++++++++++++------- lib/ui/profile/user_profile_page.dart | 9 --- lib/utils/message_handler.dart | 18 +++++ 4 files changed, 68 insertions(+), 37 deletions(-) create mode 100644 lib/utils/message_handler.dart diff --git a/lib/data/user/repositories/user_repository.dart b/lib/data/user/repositories/user_repository.dart index 463f902..19c86af 100644 --- a/lib/data/user/repositories/user_repository.dart +++ b/lib/data/user/repositories/user_repository.dart @@ -16,9 +16,15 @@ class UserRepository { User get currentUser => _currentUser; static String get uid => _uid; - Future updateUser(AppUser appUser) { - return _userDoc.update(appUser.toMap()); + Future saveToken(String token) async { + await _userDoc.update({ + 'tokens': FieldValue.arrayUnion([token]), + }); } - Future deleteTodo() => _userDoc.delete(); + Future update(AppUser appUser) async { + await _userDoc.update(appUser.toMap()); + } + + Future delete() => _userDoc.delete(); } diff --git a/lib/ui/home_page.dart b/lib/ui/home_page.dart index 7338dbe..6823878 100644 --- a/lib/ui/home_page.dart +++ b/lib/ui/home_page.dart @@ -1,11 +1,14 @@ import 'package:farmassist/app_theme.dart'; +import 'package:farmassist/data/user/repositories/user_repository.dart'; import 'package:farmassist/ui/IoT/IoT_monitoring_page.dart'; import 'package:farmassist/ui/bottom_navigation_bar/bottom_navigation_bar.dart'; import 'package:farmassist/ui/bottom_navigation_bar/tab_icon_data.dart'; import 'package:farmassist/ui/diseases/disease_detection_page.dart'; import 'package:farmassist/ui/farm/farm_management_page.dart'; import 'package:farmassist/ui/profile/user_profile_page.dart'; +import 'package:farmassist/utils/message_handler.dart'; import 'package:flutter/material.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; class HomePage extends StatefulWidget { static Route route() { @@ -17,6 +20,8 @@ class HomePage extends StatefulWidget { } class _HomePageState extends State { + final UserRepository _userRepository = UserRepository(); + MessageHandler _messageHandler; List _tabIconsList = TabIconData.tabIconsList; List _tabList = [ FarmManagementPage(pageTitle: 'Farm Management'), @@ -28,6 +33,7 @@ class _HomePageState extends State { @override void initState() { + _messageHandler = MessageHandler(_userRepository)..generateToken(); _tabIconsList.forEach((TabIconData tab) { tab.isSelected = false; }); @@ -43,31 +49,41 @@ class _HomePageState extends State { @override Widget build(BuildContext context) { - return Container( - color: AppTheme.background, - child: Scaffold( - backgroundColor: Colors.transparent, - body: Stack( - children: [ - PageView( - physics: NeverScrollableScrollPhysics(), - controller: _pageController, - children: _tabList, - ), - Column( - children: [ - const Expanded( - child: SizedBox(), - ), - BottomNavBar( - tabIconsList: _tabIconsList, - onTap: (int i) { - _pageController.jumpToPage(i); - }, - ), - ], - ), - ], + return MultiRepositoryProvider( + providers: [ + RepositoryProvider.value( + value: _userRepository, + ), + RepositoryProvider.value( + value: _messageHandler, + ), + ], + child: Container( + color: AppTheme.background, + child: Scaffold( + backgroundColor: Colors.transparent, + body: Stack( + children: [ + PageView( + physics: NeverScrollableScrollPhysics(), + controller: _pageController, + children: _tabList, + ), + Column( + children: [ + const Expanded( + child: SizedBox(), + ), + BottomNavBar( + tabIconsList: _tabIconsList, + onTap: (int i) { + _pageController.jumpToPage(i); + }, + ), + ], + ), + ], + ), ), ), ); diff --git a/lib/ui/profile/user_profile_page.dart b/lib/ui/profile/user_profile_page.dart index fec4f54..6ce8795 100644 --- a/lib/ui/profile/user_profile_page.dart +++ b/lib/ui/profile/user_profile_page.dart @@ -1,6 +1,5 @@ import 'package:farmassist/app_theme.dart'; import 'package:farmassist/bloc/authentication/authentication.dart'; -import 'package:farmassist/data/user/repositories/user_repository.dart'; import 'package:farmassist/ui/profile/avatar.dart'; import 'package:farmassist/ui/profile/user_info_field.dart'; import 'package:farmassist/ui/widgets/tab_page.dart'; @@ -32,14 +31,6 @@ class _UserProfilePagePageState extends TabPageState { tabListView.add(_LogOutButton()); super.initState(); } - - @override - Widget build(BuildContext context) { - return RepositoryProvider( - create: (_) => UserRepository(), - child: super.build(context), - ); - } } class _LogOutButton extends StatelessWidget { diff --git a/lib/utils/message_handler.dart b/lib/utils/message_handler.dart new file mode 100644 index 0000000..693fb81 --- /dev/null +++ b/lib/utils/message_handler.dart @@ -0,0 +1,18 @@ +import 'package:farmassist/data/user/repositories/user_repository.dart'; +import 'package:firebase_messaging/firebase_messaging.dart'; + +class MessageHandler { + MessageHandler(UserRepository userRepository) { + _messaging = FirebaseMessaging.instance; + _userRepository = userRepository; + } + + FirebaseMessaging _messaging; + UserRepository _userRepository; + + Future generateToken() async { + String token = await _messaging.getToken(); + await _userRepository.saveToken(token); + _messaging.onTokenRefresh.listen(_userRepository.saveToken); + } +}