285 lines
11 KiB
Dart
285 lines
11 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
import 'package:auto_size_text/auto_size_text.dart';
|
|
import 'package:manager_api/api.dart';
|
|
import 'package:mymuseum_visitapp/Components/CustomAppBar.dart';
|
|
import 'package:mymuseum_visitapp/Components/Loading.dart';
|
|
import 'package:mymuseum_visitapp/Components/ScannerBouton.dart';
|
|
import 'package:mymuseum_visitapp/Helpers/DatabaseHelper.dart';
|
|
import 'package:mymuseum_visitapp/Helpers/networkCheck.dart';
|
|
import 'package:mymuseum_visitapp/Models/resourceModel.dart';
|
|
import 'package:mymuseum_visitapp/Models/visitContext.dart';
|
|
import 'package:mymuseum_visitapp/Screens/Visit/visit.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';
|
|
|
|
class HomePage extends StatefulWidget {
|
|
const HomePage({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
State<HomePage> createState() => _HomePageState();
|
|
}
|
|
|
|
class _HomePageState extends State<HomePage> {
|
|
List<ConfigurationDTO> configurations = [];
|
|
List<String?> alreadyDownloaded = [];
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
Size size = MediaQuery.of(context).size;
|
|
final appContext = Provider.of<AppContext>(context);
|
|
return Scaffold(
|
|
appBar: CustomAppBar(
|
|
title: "Home page - liste parcours",
|
|
isHomeButton: false,
|
|
),
|
|
body: SingleChildScrollView(
|
|
child: Container(
|
|
width: size.width,
|
|
height: size.height,
|
|
child: FutureBuilder(
|
|
future: getConfigurations(appContext.clientAPI),
|
|
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: ListView.builder(
|
|
shrinkWrap: true, //I've set this as true here depending on what your listview content is
|
|
//physics: NeverScrollableScrollPhysics(),//This prevents scrolling, but may inhibit refresh indicator, remove as you need
|
|
itemBuilder: (BuildContext context, int index) {
|
|
return InkWell(
|
|
onTap: () {
|
|
setState(() {
|
|
print(configurations[index].label);
|
|
// Update context
|
|
VisitAppContext visitAppContext = appContext.getContext();
|
|
visitAppContext.configuration = configurations[index];
|
|
appContext.setContext(visitAppContext);
|
|
|
|
Navigator.of(context).pushReplacement(MaterialPageRoute(
|
|
builder: (context) => VisitPage(configurationId: configurations[index].id!),
|
|
));
|
|
});
|
|
},
|
|
child: Container(
|
|
height: size.height*0.15,
|
|
decoration: boxDecoration(configurations[index], false),
|
|
margin: const EdgeInsets.symmetric(vertical: 10, horizontal: 20),
|
|
child: Stack(
|
|
children: [
|
|
Align(
|
|
alignment: Alignment.topLeft,
|
|
child: Padding(
|
|
padding: const EdgeInsets.only(top: 20, left: 10),
|
|
child: AutoSizeText(
|
|
configurations[index].label!,
|
|
style: const TextStyle(fontSize: kMenuTitleDetailSize),
|
|
maxLines: 1,
|
|
),
|
|
),
|
|
),
|
|
Positioned(
|
|
bottom: 0,
|
|
right: 0,
|
|
child: Container(
|
|
width: 45,
|
|
height: 45,
|
|
decoration: BoxDecoration(
|
|
shape: BoxShape.rectangle,
|
|
color: kMainColor,
|
|
borderRadius: BorderRadius.circular(20.0),
|
|
),
|
|
margin: const EdgeInsets.all(8),
|
|
child: InkWell(
|
|
onTap: () async {
|
|
downloadClicked(appContext, configurations[index]);
|
|
},
|
|
child: configurations[index].isOffline! && !alreadyDownloaded.any((c) => c == configurations[index].id) ?
|
|
const Icon(Icons.download, color: Colors.white) : const Icon(Icons.refresh, color: Colors.white),
|
|
),
|
|
/*AutoSizeText(
|
|
configurations[index].isOffline.toString(),
|
|
style: const TextStyle(fontSize: kMenuDescriptionDetailSize, fontFamily: ""),
|
|
maxLines: 1,
|
|
),*/
|
|
)
|
|
)
|
|
],
|
|
),
|
|
),
|
|
);
|
|
},
|
|
itemCount: configurations.length
|
|
),
|
|
);
|
|
} else if (snapshot.connectionState == ConnectionState.none) {
|
|
return Text("No data");
|
|
} else {
|
|
return Center(
|
|
child: Container(
|
|
height: size.height * 0.15,
|
|
child: Loading()
|
|
)
|
|
);
|
|
}
|
|
}
|
|
),
|
|
),
|
|
),
|
|
floatingActionButton: const ScannerBouton(isReplacement: false),
|
|
//floatingActionButtonLocation: FloatingActionButtonLocation.miniCenterFloat,
|
|
);
|
|
}
|
|
|
|
Future<List<ConfigurationDTO>?> getConfigurations(Client client) async {
|
|
try {
|
|
List<ConfigurationDTO>? configurations;
|
|
bool isOnline = await hasNetwork();
|
|
configurations = List<ConfigurationDTO>.from(await DatabaseHelper.instance.getData(DatabaseTableType.configurations));
|
|
alreadyDownloaded = configurations.map((c) => c.id).toList();
|
|
|
|
if(isOnline) {
|
|
//var client = new Client("http://192.168.31.140:8089"); // TODO REMOVE
|
|
configurations = await client.configurationApi!.configurationGet();
|
|
return configurations ?? [];
|
|
} else {
|
|
return configurations ?? []; // TODO return local list..
|
|
}
|
|
} catch (e) {
|
|
print(e);
|
|
print("IN CATCH");
|
|
return [];
|
|
}
|
|
}
|
|
|
|
Future<List<SectionDTO>?> getAllSections(Client client, String configurationId) async {
|
|
try {
|
|
bool isOnline = await hasNetwork();
|
|
if(isOnline) {
|
|
List<SectionDTO>? sections = await client.sectionApi!.sectionGetFromConfiguration(configurationId);
|
|
return sections;
|
|
} else {
|
|
return []; // TODO return local list..
|
|
}
|
|
} catch (e) {
|
|
print(e);
|
|
print("IN CATCH");
|
|
return [];
|
|
}
|
|
}
|
|
|
|
Future<void> getAndDownloadImage(Client client, ImageDTO imageDTO) async {
|
|
try {
|
|
bool isOnline = await hasNetwork();
|
|
if(isOnline) {
|
|
var source = imageDTO.source_;
|
|
if(imageDTO.source_!.contains("localhost:5000")) {
|
|
source = imageDTO.source_!.replaceAll("http://localhost:5000", client.apiApi!.basePath);
|
|
}
|
|
HttpClient client2 = HttpClient();
|
|
var _downloadData = <int>[];
|
|
client2.getUrl(Uri.parse(source!))
|
|
.then((HttpClientRequest request) {
|
|
return request.close();
|
|
})
|
|
.then((HttpClientResponse response) {
|
|
response.listen((d) => _downloadData.addAll(d),
|
|
onDone: () async {
|
|
final base64Str = base64.encode(_downloadData);
|
|
ResourceModel resourceModel = ResourceModel(id: imageDTO.resourceId, data: base64Str, type: ResourceType.Image);
|
|
await DatabaseHelper.instance.insert(DatabaseTableType.resources, resourceModel.toMap());
|
|
//var test = base64.decode(base64Str);
|
|
//return base64Str;
|
|
/*setState(() {
|
|
base64TEST = base64Str;
|
|
});*/
|
|
}
|
|
);
|
|
});
|
|
|
|
//return file;
|
|
} else {
|
|
//return null; // TODO return local list..
|
|
}
|
|
} catch (e) {
|
|
print(e);
|
|
print("IN CATCH");
|
|
//return [];
|
|
}
|
|
}
|
|
|
|
Future<void> downloadClicked(AppContext appContext, ConfigurationDTO configuration) async {
|
|
// Display config only downloaded - TODO
|
|
|
|
// Update local DB - Configuration
|
|
await DatabaseHelper.instance.insert(DatabaseTableType.configurations, configuration.toJson());
|
|
|
|
List<SectionDTO>? sections = await getAllSections(appContext.clientAPI, configuration.id!);
|
|
|
|
if(sections!.isNotEmpty) {
|
|
// Update local DB - Sections
|
|
for(var section in sections) {
|
|
// TODO handle other type of section
|
|
if(section.type == SectionType.Article) {
|
|
await DatabaseHelper.instance.insert(DatabaseTableType.sections, sectionToMap(section));
|
|
|
|
// Download all images..
|
|
ArticleDTO? articleDTO = ArticleDTO.fromJson(jsonDecode(section.data!));
|
|
|
|
if(articleDTO != null) {
|
|
for(var image in articleDTO!.images!) {
|
|
await getAndDownloadImage(appContext.clientAPI, image);
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
Map<String, dynamic> sectionToMap(SectionDTO section) {
|
|
return {
|
|
'id': section.id,
|
|
'label': section.label,
|
|
'title': jsonEncode(section.title),
|
|
'description': jsonEncode(section.description),
|
|
'imageId': section.imageId,
|
|
'imageSource': section.imageSource,
|
|
'configurationId': section.configurationId,
|
|
'isSubSection': section.isSubSection,
|
|
'parentId': section.parentId,
|
|
'type': section.type!.value,
|
|
'data': section.data,
|
|
'dateCreation': section.dateCreation!.toUtc().toIso8601String(),
|
|
'orderOfElement': section.order,
|
|
//'token': token
|
|
};
|
|
}
|
|
}
|
|
|
|
boxDecoration(ConfigurationDTO configuration, bool isSelected) { // TODO to change
|
|
return BoxDecoration(
|
|
color: kSecondColor,
|
|
shape: BoxShape.rectangle,
|
|
borderRadius: BorderRadius.circular(20.0),
|
|
boxShadow: const [
|
|
BoxShadow(
|
|
color: kSecondColor,
|
|
spreadRadius: 0.15,
|
|
blurRadius: 3.5,
|
|
offset: Offset(0, 1), // changes position of shadow
|
|
),
|
|
],
|
|
);
|
|
} |