235 lines
9.9 KiB
Dart
235 lines
9.9 KiB
Dart
import 'dart:convert';
|
|
import 'dart:developer';
|
|
import 'dart:typed_data';
|
|
|
|
//import 'package:confetti/confetti.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:manager_api_new/api.dart';
|
|
import 'package:mymuseum_visitapp/Components/CustomAppBar.dart';
|
|
import 'package:mymuseum_visitapp/Components/loading_common.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/resourceModel.dart';
|
|
import 'package:mymuseum_visitapp/Models/visitContext.dart';
|
|
import 'package:mymuseum_visitapp/Screens/Sections/Article/article_page.dart';
|
|
import 'package:mymuseum_visitapp/Screens/Sections/PDF/pdf_view.dart';
|
|
import 'package:mymuseum_visitapp/Screens/Sections/Puzzle/puzzle_page.dart';
|
|
import 'package:mymuseum_visitapp/Screens/Sections/Quiz/quizz_page.dart';
|
|
import 'package:mymuseum_visitapp/Screens/Sections/Slider/slider_view.dart';
|
|
import 'package:mymuseum_visitapp/Screens/Sections/Video/video_page.dart';
|
|
import 'package:mymuseum_visitapp/Screens/Sections/Web/web_page.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 SectionPage extends StatefulWidget {
|
|
const SectionPage({Key? key, required this.rawSection, required this.visitAppContextIn, required this.configuration, required this.sectionId}) : super(key: key);
|
|
|
|
final Object? rawSection;
|
|
final String sectionId;
|
|
final ConfigurationDTO configuration;
|
|
final VisitAppContext visitAppContextIn;
|
|
|
|
@override
|
|
State<SectionPage> createState() => _SectionPageState();
|
|
}
|
|
|
|
class _SectionPageState extends State<SectionPage> {
|
|
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
|
|
SectionDTO? sectionDTO;
|
|
late VisitAppContext visitAppContext;
|
|
late dynamic rawSectionData;
|
|
List<ResourceModel?> resourcesModel = <ResourceModel?>[];
|
|
|
|
@override
|
|
void initState() {
|
|
widget.visitAppContextIn.isContentCurrentlyShown = true;
|
|
super.initState();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
visitAppContext.isContentCurrentlyShown = false;
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final appContext = Provider.of<AppContext>(context);
|
|
Size size = MediaQuery.of(context).size;
|
|
visitAppContext = appContext.getContext();
|
|
|
|
var test = SectionDTO.fromJson(jsonDecode(jsonEncode(widget.rawSection)));
|
|
|
|
return Scaffold(
|
|
key: _scaffoldKey,
|
|
appBar: test!.type != SectionType.Quiz && test.type != SectionType.Article && test.type != SectionType.Web && test.type != SectionType.Pdf && test.type != SectionType.Video && test.type != SectionType.Puzzle && test.type != SectionType.Slider ? CustomAppBar(
|
|
title: sectionDTO != null ? TranslationHelper.get(sectionDTO!.title, visitAppContext) : "",
|
|
isHomeButton: false,
|
|
) : null,
|
|
body: OrientationBuilder(
|
|
builder: (context, orientation) {
|
|
return FutureBuilder(
|
|
future: getSectionDetail(appContext, visitAppContext.clientAPI, widget.sectionId),
|
|
builder: (context, AsyncSnapshot<dynamic> snapshot) {
|
|
var sectionResult = snapshot.data;
|
|
if(sectionDTO != null && sectionResult != null) {
|
|
switch(sectionDTO!.type) {
|
|
case SectionType.Article:
|
|
ArticleDTO articleDTO = ArticleDTO.fromJson(sectionResult)!;
|
|
return ArticlePage(visitAppContextIn: widget.visitAppContextIn, articleDTO: articleDTO, resourcesModel: resourcesModel);
|
|
case SectionType.Quiz:
|
|
QuizDTO quizDTO = QuizDTO.fromJson(sectionResult)!;
|
|
return QuizPage(visitAppContextIn: widget.visitAppContextIn, quizDTO: quizDTO, resourcesModel: resourcesModel);
|
|
case SectionType.Web:
|
|
WebDTO webDTO = WebDTO.fromJson(sectionResult)!;
|
|
return WebPage(section: webDTO);
|
|
case SectionType.Pdf:
|
|
PdfDTO pdfDTO = PdfDTO.fromJson(sectionResult)!;
|
|
return PDFPage(section: pdfDTO);
|
|
case SectionType.Video:
|
|
VideoDTO videoDTO = VideoDTO.fromJson(sectionResult)!;
|
|
return VideoPage(section: videoDTO);
|
|
case SectionType.Puzzle:
|
|
PuzzleDTO puzzleDTO = PuzzleDTO.fromJson(sectionResult)!;
|
|
return PuzzlePage(section: puzzleDTO);
|
|
case SectionType.Slider:
|
|
SliderDTO sliderDTO = SliderDTO.fromJson(sectionResult)!;
|
|
return SliderPage(section: sliderDTO);
|
|
default:
|
|
return const Center(child: Text("Unsupported type"));
|
|
}
|
|
} else {
|
|
return const LoadingCommon();
|
|
}
|
|
}
|
|
);
|
|
}
|
|
)
|
|
);
|
|
}
|
|
|
|
Future<dynamic> getSectionDetail(AppContext appContext, Client client, String sectionId) async {
|
|
try {
|
|
bool isConfigOffline = widget.configuration.isOffline!;
|
|
if(widget.rawSection == null) {
|
|
if(isConfigOffline)
|
|
{
|
|
// OFFLINE
|
|
List<Map<String, dynamic>> sectionTest = await DatabaseHelper.instance.queryWithColumnId(DatabaseTableType.sections, sectionId);
|
|
if(sectionTest.isNotEmpty) {
|
|
sectionDTO = DatabaseHelper.instance.getSectionFromDB(sectionTest.first);
|
|
try {
|
|
SectionRead sectionRead = SectionRead(id: sectionDTO!.id!, readTime: DateTime.now().millisecondsSinceEpoch);
|
|
await DatabaseHelper.instance.insert(DatabaseTableType.articleRead, sectionRead.toMap());
|
|
visitAppContext.readSections.add(sectionRead);
|
|
|
|
appContext.setContext(visitAppContext);
|
|
} catch (e) {
|
|
print("DATABASE ERROR SECTIONREAD");
|
|
print(e);
|
|
}
|
|
} else {
|
|
print("EMPTY SECTION");
|
|
}
|
|
} else
|
|
{
|
|
// ONLINE
|
|
rawSectionData = await client.sectionApi!.sectionGetDetail(sectionId);
|
|
SectionDTO sectionOnline = jsonDecode(jsonEncode(rawSectionData)).map((json) => SectionDTO.fromJson(json)).whereType<SectionDTO>().toList();
|
|
sectionDTO = sectionOnline;
|
|
}
|
|
|
|
/*setState(() {
|
|
//print(sectionDTO!.title);
|
|
});*/
|
|
} else {
|
|
rawSectionData = widget.rawSection;
|
|
sectionDTO = SectionDTO.fromJson(jsonDecode(jsonEncode(rawSectionData)));
|
|
}
|
|
|
|
switch(sectionDTO!.type)
|
|
{
|
|
case SectionType.Quiz:
|
|
QuizDTO? quizDTO = QuizDTO.fromJson(rawSectionData);
|
|
if(quizDTO != null) {
|
|
quizDTO.questions!.sort((a, b) => a.order!.compareTo(b.order!));
|
|
if(quizDTO.questions != null && quizDTO.questions!.isNotEmpty) {
|
|
quizDTO.questions!.sort((a, b) => a.order!.compareTo(b.order!));
|
|
for (var question in quizDTO.questions!) {
|
|
if(isConfigOffline)
|
|
{
|
|
// OFFLINE
|
|
if(question.imageBackgroundResourceId != null) {
|
|
List<Map<String, dynamic>> ressourceQuizz = await DatabaseHelper.instance.queryWithColumnId(DatabaseTableType.resources, question.imageBackgroundResourceId!);
|
|
if(ressourceQuizz.isNotEmpty) {
|
|
resourcesModel.add(DatabaseHelper.instance.getResourceFromDB(ressourceQuizz.first));
|
|
} else {
|
|
print("EMPTY resourcesModel - second");
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// ONLINE
|
|
if(question.imageBackgroundResourceId != null) {
|
|
resourcesModel.add(ResourceModel(id: question.imageBackgroundResourceId, source: question.imageBackgroundResourceUrl, type: ResourceType.Image));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
case SectionType.Article:
|
|
ArticleDTO articleDTO = ArticleDTO.fromJson(rawSectionData)!;
|
|
var audioToDownload = articleDTO.audioIds!.firstWhere((a) => a.language == visitAppContext.language!);
|
|
if(audioToDownload.value != null) {
|
|
if(isConfigOffline)
|
|
{
|
|
// OFFLINE
|
|
List<Map<String, dynamic>> ressourceArticle = await DatabaseHelper.instance.queryWithColumnId(DatabaseTableType.resources, audioToDownload.value!);
|
|
if(ressourceArticle.isNotEmpty) {
|
|
resourcesModel.add(DatabaseHelper.instance.getResourceFromDB(ressourceArticle.first));
|
|
} else {
|
|
print("EMPTY resourcesModel - second");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// ONLINE
|
|
ResourceDTO? resourceDTO = await client.resourceApi!.resourceGetDetail(audioToDownload.value!);
|
|
if(resourceDTO != null && resourceDTO.url != null) {
|
|
// ONLINE
|
|
//ResourceModel? resourceAudioOnline = await ApiService.downloadAudio(client, resourceDTO.url!, resourceDTO.id!);
|
|
ResourceModel resourceAudioOnline = ResourceModel();
|
|
resourceAudioOnline.id = resourceDTO.id;
|
|
resourceAudioOnline.source = resourceDTO.url;
|
|
resourceAudioOnline.type = ResourceType.Audio;
|
|
|
|
resourcesModel.add(resourceAudioOnline);
|
|
|
|
/*Uint8List base64String = base64Decode(resourceAudioOnline.path!); // GET FROM FILE
|
|
audiobytes = base64String;*/
|
|
} else {
|
|
print("EMPTY resourcesModel online - audio");
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
return rawSectionData;
|
|
|
|
} catch (e) {
|
|
print(e);
|
|
print("IN CATCH");
|
|
return null;
|
|
}
|
|
}
|
|
}
|