2022-01-16 15:28:42 +01:00
|
|
|
// 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 <https://www.gnu.org/licenses/>.
|
|
|
|
|
2021-12-13 13:39:06 +01:00
|
|
|
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';
|
|
|
|
|
|
|
|
Future<List> getNewsRead() async {
|
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
|
|
String? newsReadString = prefs.getString("newsRead");
|
|
|
|
List<dynamic> newsRead;
|
|
|
|
if (newsReadString == null ||
|
|
|
|
(jsonDecode(newsReadString) as List<dynamic>).isEmpty) {
|
|
|
|
newsRead = [];
|
|
|
|
} else {
|
|
|
|
newsRead = jsonDecode(newsReadString) as List<dynamic>;
|
|
|
|
}
|
|
|
|
return newsRead;
|
|
|
|
}
|
|
|
|
|
|
|
|
class News extends StatefulWidget {
|
|
|
|
const News({Key? key}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<StatefulWidget> createState() => _NewsState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _NewsState extends State<News> {
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
2021-12-19 22:20:56 +01:00
|
|
|
appBar: AppBar(
|
|
|
|
title: const Text("Aktuelles"),
|
|
|
|
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<http.Response>(
|
|
|
|
future: getNews(),
|
|
|
|
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<Widget> articleTiles = [];
|
|
|
|
for (var element in articles) {
|
|
|
|
int id = element["id"];
|
|
|
|
element = element["attributes"];
|
|
|
|
Color color = Colors.white70;
|
|
|
|
Widget card = FutureBuilder(
|
|
|
|
future: getNewsRead(),
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
if (snapshot.hasData) {
|
|
|
|
List<dynamic> readList =
|
|
|
|
snapshot.data! as List<dynamic>;
|
|
|
|
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(
|
|
|
|
"newsRead", 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();
|
|
|
|
}
|
2021-12-13 13:39:06 +01:00
|
|
|
},
|
|
|
|
);
|
2021-12-19 22:20:56 +01:00
|
|
|
articleTiles.add(card);
|
2021-12-13 13:39:06 +01:00
|
|
|
}
|
2021-12-19 22:20:56 +01:00
|
|
|
return ListView(
|
|
|
|
children: articleTiles.reversed.toList(),
|
|
|
|
);
|
2021-12-13 13:39:06 +01:00
|
|
|
} else {
|
2021-12-19 22:20:56 +01:00
|
|
|
return (const Center(
|
|
|
|
child:
|
|
|
|
Text("Uups... Irgendwas ist schief gelaufen")));
|
2021-12-13 13:39:06 +01:00
|
|
|
}
|
2021-12-19 22:20:56 +01:00
|
|
|
} else {
|
|
|
|
return (const Center(child: CircularProgressIndicator()));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}));
|
2021-12-13 13:39:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
],
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
}
|