e86c1bad1d
- code cleanup - caching - black-/whitelist - sz/news fixes - added settings options ...
224 lines
10 KiB
Dart
224 lines
10 KiB
Dart
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> getSZread() async {
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
String? szReadString = prefs.getString("SZread");
|
|
List<dynamic> szRead;
|
|
if (szReadString == null ||
|
|
(jsonDecode(szReadString) as List<dynamic>).isEmpty) {
|
|
szRead = [];
|
|
} else {
|
|
szRead = jsonDecode(szReadString) as List<dynamic>;
|
|
}
|
|
return szRead;
|
|
}
|
|
|
|
class SZ extends StatefulWidget {
|
|
const SZ({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
State<StatefulWidget> createState() => _SZState();
|
|
}
|
|
|
|
class _SZState extends State<SZ> {
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text("Schülerzeitung"),
|
|
centerTitle: true,
|
|
),
|
|
body: FutureBuilder<http.Response>(
|
|
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);
|
|
List<Widget> articleTiles = [];
|
|
for (var element in articles) {
|
|
Color color = Colors.white70;
|
|
Widget card = FutureBuilder(
|
|
future: getSZread(),
|
|
builder: (context, snapshot) {
|
|
if (snapshot.hasData) {
|
|
List<dynamic> readList = snapshot.data! as List<dynamic>;
|
|
if (!readList.contains(element["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["published_at"])
|
|
.widget),
|
|
);
|
|
readList.add(element["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["published_at"]).day.toString()}.${DateTime.parse(element["published_at"]).month.toString()}.${DateTime.parse(element["published_at"]).year.toString()}"),
|
|
);
|
|
} else {
|
|
return ListTile(
|
|
title: Text(element["title"],
|
|
style: const TextStyle(
|
|
fontWeight: FontWeight.bold)),
|
|
trailing: Text(
|
|
"${DateTime.parse(element["published_at"]).day.toString()}.${DateTime.parse(element["published_at"]).month.toString()}.${DateTime.parse(element["published_at"]).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["published_at"])
|
|
.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["published_at"]).day.toString()}.${DateTime.parse(element["published_at"]).month.toString()}.${DateTime.parse(element["published_at"]).year.toString()}"),
|
|
);
|
|
} else {
|
|
return ListTile(
|
|
title: Text(element["title"],
|
|
style: const TextStyle(
|
|
fontWeight: FontWeight.bold)),
|
|
trailing: Text(
|
|
"${DateTime.parse(element["published_at"]).day.toString()}.${DateTime.parse(element["published_at"]).month.toString()}.${DateTime.parse(element["published_at"]).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)
|
|
],
|
|
)));
|
|
}
|
|
}
|