added german translation and store stuff for app

This commit is contained in:
Jan Pleterski
2026-11-13 10:56:11 +01:00
parent d4bc01f202
commit 30708c73b1
47 changed files with 530 additions and 160 deletions
+18 -25
View File
@@ -27,7 +27,9 @@ class AppSettings extends ChangeNotifier {
static const List<Map<String, String>> languages = [
{'name': 'English', 'flag': '🇬🇧'},
{'name': 'Slovenian', 'flag': '🇸🇮'},
{'name': 'German', 'flag': '🇩🇪'},
];
static const String defaultLanguage = 'Slovenian';
static const Map<String, Color> bulletColors = {
'Blue': Colors.blue,
@@ -47,16 +49,18 @@ class AppSettings extends ChangeNotifier {
'Grey': Colors.grey,
};
// 13 was dropped earlier; 21 and 31–34 were Christmas / Easter themes
static const Set<int> removedTargets = {13, 21, 31, 32, 33, 34};
static final List<String> targets = [
defaultTarget,
for (int i = 1; i <= 35; i++)
if (i != 13) 'assets/targets/Target$i.jpg', // 13 was removed
if (!removedTargets.contains(i)) 'assets/targets/Target$i.jpg',
];
late SharedPreferences _prefs;
String _language = 'English';
bool _followSystemLanguage = false;
String _language = defaultLanguage;
String _bulletColorName = 'Blue';
String _selectedTarget = defaultTarget;
int _reminderHours = 1;
@@ -67,11 +71,14 @@ class AppSettings extends ChangeNotifier {
List<Contact>? _clubContacts; // null until the club code is entered
String get language => _language;
bool get followSystemLanguage => _followSystemLanguage;
// Phone language (or Android 13+ per-app language) mapped to our two languages
static String get systemLanguage =>
WidgetsBinding.instance.platformDispatcher.locale.languageCode == 'sl' ? 'Slovenian' : 'English';
/// intl locale for dates (month and day names) in [language].
static String dateLocaleFor(String language) => switch (language) {
'Slovenian' => 'sl_SI',
'German' => 'de_DE',
_ => 'en_US',
};
String get dateLocale => dateLocaleFor(_language);
String get bulletColorName => _bulletColorName;
Color get bulletColor => bulletColors[_bulletColorName] ?? Colors.blue;
String get selectedTarget => _selectedTarget;
@@ -96,12 +103,12 @@ class AppSettings extends ChangeNotifier {
Future<void> load() async {
_prefs = await SharedPreferences.getInstance();
// New installs follow the phone; an explicit earlier choice stays as it is
// New installs start in Slovenian. Whoever had "automatic" keeps the language
// it last resolved to (saved under the same key).
final saved = _prefs.getString('language');
_followSystemLanguage = _prefs.getBool('languageFollowsSystem') ?? saved == null;
_language = _followSystemLanguage ? systemLanguage : saved!;
_language = languages.any((l) => l['name'] == saved) ? saved! : defaultLanguage;
await _prefs.setString('language', _language); // other screens read this key
WidgetsBinding.instance.platformDispatcher.onLocaleChanged = _onSystemLocaleChanged;
await _prefs.remove('languageFollowsSystem');
final color = _prefs.getString('bulletColor') ?? 'Blue';
_bulletColorName = bulletColors.containsKey(color) ? color : 'Blue';
@@ -141,22 +148,8 @@ class AppSettings extends ChangeNotifier {
Future<void> setLanguage(String value) async {
_language = value;
_followSystemLanguage = false;
notifyListeners();
await _prefs.setString('language', value);
await _prefs.setBool('languageFollowsSystem', false);
}
Future<void> useSystemLanguage() async {
_followSystemLanguage = true;
_language = systemLanguage;
notifyListeners();
await _prefs.setString('language', _language);
await _prefs.setBool('languageFollowsSystem', true);
}
void _onSystemLocaleChanged() {
if (_followSystemLanguage && systemLanguage != _language) useSystemLanguage();
}
Future<void> setBulletColor(String name) async {
+17 -32
View File
@@ -63,33 +63,9 @@ class NotificationService {
AndroidFlutterLocalNotificationsPlugin>();
await androidPlugin?.createNotificationChannel(_channel);
await requestPermissions();
await _requestExactAlarmsOnce();
} catch (_) {}
}
// Android 12+ needs "Alarms & reminders" access for on-time delivery.
// Ask once; without it we fall back to inexact alarms.
Future<void> _requestExactAlarmsOnce() async {
final android = _plugin.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>();
if (android == null) return;
if (await android.canScheduleExactNotifications() ?? true) return;
final prefs = await SharedPreferences.getInstance();
if (prefs.getBool('exactAlarmAsked') ?? false) return;
await prefs.setBool('exactAlarmAsked', true);
await android.requestExactAlarmsPermission();
}
Future<AndroidScheduleMode> _scheduleMode() async {
final android = _plugin.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>();
final canExact = await android?.canScheduleExactNotifications() ?? true;
return canExact
? AndroidScheduleMode.exactAllowWhileIdle
: AndroidScheduleMode.inexactAllowWhileIdle;
}
/// Fetches the calendar and reschedules all reminders. Call on app start
/// and whenever notification settings change.
Future<void> rescheduleFromCalendar() async {
@@ -140,9 +116,8 @@ class NotificationService {
final notifyDezuranje = prefs.getBool('notifyDezuranje') ?? true;
final notifySestanek = prefs.getBool('notifySestanek') ?? true;
final notifyTekma = prefs.getBool('notifyTekma') ?? true;
final isSlovenian = (prefs.getString('language') ?? 'English') == 'Slovenian';
final language = prefs.getString('language') ?? 'Slovenian';
final now = DateTime.now();
final mode = await _scheduleMode();
await _plugin.cancelAll();
@@ -166,23 +141,32 @@ class NotificationService {
await _scheduleNotification(
id: notifId++,
title: event.summary ?? 'Event',
body: isSlovenian
? (hours == 1 ? 'Začne se čez 1 uro' : 'Začne se čez $hours ur')
: (hours == 1 ? 'Starts in 1 hour' : 'Starts in $hours hours'),
body: _startsIn(hours, language),
scheduledDate: notifyAt,
mode: mode,
);
}
}
} catch (_) {}
}
static String _startsIn(int hours, String language) {
switch (language) {
case 'Slovenian':
final mod = hours % 100;
final unit = mod == 1 ? 'uro' : mod == 2 ? 'uri' : (mod == 3 || mod == 4) ? 'ure' : 'ur';
return 'Začne se čez $hours $unit';
case 'German':
return hours == 1 ? 'Beginnt in 1 Stunde' : 'Beginnt in $hours Stunden';
default:
return hours == 1 ? 'Starts in 1 hour' : 'Starts in $hours hours';
}
}
Future<void> _scheduleNotification({
required int id,
required String title,
required String body,
required DateTime scheduledDate,
required AndroidScheduleMode mode,
}) async {
final tzDate = tz.TZDateTime.from(scheduledDate, tz.local);
await _plugin.zonedSchedule(
@@ -191,7 +175,8 @@ class NotificationService {
body,
tzDate,
_notifDetails,
androidScheduleMode: mode,
// Inexact: avoids the SCHEDULE_EXACT_ALARM permission (Play policy).
androidScheduleMode: AndroidScheduleMode.inexactAllowWhileIdle,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
);