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 2dc41b7e24 - added license info
- fixed issues
2022-01-16 15:28:42 +01:00

269 lines
12 KiB
Dart

// 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/>.
import 'dart:convert';
import 'package:meincantor/main.dart';
import 'package:flutter/material.dart';
import 'package:cyclop/cyclop.dart';
import 'package:meincantor/networking.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 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("Kurse"),
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 Padding(
padding: const EdgeInsets.all(5),
child: ListTile(
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.circular(
15.0)),
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] ?? subject),
subtitle: Text(
teachers[teacher] ?? 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");
if (lessonsJson == null || lessonsJson.isEmpty) {
await fetchLessonList();
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 Padding(
padding: const EdgeInsets.all(5),
child: ListTile(
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius.circular(15.0)),
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] ?? subject),
subtitle:
Text(teachers[teacher] ?? teacher),
));
} else {
return (const LinearProgressIndicator());
}
}),
);
}
return Column(
children: children,
);
} else {
return (const Center(child: CircularProgressIndicator()));
}
}),
],
));
}
}