Skip to content

Commit

Permalink
Converting time zone settings if a non supported one was picked #139
Browse files Browse the repository at this point in the history
  • Loading branch information
MrCsabaToth committed Mar 4, 2023
1 parent 60f2eb3 commit 21e8843
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/preferences/generic.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const preferencesVersionExclusiveSportOrDeviceLeaderboard = 7;
const preferencesVersionTimeDisplayMode = 8;
const preferencesVersionNoWhitespaceInNetworkAddresses = 9;
const preferencesVersionPerMetricColoringByZone = 10;
const preferencesVersionDefault = preferencesVersionPerMetricColoringByZone;
const preferencesVersionDefaultingOldTimeZone = 11;
const preferencesVersionDefault = preferencesVersionDefaultingOldTimeZone;
const preferencesVersionNext = preferencesVersionDefault + 1;

const intTagPostfix = "_int";
16 changes: 16 additions & 0 deletions lib/utils/init_preferences.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import '../preferences/workout_mode.dart';
import '../preferences/zone_index_display_coloring.dart';
import '../utils/logging.dart';
import '../utils/preferences.dart';
import '../utils/time_zone.dart';
import 'constants.dart';

Future<void> migrateStringIntegerPreference(
Expand Down Expand Up @@ -375,6 +376,21 @@ Future<BasePrefService> initPreferences() async {
}
}

if (prefVersion <= preferencesVersionDefaultingOldTimeZone) {
final enforcedTimeZone =
prefService.get<String>(enforcedTimeZoneTag) ?? enforcedTimeZoneDefault;

if (enforcedTimeZone != enforcedTimeZoneDefault) {
final closestTimeZone = getClosestTimeZone(enforcedTimeZone);
if (closestTimeZone != enforcedTimeZone) {
prefService.set<String>(enforcedTimeZoneTag, closestTimeZone);
}
}

// Activities have stored timeZone, but we would need to convert those
// only if TrackManager.getTrack would get the timeZone besides the sport
}

await prefService.set<int>(preferencesVersionTag, preferencesVersionNext);

for (var sport in SportSpec.sportPrefixes) {
Expand Down
31 changes: 30 additions & 1 deletion lib/utils/time_zone.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import 'package:collection/collection.dart';
import 'package:flutter_timezone/flutter_timezone.dart';
import 'package:get/get.dart';
import 'package:pref/pref.dart';
import 'package:timezone/timezone.dart' as tz;
import 'package:tuple/tuple.dart';
import '../preferences/enforced_time_zone.dart';

Future<String> getTimeZone() async {
Expand All @@ -12,7 +14,34 @@ Future<String> getTimeZone() async {
return timeZone;
}

return await FlutterTimezone.getLocalTimezone();
return getClosestTimeZone(await FlutterTimezone.getLocalTimezone());
}

int timeZoneOffset(String timeZoneName) {
DateTime? now;
if (timeZoneName == enforcedTimeZoneDefault ||
!tz.timeZoneDatabase.locations.containsKey(timeZoneName)) {
now = DateTime.now();
} else {
final location = tz.getLocation(getClosestTimeZone(timeZoneName));
now = tz.TZDateTime.now(location);
}

return now.timeZoneOffset.inMinutes;
}

String getClosestTimeZone(String timeZoneName) {
if (tz.timeZoneDatabase.locations.containsKey(timeZoneName)) {
return timeZoneName;
}

final timeOffset = timeZoneOffset(timeZoneName);
return tz.timeZoneDatabase.locations.entries
.map((loc) => Tuple2<String, int>(
loc.key, (loc.value.currentTimeZone.offset ~/ 60000 - timeOffset).abs()))
.sortedByCompare((loc) => loc.item2, (int o1, int o2) => o1.compareTo(o2))
.first
.item1;
}

Future<List<String>> getSortedTimezones() async {
Expand Down

0 comments on commit 21e8843

Please sign in to comment.