ba75ce8d6d
- removed obsolete deps - fixed many issues - improved features - added: * favourites * account console * push notifications * background sync * improved settings
260 lines
13 KiB
Dart
260 lines
13 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> 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(
|
|
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();
|
|
}
|
|
},
|
|
);
|
|
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)
|
|
],
|
|
)));
|
|
}
|
|
}
|