172 lines
6.5 KiB
Dart
172 lines
6.5 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:manager_api/api.dart';
|
|
import 'package:mymuseum_visitapp/Components/Loading.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/articleRead.dart';
|
|
import 'package:mymuseum_visitapp/Models/visitContext.dart';
|
|
import 'package:mymuseum_visitapp/Screens/Article/article.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.configurationId}) : super(key: key);
|
|
|
|
final String? configurationId;
|
|
|
|
@override
|
|
State<Body> createState() => _BodyState();
|
|
}
|
|
|
|
class _BodyState extends State<Body> {
|
|
List<SectionDTO> sections = [];
|
|
List<SectionDTO> sectionsToDisplay = [];
|
|
String? searchValue;
|
|
int? searchNumberValue;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final appContext = Provider.of<AppContext>(context);
|
|
Size size = MediaQuery.of(context).size;
|
|
|
|
return SafeArea(
|
|
bottom: false,
|
|
child: Column(
|
|
children: <Widget>[
|
|
Row(
|
|
children: [
|
|
SearchBox(onChanged: (value) {
|
|
setState(() {
|
|
if(value != null && value != "") {
|
|
searchValue = value;
|
|
} else {
|
|
searchValue = null;
|
|
}
|
|
});
|
|
}),
|
|
Expanded(
|
|
child: SearchNumberBox(onChanged: (value) {
|
|
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(
|
|
color: kBackgroundColor,
|
|
borderRadius: BorderRadius.only(
|
|
topLeft: Radius.circular(40),
|
|
topRight: Radius.circular(40),
|
|
),
|
|
),
|
|
),
|
|
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(!(appContext.getContext() as VisitAppContext).configuration!.isOffline!) {
|
|
// Force refresh if online
|
|
setState(() {});
|
|
} },
|
|
child: ListView.builder(
|
|
itemCount: sectionsToDisplay.length,
|
|
itemBuilder: (context, index) => SectionCard(
|
|
itemCount: sectionsToDisplay.length,
|
|
itemIndex: index,
|
|
sectionDTO: sectionsToDisplay[index],
|
|
press: () {
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) => ArticlePage(
|
|
visitAppContextIn: appContext.getContext(),
|
|
articleId: sectionsToDisplay[index].id!,
|
|
),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
),
|
|
);
|
|
} else if (snapshot.connectionState == ConnectionState.none) {
|
|
return Text(TranslationHelper.getFromLocale("noData", appContext.getContext()));
|
|
} else {
|
|
return Center(
|
|
child: Container(
|
|
height: size.height * 0.15,
|
|
child: Loading()
|
|
)
|
|
);
|
|
}
|
|
}
|
|
)
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
getSections(AppContext appContext) async {
|
|
VisitAppContext visitAppContext = (appContext.getContext() as VisitAppContext);
|
|
if(visitAppContext.configuration!.isOffline!)
|
|
{
|
|
// OFFLINE
|
|
sections = List<SectionDTO>.from(await DatabaseHelper.instance.getData(DatabaseTableType.sections));
|
|
}
|
|
else
|
|
{
|
|
// ONLINE
|
|
List<SectionDTO>? sectionsDownloaded = await ApiService.getAllSections(appContext.clientAPI, visitAppContext.configuration!.id!);
|
|
print(sectionsDownloaded);
|
|
if(sectionsDownloaded!.isNotEmpty) {
|
|
sections = sectionsDownloaded.where((s) => s.type == SectionType.Article).toList(); // HERE TODO IF support more than article type
|
|
print(sections);
|
|
}
|
|
}
|
|
|
|
sections = sections.where((s) => s.configurationId == widget.configurationId).toList();
|
|
sections.sort((a,b) => a.order!.compareTo(b.order!));
|
|
sectionsToDisplay = sections;
|
|
|
|
visitAppContext.currentSections = sectionsToDisplay;
|
|
|
|
if(searchValue != '' && searchValue != null) {
|
|
sectionsToDisplay = sections.where((s) => TranslationHelper.get(s.title, appContext.getContext()).toLowerCase().contains(searchValue.toString().toLowerCase())).toList();
|
|
} else {
|
|
if(searchNumberValue != null) {
|
|
sectionsToDisplay = sections.where((s) => s.order!+1 == searchNumberValue).toList();
|
|
} else {
|
|
sectionsToDisplay = sections;
|
|
}
|
|
}
|
|
}
|
|
}
|