201 lines
7.6 KiB
Dart
201 lines
7.6 KiB
Dart
import 'dart:async';
|
|
import 'dart:io';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:auto_size_text/auto_size_text.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:manager_api_new/api.dart';
|
|
import 'package:mymuseum_visitapp/Components/CustomAppBar.dart';
|
|
import 'package:mymuseum_visitapp/Components/ScannerBouton.dart';
|
|
import 'package:mymuseum_visitapp/Components/loading_common.dart';
|
|
import 'package:mymuseum_visitapp/Helpers/DatabaseHelper.dart';
|
|
import 'package:mymuseum_visitapp/Helpers/modelsHelper.dart';
|
|
import 'package:mymuseum_visitapp/Helpers/networkCheck.dart';
|
|
import 'package:mymuseum_visitapp/Helpers/requirement_state_controller.dart';
|
|
import 'package:mymuseum_visitapp/Helpers/translationHelper.dart';
|
|
import 'package:mymuseum_visitapp/Models/beaconSection.dart';
|
|
import 'package:mymuseum_visitapp/Models/visitContext.dart';
|
|
import 'package:mymuseum_visitapp/Services/apiService.dart';
|
|
import 'package:mymuseum_visitapp/Services/downloadConfiguration.dart';
|
|
import 'package:mymuseum_visitapp/app_context.dart';
|
|
import 'package:mymuseum_visitapp/client.dart';
|
|
import 'package:mymuseum_visitapp/constants.dart';
|
|
import 'package:provider/provider.dart';
|
|
|
|
import '../Tests/TestAR.dart';
|
|
import 'configurations_list.dart';
|
|
|
|
class HomePage extends StatefulWidget {
|
|
const HomePage({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
State<HomePage> createState() => _HomePageState();
|
|
}
|
|
|
|
class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
|
|
int currentIndex = 0;
|
|
|
|
List<ConfigurationDTO> configurations = [];
|
|
List<String?> alreadyDownloaded = [];
|
|
late VisitAppContext visitAppContext;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
Size size = MediaQuery.of(context).size;
|
|
final appContext = Provider.of<AppContext>(context);
|
|
visitAppContext = appContext.getContext();
|
|
|
|
return Scaffold(
|
|
appBar: CustomAppBar(
|
|
title: TranslationHelper.getFromLocale("visitTitle", appContext.getContext()),
|
|
isHomeButton: false,
|
|
),
|
|
body: SingleChildScrollView(
|
|
child: Column(
|
|
children: [
|
|
SizedBox(
|
|
width: size.width,
|
|
height: size.height,
|
|
child: FutureBuilder(
|
|
future: getConfigurationsCall(visitAppContext.clientAPI, appContext),
|
|
builder: (context, AsyncSnapshot<dynamic> snapshot) {
|
|
if (snapshot.connectionState == ConnectionState.done) {
|
|
configurations = List<ConfigurationDTO>.from(snapshot.data).where((configuration) => configuration.isMobile!).toList();
|
|
return RefreshIndicator (
|
|
onRefresh: () {
|
|
setState(() {});
|
|
return Future(() => null);
|
|
},
|
|
color: kSecondColor,
|
|
child: ConfigurationsList(
|
|
alreadyDownloaded: alreadyDownloaded,
|
|
configurations: configurations,
|
|
requestRefresh: () {
|
|
setState(() {}); // For refresh
|
|
},
|
|
),
|
|
);
|
|
} else if (snapshot.connectionState == ConnectionState.none) {
|
|
return Text(TranslationHelper.getFromLocale("noData", appContext.getContext()));
|
|
} else {
|
|
return Center(
|
|
child: Container(
|
|
height: size.height * 0.15,
|
|
child: LoadingCommon()
|
|
)
|
|
);
|
|
}
|
|
}
|
|
),
|
|
),
|
|
/*InkWell(
|
|
onTap: () {
|
|
Navigator.pushReplacement(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) {
|
|
return TestAR();
|
|
//return XRWithQRScannerPage();
|
|
},
|
|
),
|
|
);
|
|
|
|
},
|
|
child: const SizedBox(
|
|
height: 50,
|
|
width: 10,
|
|
child: Text('TEST XR'),
|
|
),
|
|
),*/
|
|
],
|
|
)
|
|
),
|
|
// floatingActionButton: ScannerBouton(appContext: appContext),
|
|
//floatingActionButtonLocation: FloatingActionButtonLocation.miniCenterFloat,
|
|
/*bottomNavigationBar: BottomNavigationBar(
|
|
currentIndex: currentIndex,
|
|
onTap: (index) {
|
|
setState(() {
|
|
currentIndex = index;
|
|
});
|
|
|
|
if (currentIndex == 0) {
|
|
controller.startScanning();
|
|
} else {
|
|
controller.pauseScanning();
|
|
controller.startBroadcasting();
|
|
}
|
|
},
|
|
items: [
|
|
BottomNavigationBarItem(
|
|
icon: Icon(Icons.list),
|
|
label: 'Scan',
|
|
),
|
|
BottomNavigationBarItem(
|
|
icon: Icon(Icons.bluetooth_audio),
|
|
label: 'Broadcast',
|
|
),
|
|
],
|
|
),*/
|
|
);
|
|
}
|
|
|
|
Future<List<ConfigurationDTO>?> getConfigurationsCall(Client client, AppContext appContext) async {
|
|
bool isOnline = await hasNetwork();
|
|
VisitAppContext visitAppContext = appContext.getContext();
|
|
|
|
List<ConfigurationDTO>? configurations;
|
|
configurations = List<ConfigurationDTO>.from(await DatabaseHelper.instance.getData(DatabaseTableType.configurations));
|
|
alreadyDownloaded = configurations.map((c) => c.id).toList();
|
|
|
|
if(!isOnline) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text(TranslationHelper.getFromLocale("noInternet", appContext.getContext())), backgroundColor: kMainColor2),
|
|
);
|
|
|
|
// GET ALL SECTIONIDS FOR ALL CONFIGURATION (OFFLINE)
|
|
for(var configuration in configurations)
|
|
{
|
|
var sections = List<SectionDTO>.from(await DatabaseHelper.instance.queryWithConfigurationId(DatabaseTableType.sections, configuration.id!));
|
|
configuration.sectionIds = sections.map((e) => e.id!).toList();
|
|
}
|
|
|
|
// GET BEACONS FROM LOCAL
|
|
List<BeaconSection> beaconSections = List<BeaconSection>.from(await DatabaseHelper.instance.getData(DatabaseTableType.beaconSection));
|
|
print("GOT beaconSection from LOCAL");
|
|
print(beaconSections);
|
|
|
|
visitAppContext.beaconSections = beaconSections;
|
|
//appContext.setContext(visitAppContext);
|
|
|
|
return configurations;
|
|
}
|
|
|
|
if(visitAppContext.beaconSections == null) {
|
|
List<SectionDTO>? sections = await ApiService.getAllBeacons(client, visitAppContext.instanceId!);
|
|
if(sections != null && sections.isNotEmpty) {
|
|
List<BeaconSection> beaconSections = sections.map((e) => BeaconSection(minorBeaconId: e.beaconId, orderInConfig: e.order, configurationId: e.configurationId, sectionId: e.id, sectionType: e.type)).toList();
|
|
visitAppContext.beaconSections = beaconSections;
|
|
|
|
try {
|
|
// Clear all before
|
|
await DatabaseHelper.instance.clearTable(DatabaseTableType.beaconSection);
|
|
// Store it locally for offline mode
|
|
for(var beaconSection in beaconSections) {
|
|
await DatabaseHelper.instance.insert(DatabaseTableType.beaconSection, ModelsHelper.beaconSectionToMap(beaconSection));
|
|
}
|
|
print("STORE beaconSection DONE");
|
|
} catch(e) {
|
|
print("Issue during beaconSection insertion");
|
|
print(e);
|
|
}
|
|
|
|
print("Got some Beacons for you");
|
|
print(beaconSections);
|
|
appContext.setContext(visitAppContext);
|
|
}
|
|
}
|
|
|
|
return await ApiService.getConfigurations(client, visitAppContext);
|
|
}
|
|
} |