347 lines
12 KiB
Dart
347 lines
12 KiB
Dart
/*
|
|
This is home page
|
|
we used AutomaticKeepAliveClientMixin to keep the state when moving from 1 navbar to another navbar, so the page is not refresh overtime
|
|
*/
|
|
|
|
import 'dart:async';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'package:badges/badges.dart' as badges;
|
|
|
|
import 'package:mobdr/main.dart';
|
|
import 'package:mobdr/config/constant.dart';
|
|
import 'package:mobdr/cubit/language/language_cubit.dart';
|
|
import 'package:mobdr/cubit/language/app_localizations.dart';
|
|
import 'package:mobdr/service/shared_prefs.dart';
|
|
import 'package:mobdr/config/global_style.dart';
|
|
import 'package:mobdr/events.dart';
|
|
import 'package:mobdr/ui/sync/sync_calendar.dart';
|
|
import 'package:mobdr/model/visite_model.dart';
|
|
import 'package:mobdr/ui/visit/visit_photo_typology.dart';
|
|
import 'package:mobdr/ui/general/chat_us.dart';
|
|
import 'package:mobdr/ui/general/notification.dart';
|
|
import 'package:mobdr/ui/reusable/reusable_widget.dart';
|
|
import 'package:mobdr/ui/reusable/cache_image_network.dart';
|
|
|
|
class TabHomePage extends StatefulWidget {
|
|
@override
|
|
_TabHomePageState createState() => _TabHomePageState();
|
|
}
|
|
|
|
class _TabHomePageState extends State<TabHomePage>
|
|
with AutomaticKeepAliveClientMixin {
|
|
// initialize global function and reusable widget
|
|
final _reusableWidget = ReusableWidget();
|
|
|
|
late StreamSubscription subVisitPhotoCountEvent;
|
|
|
|
// keep the state to do not refresh when switch navbar
|
|
@override
|
|
bool get wantKeepAlive => true;
|
|
|
|
String defaultLang = 'en';
|
|
|
|
late LanguageCubit _languageCubit;
|
|
|
|
bool _isLoading = true;
|
|
String _errorMessage = '';
|
|
|
|
late List<VisiteModel> todayVisitData = [];
|
|
late List<VisiteModel> previousVisitData = [];
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
|
|
_languageCubit = BlocProvider.of<LanguageCubit>(context);
|
|
|
|
_getLocale().then((val) {
|
|
setState(() {
|
|
defaultLang = val!;
|
|
});
|
|
});
|
|
|
|
loadData().then((_) {
|
|
setState(() {
|
|
_isLoading = false;
|
|
|
|
// Listen particular event
|
|
subVisitPhotoCountEvent =
|
|
eventBus.on<VisitPhotoCountEvent>().listen((e) {
|
|
setState(() {
|
|
for (int i = 0; i < todayVisitData.length; i++) {
|
|
if (todayVisitData[i].id_visite == e.id_visite) {
|
|
todayVisitData[i].photoCount = e.photoCount;
|
|
break;
|
|
}
|
|
}
|
|
for (int i = 0; i < previousVisitData.length; i++) {
|
|
if (previousVisitData[i].id_visite == e.id_visite) {
|
|
previousVisitData[i].photoCount = e.photoCount;
|
|
break;
|
|
}
|
|
}
|
|
});
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
subVisitPhotoCountEvent.cancel();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<String?> _getLocale() async {
|
|
final SharedPreferences _pref = await SharedPreferences.getInstance();
|
|
String? lCode = _pref.getString('lCode');
|
|
return lCode;
|
|
}
|
|
|
|
void changeLocale(Locale locale) async {
|
|
final SharedPreferences _pref = await SharedPreferences.getInstance();
|
|
await _pref.setString('lCode', locale.languageCode);
|
|
await _pref.setString('cCode', locale.countryCode!);
|
|
_languageCubit.changeLanguage(locale);
|
|
defaultLang = locale.languageCode;
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
// if we used AutomaticKeepAliveClientMixin, we must call super.build(context);
|
|
super.build(context);
|
|
final double boxImageSize = (MediaQuery.of(context).size.width / 3);
|
|
if (_isLoading) {
|
|
return Center(child: CircularProgressIndicator());
|
|
}
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
automaticallyImplyLeading: false,
|
|
elevation: GlobalStyle.appBarElevation,
|
|
title: Text(
|
|
AppLocalizations.of(context)!.translate('i18n_hello')! +
|
|
', ${SharedPrefs().prenom}',
|
|
style: GlobalStyle.appBarTitle,
|
|
),
|
|
backgroundColor: GlobalStyle.appBarBackgroundColor,
|
|
systemOverlayStyle: GlobalStyle.appBarSystemOverlayStyle,
|
|
actions: [
|
|
GestureDetector(
|
|
onTap: () {
|
|
Navigator.push(context,
|
|
MaterialPageRoute(builder: (context) => ChatUsPage()));
|
|
},
|
|
child: Icon(Icons.email, color: BLACK_GREY)),
|
|
IconButton(
|
|
icon: _reusableWidget.customNotifIcon(
|
|
count: 8, notifColor: BLACK_GREY),
|
|
onPressed: () {
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) => NotificationPage()));
|
|
}),
|
|
],
|
|
),
|
|
body: ListView(
|
|
physics: AlwaysScrollableScrollPhysics(),
|
|
children: [
|
|
_buildTodayVisits(boxImageSize),
|
|
_builPreviousVisits(boxImageSize),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildTodayVisits(boxImageSize) {
|
|
return Column(
|
|
children: [
|
|
Container(
|
|
padding: EdgeInsets.fromLTRB(16, 16, 16, 0),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
Text('Today Visits', style: GlobalStyle.horizontalTitle),
|
|
],
|
|
),
|
|
),
|
|
if (todayVisitData.length == 0)
|
|
Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
SizedBox(height: 16), // Ajout de l'espace ici
|
|
Text('Aucune visite ce jour'),
|
|
ElevatedButton(
|
|
onPressed: () {
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(builder: (context) => SyncCalendarPage()),
|
|
);
|
|
},
|
|
child: Text('Synchroniser'),
|
|
),
|
|
],
|
|
)
|
|
else
|
|
Container(
|
|
margin: EdgeInsets.only(top: 8),
|
|
height: boxImageSize * GlobalStyle.cardHeightMultiplication,
|
|
child: ListView.builder(
|
|
padding: EdgeInsets.symmetric(horizontal: 12),
|
|
scrollDirection: Axis.horizontal,
|
|
itemCount: todayVisitData.length,
|
|
itemBuilder: (BuildContext context, int index) {
|
|
return buildHorizontalVisitListCard(
|
|
context, todayVisitData[index]);
|
|
},
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _builPreviousVisits(boxImageSize) {
|
|
if (previousVisitData.length == 0) {
|
|
return SizedBox.shrink(); // Rien ne sera affiché
|
|
}
|
|
return Column(
|
|
children: [
|
|
Container(
|
|
padding: EdgeInsets.fromLTRB(16, 16, 16, 0),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
Text('Previous Visits', style: GlobalStyle.horizontalTitle),
|
|
],
|
|
),
|
|
),
|
|
Container(
|
|
margin: EdgeInsets.only(top: 8),
|
|
height: boxImageSize * GlobalStyle.cardHeightMultiplication,
|
|
child: ListView.builder(
|
|
padding: EdgeInsets.symmetric(horizontal: 12),
|
|
scrollDirection: Axis.horizontal,
|
|
itemCount: previousVisitData.length,
|
|
itemBuilder: (BuildContext context, int index) {
|
|
return buildHorizontalVisitListCard(
|
|
context, previousVisitData[index]);
|
|
},
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget buildHorizontalVisitListCard(context, data) {
|
|
final double imageWidth = (MediaQuery.of(context).size.width / 2.3);
|
|
final double imageheight = (MediaQuery.of(context).size.width / 3.07);
|
|
|
|
return Container(
|
|
width: imageWidth,
|
|
child: Card(
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(6),
|
|
),
|
|
elevation: 2,
|
|
color: Colors.white,
|
|
child: GestureDetector(
|
|
behavior: HitTestBehavior.translucent,
|
|
onTap: () {
|
|
Route route = MaterialPageRoute(
|
|
builder: (context) => VisitPhotoTypologyPage(
|
|
pp_id_distrib: data.id_distrib,
|
|
pp_langage: data.langage,
|
|
pp_id_visite: data.id_visite,
|
|
pp_name: data.name,
|
|
),
|
|
);
|
|
Navigator.push(context, route);
|
|
},
|
|
child: Stack(
|
|
children: [
|
|
Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: <Widget>[
|
|
ClipRRect(
|
|
borderRadius: BorderRadius.only(
|
|
topLeft: Radius.circular(10),
|
|
topRight: Radius.circular(6)),
|
|
child: buildCacheNetworkImage(
|
|
width: imageWidth,
|
|
height: imageheight,
|
|
url: data.image)),
|
|
Container(
|
|
margin: EdgeInsets.all(8),
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.start,
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Container(
|
|
height: 36,
|
|
child: Text(data.name,
|
|
style: GlobalStyle.cardTitle,
|
|
maxLines: 2,
|
|
overflow: TextOverflow.ellipsis),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
Positioned(
|
|
bottom: 4,
|
|
right: 4, // alignement à droite
|
|
child: GestureDetector(
|
|
onTap: () {
|
|
// TODO si visite validée ce n'est pas la meme url (on il mettre date validation dans le json frederik
|
|
SharedPrefs().urlMP4 =
|
|
'${ApiConstants.baseUrl}/MobilePortal4/index.html#ajax/visite_modification.html?visite=${data.id_visite}';
|
|
eventBus.fire(UrlEvent(SharedPrefs().urlMP4));
|
|
},
|
|
child: Image.asset(
|
|
"assets/images/logo_mp4.png",
|
|
width: 24,
|
|
height: 24,
|
|
),
|
|
),
|
|
),
|
|
if (data.photoCount != 0)
|
|
Positioned(
|
|
bottom: 0,
|
|
left: 2, // alignement à gauche
|
|
child: Container(
|
|
margin: EdgeInsets.symmetric(vertical: 2),
|
|
child: badges.Badge(
|
|
badgeStyle: badges.BadgeStyle(
|
|
badgeColor: Colors.blue,
|
|
padding: EdgeInsets.all(data.photoCount >= 10 ? 2 : 6),
|
|
),
|
|
badgeContent: Text(data.photoCount.toString(),
|
|
style: TextStyle(color: Colors.white)),
|
|
child: Icon(Icons.camera_alt_sharp),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
/// Initializes data when the page loads.
|
|
Future<void> loadData() async {
|
|
try {
|
|
// visite model initialisation
|
|
todayVisitData = await VisiteModel.getTodayVisit();
|
|
previousVisitData = await VisiteModel.getPreviousVisit();
|
|
} catch (e) {
|
|
// set errorMessage for debug
|
|
_errorMessage = 'Error loading visites : $e';
|
|
}
|
|
}
|
|
}
|