|
|
|
@@ -1,12 +1,21 @@ |
|
|
|
import 'dart:io'; |
|
|
|
|
|
|
|
import 'package:badges/badges.dart'; |
|
|
|
import 'package:dio/dio.dart'; |
|
|
|
import 'package:farm_tpf/custom_model/NotificationDTO.dart'; |
|
|
|
import 'package:farm_tpf/custom_model/NotificationObjectDTO.dart'; |
|
|
|
import 'package:farm_tpf/data/repository/user_repository.dart'; |
|
|
|
import 'package:farm_tpf/presentation/screens/account/sc_account.dart'; |
|
|
|
import 'package:farm_tpf/presentation/screens/control_device/sc_control_device.dart'; |
|
|
|
import 'package:farm_tpf/presentation/screens/notification/sc_notification.dart'; |
|
|
|
import 'package:farm_tpf/presentation/screens/notification/update_count_noti_bloc.dart'; |
|
|
|
import 'package:farm_tpf/presentation/screens/plot/sc_plot.dart'; |
|
|
|
import 'package:farm_tpf/presentation/screens/plot_detail/sc_plot_detail.dart'; |
|
|
|
import 'package:farm_tpf/utils/const_color.dart'; |
|
|
|
import 'package:farm_tpf/utils/const_common.dart'; |
|
|
|
import 'package:farm_tpf/utils/const_icons.dart'; |
|
|
|
import 'package:farm_tpf/utils/pref.dart'; |
|
|
|
import 'package:firebase_messaging/firebase_messaging.dart'; |
|
|
|
import 'package:flutter/material.dart'; |
|
|
|
import 'package:flutter_svg/flutter_svg.dart'; |
|
|
|
import 'package:get/get.dart'; |
|
|
|
@@ -23,6 +32,13 @@ class TabbarScreen extends StatefulWidget { |
|
|
|
} |
|
|
|
|
|
|
|
class _TabbarScreenState extends State<TabbarScreen> { |
|
|
|
UserRepository _userRepository = UserRepository(); |
|
|
|
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging(); |
|
|
|
var pref = LocalPref(); |
|
|
|
String pushkey = ""; |
|
|
|
String currentFullName = ""; |
|
|
|
var token; |
|
|
|
var client; |
|
|
|
final changeTabbar = Get.put(TabbarSelected()); |
|
|
|
List<TabbarItem> itemsTabbar = [ |
|
|
|
TabbarItem( |
|
|
|
@@ -38,13 +54,110 @@ class _TabbarScreenState extends State<TabbarScreen> { |
|
|
|
icon: AppIcons.icPerson, title: 'Cá nhân', index: TabBarIndex.account) |
|
|
|
]; |
|
|
|
|
|
|
|
Future<Null> getSharedPrefs() async { |
|
|
|
token = await pref.getString(DATA_CONST.TOKEN_KEY); |
|
|
|
pushkey = await pref.getString(DATA_CONST.PUSH_KEY); |
|
|
|
currentFullName = await pref.getString(DATA_CONST.CURRENT_FULL_NAME); |
|
|
|
var options = BaseOptions(baseUrl: ConstCommon.baseUrl); |
|
|
|
options.headers["Authorization"] = "Bearer $token"; |
|
|
|
client = Dio(options); |
|
|
|
if (Platform.isIOS) { |
|
|
|
_firebaseMessaging |
|
|
|
.requestNotificationPermissions(IosNotificationSettings()); |
|
|
|
} |
|
|
|
_firebaseMessaging.configure( |
|
|
|
onMessage: (Map<String, dynamic> message) async { |
|
|
|
print("onMessage---: $message"); |
|
|
|
try { |
|
|
|
print('call bloc update noti'); |
|
|
|
updateCountNotiBloc.getNotifications((data) {}, (err) {}); |
|
|
|
} catch (e) { |
|
|
|
print('error'); |
|
|
|
print(e); |
|
|
|
} |
|
|
|
}, |
|
|
|
onBackgroundMessage: Platform.isIOS ? null : myBackgroundMessageHandler, |
|
|
|
onLaunch: (Map<String, dynamic> message) async { |
|
|
|
print("onLaunch: $message"); |
|
|
|
Future.delayed(Duration(milliseconds: 500), () { |
|
|
|
_notificationNavigateOnFCM(message); |
|
|
|
}); |
|
|
|
}, |
|
|
|
onResume: (Map<String, dynamic> message) async { |
|
|
|
print("onResume: $message"); |
|
|
|
_notificationNavigateOnFCM(message); |
|
|
|
}, |
|
|
|
); |
|
|
|
_firebaseMessaging.requestNotificationPermissions( |
|
|
|
const IosNotificationSettings( |
|
|
|
sound: true, badge: true, alert: true, provisional: true)); |
|
|
|
_firebaseMessaging.onIosSettingsRegistered |
|
|
|
.listen((IosNotificationSettings settings) { |
|
|
|
print("Settings registered: $settings"); |
|
|
|
}); |
|
|
|
if (pushkey?.isEmpty ?? true) { |
|
|
|
_firebaseMessaging.getToken().then((String token) { |
|
|
|
assert(token != null); |
|
|
|
print("Push Messaging token: $token"); |
|
|
|
_userRepository.updateFcmToken(token).then((value) { |
|
|
|
print("send push key successful"); |
|
|
|
pref.saveString(DATA_CONST.PUSH_KEY, token); |
|
|
|
}); |
|
|
|
// client.put(""); |
|
|
|
}); |
|
|
|
} else { |
|
|
|
print("Don't need get push key"); |
|
|
|
} |
|
|
|
|
|
|
|
if (currentFullName?.isEmpty ?? true) { |
|
|
|
try { |
|
|
|
var currentUser = await _userRepository.getUser(); |
|
|
|
pref.saveString(DATA_CONST.CURRENT_FULL_NAME, currentUser.fullName); |
|
|
|
print("fullname: ${currentUser.fullName}"); |
|
|
|
} catch (e) { |
|
|
|
print("error: ${e.toString()}"); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@override |
|
|
|
void initState() { |
|
|
|
super.initState(); |
|
|
|
getSharedPrefs(); |
|
|
|
changeTabbar.initValue(); |
|
|
|
updateCountNotiBloc.getNotifications((data) {}, (err) {}); |
|
|
|
} |
|
|
|
|
|
|
|
_notificationNavigateOnFCM(Map<String, dynamic> message) { |
|
|
|
try { |
|
|
|
final int type = message['tbCropType']; |
|
|
|
final String contents = message['contents']; |
|
|
|
final int tbCropId = message['tbCropId']; |
|
|
|
if (contents == "ENV_UPDATE") { |
|
|
|
Get.to(PlotDetailScreen( |
|
|
|
cropType: type, |
|
|
|
cropId: tbCropId, |
|
|
|
initialIndex: 0, |
|
|
|
)); |
|
|
|
} else if (contents == "PIC_UPDATE") { |
|
|
|
Get.to(PlotDetailScreen( |
|
|
|
cropType: type, |
|
|
|
cropId: tbCropId, |
|
|
|
initialIndex: 1, |
|
|
|
)); |
|
|
|
} else { |
|
|
|
//Go home |
|
|
|
} |
|
|
|
} catch (e) { |
|
|
|
//Go home |
|
|
|
} |
|
|
|
Get.to(PlotDetailScreen( |
|
|
|
cropType: 0, |
|
|
|
cropId: 1, |
|
|
|
initialIndex: 0, |
|
|
|
)); |
|
|
|
} |
|
|
|
|
|
|
|
Widget textCountNoti() { |
|
|
|
return StreamBuilder( |
|
|
|
stream: updateCountNotiBloc.actions, |