This repository has been archived on 2023-06-24. You can view files and clone it, but cannot push or open issues or pull requests.
meincantor-app/lib/Settings/Pages/plan_settings.dart
Denys Konovalov e86c1bad1d = 0.8.0-dev =
- code cleanup
- caching
- black-/whitelist
- sz/news fixes
- added settings options
...
2021-12-13 13:39:06 +01:00

279 lines
11 KiB
Dart

import 'dart:convert';
import 'package:meincantor/main.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:cyclop/cyclop.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:meincantor/presets/colors.dart';
import 'package:meincantor/presets/subjects.dart';
import 'package:meincantor/presets/teachers.dart';
class PlanSettings extends StatelessWidget {
const PlanSettings({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Plan"),
centerTitle: true,
),
body: ListView(
padding: const EdgeInsets.fromLTRB(5, 5, 5, 5),
children: [
ListTile(
leading: const Icon(Icons.list_alt_outlined, color: Colors.red),
trailing: const Icon(Icons.arrow_forward_ios, size: 16),
title: const Text("Kurse"),
subtitle: const Text("Konfiguration der Kurse (Whitelist)"),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const WhitelistSettings()),
);
},
),
ListTile(
leading:
const Icon(Icons.color_lens_outlined, color: Colors.teal),
trailing: const Icon(Icons.arrow_forward_ios, size: 16),
title: const Text("Farben"),
subtitle:
const Text("Konfiguration der Farben für die Plankacheln"),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const PlanColorSettings()),
);
},
),
],
));
}
}
class WhitelistSettings extends StatefulWidget {
const WhitelistSettings({Key? key}) : super(key: key);
@override
State<StatefulWidget> createState() => _WhitelistSettingsState();
}
class _WhitelistSettingsState extends State<WhitelistSettings> {
Set<Color> swatches = {
...Colors.primaries,
...Colors.accents,
Palette.accent,
Palette.primary
};
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Farben"),
centerTitle: true,
),
body: ListView(
padding: const EdgeInsets.fromLTRB(5, 5, 5, 5),
children: [
FutureBuilder(
future: buildLessonsList(),
builder: (context, snapshot) {
if (snapshot.hasData) {
List<Widget> children = [];
for (var element in (snapshot.data as List<dynamic>)) {
String subject = element['subject'];
String teacher = element['teacher'];
int id = element['id'];
children.add(
FutureBuilder(
future: buildPlanColors(subject),
builder: (context, snapshot) {
if (snapshot.hasData) {
Color color = snapshot.data as Color;
return FutureBuilder(
future: buildBlacklist(),
builder: (context, snapshot) {
if (snapshot.hasData) {
final _blacklist =
snapshot.data! as List<dynamic>;
final _blacklisted =
_blacklist.contains(id);
return ListTile(
leading: Checkbox(
value:
_blacklisted ? false : true,
onChanged: (state) async {
SharedPreferences prefs =
await SharedPreferences
.getInstance();
setState(() {
_blacklisted
? _blacklist.remove(id)
: _blacklist.add(id);
});
prefs.setString("blacklist",
jsonEncode(_blacklist));
},
activeColor: color),
title: Text(subjects[subject] ?? ""),
subtitle:
Text(teachers[teacher] ?? ""),
onTap: () async {
SharedPreferences prefs =
await SharedPreferences
.getInstance();
setState(() {
_blacklisted
? _blacklist.remove(id)
: _blacklist.add(id);
});
prefs.setString("blacklist",
jsonEncode(_blacklist));
},
);
} else {
return const LinearProgressIndicator();
}
});
} else {
return (const LinearProgressIndicator());
}
}),
);
}
return Column(
children: children,
);
} else {
return (const Center(child: CircularProgressIndicator()));
}
}),
],
));
}
}
class PlanColorSettings extends StatefulWidget {
const PlanColorSettings({Key? key}) : super(key: key);
@override
State<StatefulWidget> createState() => _PlanColorSettingsState();
}
Future<Color> buildPlanColors(String lesson) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String key = "color$lesson";
if (prefs.containsKey(key) == false) {
int colorValue;
if (colors.containsKey(lesson)) {
colorValue = colors[lesson].value;
} else {
colorValue = Colors.grey.value;
}
prefs.setInt(key, colorValue);
}
//await fetchLessonList();
Color color = Color(prefs.getInt(key)!);
return color;
}
Future<List<dynamic>> buildLessonsList() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String lessonsJson = prefs.getString("lessons")!;
List<dynamic> lessons = jsonDecode(lessonsJson);
return lessons;
}
Future<List> buildBlacklist() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
if (!prefs.containsKey("blacklist") ||
jsonDecode(prefs.getString("blacklist")!).isEmpty) {
return <int>[];
}
String blacklistJson = prefs.getString("blacklist")!;
List blacklist = jsonDecode(blacklistJson);
return blacklist;
}
class _PlanColorSettingsState extends State<PlanColorSettings> {
Set<Color> swatches = {
...Colors.primaries,
...Colors.accents,
Palette.accent,
Palette.primary
};
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Farben"),
centerTitle: true,
),
body: ListView(
padding: const EdgeInsets.fromLTRB(5, 5, 5, 5),
children: [
FutureBuilder(
future: buildLessonsList(),
builder: (context, snapshot) {
if (snapshot.hasData) {
List<Widget> children = [];
for (var element in (snapshot.data as List<dynamic>)) {
String subject = element['subject'];
String teacher = element['teacher'];
children.add(
FutureBuilder(
future: buildPlanColors(subject),
builder: (context, snapshot) {
if (snapshot.hasData) {
Color color = snapshot.data as Color;
return ListTile(
leading: ColorButton(
key: const Key('c1'),
color: color,
config: const ColorPickerConfig(
enableEyePicker: false),
onSwatchesChanged: (Set<Color> value) {
swatches = value;
},
size: 32,
swatches: swatches,
onColorChanged: (Color value) async {
setState(() {
color = value;
});
SharedPreferences prefs =
await SharedPreferences
.getInstance();
prefs.setInt(
"color$subject", value.value);
}),
title: Text(subjects[subject] ?? ""),
subtitle: Text(teachers[teacher] ?? ""),
);
} else {
return (const LinearProgressIndicator());
}
}),
);
}
return Column(
children: children,
);
} else {
return (const Center(child: CircularProgressIndicator()));
}
}),
],
));
}
}