2025-06-10 16:51:48 +02:00

296 lines
12 KiB
Dart

import 'dart:convert';
import 'package:diacritic/diacritic.dart';
import 'package:flutter/material.dart';
import 'package:manager_api_new/api.dart';
import 'package:mymuseum_visitapp/Components/SlideFromRouteRight.dart';
import 'package:mymuseum_visitapp/Components/loading_common.dart';
import 'package:mymuseum_visitapp/Components/SearchBox.dart';
import 'package:mymuseum_visitapp/Components/SearchNumberBox.dart';
import 'package:mymuseum_visitapp/Helpers/DatabaseHelper.dart';
import 'package:mymuseum_visitapp/Helpers/translationHelper.dart';
import 'package:mymuseum_visitapp/Models/visitContext.dart';
import 'package:mymuseum_visitapp/Screens/section_page.dart';
import 'package:mymuseum_visitapp/Services/apiService.dart';
import 'package:mymuseum_visitapp/app_context.dart';
import 'package:mymuseum_visitapp/constants.dart';
import 'package:provider/provider.dart';
import 'section_card.dart';
class Body extends StatefulWidget {
const Body({Key? key, required this.configuration}) : super(key: key);
final ConfigurationDTO configuration;
@override
State<Body> createState() => _BodyState();
}
class _BodyState extends State<Body> {
late List<SectionDTO> sections;
late List<SectionDTO> _allSections;
late List<dynamic> rawSections;
List<SectionDTO> sectionsToDisplay = [];
String? searchValue;
int? searchNumberValue;
final ValueNotifier<List<SectionDTO>> filteredSections = ValueNotifier([]);
@override
Widget build(BuildContext context) {
final appContext = Provider.of<AppContext>(context);
VisitAppContext visitAppContext = appContext.getContext();
Size size = MediaQuery.of(context).size;
return SafeArea(
bottom: false,
top: false,
child: Stack(
children: [
Hero(
tag: widget.configuration.id!,
child: Container(
height: size.height * 0.28,
decoration: BoxDecoration(
boxShadow: const [
BoxShadow(
color: kMainGrey,
spreadRadius: 0.5,
blurRadius: 5,
offset: Offset(0, 1), // changes position of shadow
),
],
image: widget.configuration.imageSource != null ? DecorationImage(
fit: BoxFit.cover,
opacity: 0.65,
image: NetworkImage(
widget.configuration.imageSource!,
),
): null,
),
),
),
Column(
children: <Widget>[
SizedBox(
height: size.height * 0.11,
width: size.width,
child: Stack(
fit: StackFit.expand,
children: [
Positioned(
top: 35,
left: 10,
child: SizedBox(
width: 50,
height: 50,
child: InkWell(
onTap: () {
//setState(() {
visitAppContext.configuration = null;
visitAppContext.isScanningBeacons = false;
Navigator.of(context).pop();
/*Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(
builder: (context) => const HomePage3(),
),(route) => false);*/
//});
},
child: Container(
decoration: const BoxDecoration(
color: kMainColor,
shape: BoxShape.circle,
),
child: const Icon(Icons.arrow_back, size: 23, color: Colors.white)
),
)
),
),
],
),
),
Row(
children: [
SearchBox(onChanged: (value) {
searchValue = value?.trim();
applyFilters(visitAppContext);
/*setState(() {
if(value != null && value != "") {
searchValue = value;
} else {
searchValue = null;
}
});*/
}),
Expanded(
child: SearchNumberBox(onChanged: (value) {
if (value != null && value.isNotEmpty) {
searchNumberValue = int.tryParse(value);
} else {
searchNumberValue = null;
}
FocusScope.of(context).unfocus();
applyFilters(visitAppContext);
}
/*setState(() {
if(value != null && value != "") {
searchNumberValue = int.parse(value);
} else {
searchNumberValue = null;
FocusScope.of(context).unfocus();
}
});
}*/
),
),
],
),
//const SizedBox(height: kDefaultPadding / 2),
Expanded(
child: Stack(
children: <Widget>[
// Our background
Container(
margin: const EdgeInsets.only(top: 0),
decoration: const BoxDecoration(
boxShadow: [
BoxShadow(
color: kMainGrey,
spreadRadius: 0.5,
blurRadius: 2,
offset: Offset(0, 1), // changes position of shadow
),
],
color: kBackgroundColor,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(30),
topRight: Radius.circular(30),
),
),
),
FutureBuilder(
future: getSections(appContext),
builder: (context, AsyncSnapshot<dynamic> snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
/*print("SECTIONTODISPA");
print(sectionsToDisplay);*/
return Padding(
padding: const EdgeInsets.only(bottom: 0),
child: RefreshIndicator(
onRefresh: () async {
if(!widget.configuration.isOffline!) {
// Force refresh if online
setState(() {});
} },
child: ValueListenableBuilder<List<SectionDTO>>(
valueListenable: filteredSections,
builder: (context, value, child) {
return ListView.builder(
itemCount: value.length,
itemBuilder: (context, index) => SectionCard(
configuration: widget.configuration,
itemCount: value.length,
itemIndex: index,
sectionDTO: value[index],
press: () {
Navigator.push(
context,
SlideFromRightRoute(page: SectionPage(
configuration: widget.configuration,
rawSection: rawSections[index],
visitAppContextIn: appContext.getContext(),
sectionId: value[index].id!,
)),
);
},
),
);
}
)
),
);
} else if (snapshot.connectionState == ConnectionState.none) {
return Text(TranslationHelper.getFromLocale("noData", appContext.getContext()));
} else {
return Center(
child: SizedBox(
height: size.height * 0.15,
child: const LoadingCommon()
)
);
}
}
)
],
),
),
],
),
],
),
);
}
getSections(AppContext appContext) async {
VisitAppContext visitAppContext = appContext.getContext();
if(widget.configuration.isOffline!)
{
// OFFLINE
sections = List<SectionDTO>.from(await DatabaseHelper.instance.getData(DatabaseTableType.sections));
}
else
{
// ONLINE
List<dynamic>? sectionsDownloaded = await ApiService.getAllSections(visitAppContext.clientAPI, widget.configuration.id!);
rawSections = jsonDecode(jsonEncode(sectionsDownloaded));
var rawToSection = jsonDecode(jsonEncode(rawSections)).map((json) => SectionDTO.fromJson(json)).toList();
List<SectionDTO> sectionList = rawToSection.whereType<SectionDTO>().toList();
//print(sectionsDownloaded);
if(sectionList.isNotEmpty) {
sections = sectionList.toList();
//print(sections);
}
}
sections = sections.where((s) => s.configurationId == widget.configuration.id!).toList();
sections.sort((a,b) => a.order!.compareTo(b.order!));
//sectionsToDisplay = sections;
try {
_allSections = sections;
//visitAppContext.currentSections = sectionsToDisplay;
applyFilters(visitAppContext);
} catch(e) {
print(e);
}
/*if(searchValue != '' && searchValue != null) {
sectionsToDisplay = sections.where((s) => removeDiacritics(TranslationHelper.get(s.title, appContext.getContext()).toLowerCase()).contains(removeDiacritics(searchValue.toString().toLowerCase()))).toList();
} else {
if(searchNumberValue != null) {
sectionsToDisplay = sections.where((s) => s.order!+1 == searchNumberValue).toList();
} else {
sectionsToDisplay = sections;
}
}*/
}
void applyFilters(VisitAppContext visitAppContext) {
List<SectionDTO> result = _allSections;
if (searchValue != null && searchValue!.isNotEmpty) {
result = result.where((s) =>
removeDiacritics(TranslationHelper.get(s.title, visitAppContext).toLowerCase())
.contains(removeDiacritics(searchValue!.toLowerCase()))
).toList();
} else if (searchNumberValue != null) {
result = result.where((s) => s.order! + 1 == searchNumberValue).toList();
}
filteredSections.value = result;
}
}