// GCG.MeinCantor - Die Schulplattform für Cantorianer. // Copyright (C) 2021-2022 Georg-Cantor-Gymnasium Halle (Saale) // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as published // by the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Affero General Public License for more details. // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . import 'dart:convert'; import 'package:meincantor/networking.dart'; import 'package:flutter/material.dart'; import 'package:flutter_markdown/flutter_markdown.dart'; import 'package:http/http.dart' as http; import 'package:material_design_icons_flutter/material_design_icons_flutter.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:url_launcher/url_launcher.dart'; import 'package:markdown/markdown.dart' as md; Future getSZread() async { SharedPreferences prefs = await SharedPreferences.getInstance(); String? szReadString = prefs.getString("SZread"); List szRead; if (szReadString == null || (jsonDecode(szReadString) as List).isEmpty) { szRead = []; } else { szRead = jsonDecode(szReadString) as List; } return szRead; } class SZ extends StatefulWidget { const SZ({Key? key}) : super(key: key); @override State createState() => _SZState(); } class _SZState extends State { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text("Schülerzeitung"), centerTitle: true, ), body: LayoutBuilder(builder: (context, constraints) { double widgetWidth = constraints.maxWidth; int factor; if (widgetWidth <= 600) { factor = 1; } else if (widgetWidth <= 1400) { factor = 2; } else if (widgetWidth <= 2000) { factor = 3; } else { factor = 1; } return Center( heightFactor: 1, child: Container( constraints: BoxConstraints( maxWidth: MediaQuery.of(context).size.width / factor, ), child: FutureBuilder( future: getArticles(), builder: (context, snapshot) { if (snapshot.hasData) { int statusCode = snapshot.data!.statusCode; if (statusCode == 200) { String data = utf8.decode(snapshot.data!.bodyBytes); List articles = jsonDecode(data)["data"]; List articleTiles = []; for (var element in articles) { int id = element["id"]; element = element["attributes"]; Color color = Colors.white70; Widget card = FutureBuilder( future: getSZread(), builder: (context, snapshot) { if (snapshot.hasData) { List readList = snapshot.data! as List; if (!readList.contains(id)) { return GestureDetector( onTap: () async { SharedPreferences prefs = await SharedPreferences.getInstance(); Navigator.push( context, MaterialPageRoute( builder: (context) => Article.fromData( element["title"], element["content"], element["author"], element["publish_date"]) .widget), ); readList.add(id); prefs.setString( "SZread", jsonEncode(readList)); setState(() { color = Colors.transparent; }); }, child: Card( color: color, child: Padding( padding: const EdgeInsets.fromLTRB( 10, 10, 10, 10), child: FutureBuilder( future: Future.delayed( const Duration(seconds: 0)), builder: (context, snapshot) { if (element["summary"] != null && (element["summary"] as String) .isNotEmpty) { return ListTile( title: Text(element["title"], style: const TextStyle( fontWeight: FontWeight.bold)), subtitle: Text( element["summary"], overflow: TextOverflow.ellipsis, maxLines: 2, softWrap: true, ), trailing: Text( "${DateTime.parse(element["publish_date"]).day.toString()}.${DateTime.parse(element["publish_date"]).month.toString()}.${DateTime.parse(element["publish_date"]).year.toString()}"), ); } else { return ListTile( title: Text(element["title"], style: const TextStyle( fontWeight: FontWeight.bold)), trailing: Text( "${DateTime.parse(element["publish_date"]).day.toString()}.${DateTime.parse(element["publish_date"]).month.toString()}.${DateTime.parse(element["publish_date"]).year.toString()}"), ); } }, )), shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(15), )), ); } else { return GestureDetector( onTap: () { Navigator.push( context, MaterialPageRoute( builder: (context) => Article.fromData( element["title"], element["content"], element["author"], element["publish_date"]) .widget), ); }, child: Card( child: Padding( padding: const EdgeInsets.fromLTRB( 10, 10, 10, 10), child: FutureBuilder( future: Future.delayed( const Duration(seconds: 0)), builder: (context, snapshot) { if (element["summary"] != null && (element["summary"] as String) .isNotEmpty) { return ListTile( title: Text(element["title"], style: const TextStyle( fontWeight: FontWeight.bold)), subtitle: Text( element["summary"], overflow: TextOverflow.ellipsis, maxLines: 2, softWrap: true, ), trailing: Text( "${DateTime.parse(element["publish_date"]).day.toString()}.${DateTime.parse(element["publish_date"]).month.toString()}.${DateTime.parse(element["publish_date"]).year.toString()}"), ); } else { return ListTile( title: Text(element["title"], style: const TextStyle( fontWeight: FontWeight.bold)), trailing: Text( "${DateTime.parse(element["publish_date"]).day.toString()}.${DateTime.parse(element["publish_date"]).month.toString()}.${DateTime.parse(element["publish_date"]).year.toString()}"), ); } }, )), shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(15), )), ); } } else { return const LinearProgressIndicator(); } }, ); articleTiles.add(card); } return ListView( children: articleTiles.reversed.toList(), ); } else { return (const Center( child: Text("Uups... Irgendwas ist schief gelaufen"))); } } else { return (const Center(child: CircularProgressIndicator())); } }, ), ), ); })); } } class Article { Widget widget; //const Article({Key? key}) : super(key: key); Article({required this.widget}); factory Article.fromData( String title, String content, String author, String publishDate) { return Article( widget: Scaffold( appBar: AppBar( title: SingleChildScrollView( scrollDirection: Axis.vertical, child: SingleChildScrollView( scrollDirection: Axis.horizontal, child: Text(title)), ), centerTitle: true, ), body: ListView( padding: const EdgeInsets.fromLTRB(15, 15, 15, 15), children: [ ListTile( leading: const Icon(MdiIcons.accountOutline), title: Text(author), ), ListTile( leading: const Icon(MdiIcons.calendarOutline), title: Text( "${DateTime.parse(publishDate).day.toString()}.${DateTime.parse(publishDate).month.toString()}.${DateTime.parse(publishDate).year.toString()}"), ), MarkdownBody( data: content, onTapLink: (text, url, title) { launch(url!); }, extensionSet: md.ExtensionSet.commonMark, imageDirectory: "https://cms.mein.cantorgymnasium.de", ) ], ))); } }