Skip to content

Commit

Permalink
Change id generation
Browse files Browse the repository at this point in the history
  • Loading branch information
AhsanSarwar45 committed Aug 25, 2024
1 parent 62f328a commit 1567d70
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 86 deletions.
4 changes: 2 additions & 2 deletions lib/alarm/types/alarm_event.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:clock_app/common/types/json.dart';
import 'package:clock_app/common/types/list_item.dart';
import 'package:clock_app/common/types/notification_type.dart';
import 'package:flutter/foundation.dart';
import 'package:clock_app/common/utils/id.dart';

// enum AlarmEventType{
// schedule,
Expand All @@ -26,7 +26,7 @@ class AlarmEvent extends ListItem {
required this.scheduleId,
required this.startDate,
required this.isActive,
}) : id = UniqueKey().hashCode;
}) : id = getId();

AlarmEvent.fromJson(Json json) {
if (json == null) {
Expand Down
10 changes: 5 additions & 5 deletions lib/alarm/types/alarm_runner.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import 'package:clock_app/alarm/logic/schedule_alarm.dart';
import 'package:clock_app/common/types/json.dart';
import 'package:clock_app/common/types/notification_type.dart';
import 'package:flutter/material.dart';
import 'package:clock_app/common/utils/id.dart';

class AlarmRunner extends JsonSerializable {
late int _id;
DateTime? _currentScheduleDateTime;

int get id => _id;
get id => _id;
DateTime? get currentScheduleDateTime => _currentScheduleDateTime;

AlarmRunner() {
_id = UniqueKey().hashCode;
_id = getId();
}

Future<void> schedule(DateTime dateTime, String description) async {
Expand All @@ -27,10 +27,10 @@ class AlarmRunner extends JsonSerializable {

AlarmRunner.fromJson(Json? json) {
if (json == null) {
_id = UniqueKey().hashCode;
_id = getId();
return;
}
_id = json['id'] ?? UniqueKey().hashCode;
_id = json['id'] ?? getId();
int millisecondsSinceEpoch = json['currentScheduleDateTime'] ?? 0;
_currentScheduleDateTime = millisecondsSinceEpoch == 0
? null
Expand Down
9 changes: 5 additions & 4 deletions lib/alarm/types/alarm_task.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:clock_app/alarm/data/alarm_task_schemas.dart';
import 'package:clock_app/common/types/json.dart';
import 'package:clock_app/common/types/list_item.dart';
import 'package:clock_app/common/utils/id.dart';
import 'package:clock_app/settings/types/setting_group.dart';
import 'package:flutter/material.dart';

Expand Down Expand Up @@ -55,21 +56,21 @@ class AlarmTask extends CustomizableListItem {

AlarmTask(this.type)
: _schema = alarmTaskSchemasMap[type]!.copy(),
_id = UniqueKey().hashCode;
_id = getId();

AlarmTask.from(AlarmTask task)
: type = task.type,
_id = UniqueKey().hashCode,
_id = getId(),
_schema = task._schema.copy();

AlarmTask.fromJson(Json json) {
if (json == null) {
_id = UniqueKey().hashCode;
_id = getId();
type = AlarmTaskType.math;
_schema = alarmTaskSchemasMap[type]!.copy();
return;
}
_id = json['id'] ?? UniqueKey().hashCode;
_id = json['id'] ?? getId();
type = AlarmTaskType.values.byName(json['type']);
_schema = alarmTaskSchemasMap[type]!.copy();
_schema.loadFromJson(json['schema']);
Expand Down
8 changes: 4 additions & 4 deletions lib/clock/types/city.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:clock_app/common/types/json.dart';
import 'package:clock_app/common/types/list_item.dart';
import 'package:flutter/material.dart';
import 'package:clock_app/common/utils/id.dart';

class City extends ListItem {
late String _name = "Unknown";
Expand All @@ -17,7 +17,7 @@ class City extends ListItem {
@override
bool get isDeletable => true;

City(this._name, this._country, this._timezone) : _id = UniqueKey().hashCode;
City(this._name, this._country, this._timezone) : _id = getId();

@override
copy() {
Expand All @@ -26,13 +26,13 @@ class City extends ListItem {

City.fromJson(Json json) {
if (json == null) {
_id = UniqueKey().hashCode;
_id = getId();
return;
}
_name = json['name'] ?? 'Unknown';
_country = json['country'] ?? 'Unknown';
_timezone = json['timezone'] ?? 'America/Detroit';
_id = json['id'] ?? UniqueKey().hashCode;
_id = json['id'] ?? getId();
}

@override
Expand Down
9 changes: 4 additions & 5 deletions lib/common/types/file_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import 'dart:convert';

import 'package:clock_app/common/types/json.dart';
import 'package:clock_app/common/types/list_item.dart';
import 'package:clock_app/common/types/timer_state.dart';
import 'package:flutter/material.dart';
import 'package:clock_app/common/utils/id.dart';

enum FileItemType {
audio,
Expand Down Expand Up @@ -33,14 +32,14 @@ class FileItem extends ListItem {
bool get isDeletable => _isDeletable;

FileItem(this.name, this._uri, this._type, {isDeletable = true})
: _id = UniqueKey().hashCode,
: _id = getId(),
_isDeletable = isDeletable;

@override
FileItem.fromJson(Json json)
: _id = json != null
? json['id'] ?? UniqueKey().hashCode
: UniqueKey().hashCode,
? json['id'] ?? getId()
: getId(),
_type = json != null
? json['type'] != null
? FileItemType.values
Expand Down
3 changes: 2 additions & 1 deletion lib/common/types/list_filter.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:clock_app/common/types/list_item.dart';
import 'package:clock_app/common/utils/debug.dart';
import 'package:clock_app/common/utils/id.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

Expand Down Expand Up @@ -37,7 +38,7 @@ class ListFilter<Item extends ListItem> extends ListFilterItem<Item> {

ListFilter(this.getLocalizedName, bool Function(Item) filterFunction,
{int? id})
: _id = id ?? UniqueKey().hashCode,
: _id = id ?? getId(),
_filterFunction = filterFunction;

int get id => _id;
Expand Down
7 changes: 4 additions & 3 deletions lib/common/types/tag.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:clock_app/common/types/json.dart';
import 'package:clock_app/common/types/list_item.dart';
import 'package:clock_app/common/utils/id.dart';
import 'package:flutter/material.dart';

class Tag extends ListItem {
Expand All @@ -8,16 +9,16 @@ class Tag extends ListItem {
String description;
Color color;
Tag(this.name, {this.description = "", this.color = Colors.blue})
: _id = UniqueKey().hashCode;
: _id = getId();

Tag.fromJson(Json json)
: _id = json?['id'] ?? UniqueKey().hashCode,
: _id = json?['id'] ?? getId(),
name = json?['name'] ?? "Unknown",
description = json?['description'] ?? "",
color = Color(json?['color'] ?? 0);

Tag.from(Tag tag)
: _id = UniqueKey().hashCode,
: _id = getId(),
name = tag.name,
description = tag.description,
color = tag.color;
Expand Down
5 changes: 5 additions & 0 deletions lib/common/utils/id.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'package:flutter/material.dart';

int getId() {
return UniqueKey().hashCode + DateTime.now().microsecondsSinceEpoch;
}
60 changes: 30 additions & 30 deletions lib/settings/data/backup_settings_schema.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,36 @@ SettingGroup backupSettingsSchema = SettingGroup(
"Settings",
(context) => AppLocalizations.of(context)!.settingsTitle,
[
SettingPageLink("Export", (context) => AppLocalizations.of(context)!.exportSettingsSetting, BackupScreen()),
SettingPageLink("Import", (context) => AppLocalizations.of(context)!.importSettingsSetting, BackupScreen()),
// SettingAction(
// "Export",
// (context) => AppLocalizations.of(context)!.exportSettingsSetting,
// (context) async {
// saveBackupFile(json.encode(appSettings.valueToJson()), "settings");
// },
// searchTags: ["settings", "export", "backup", "save"],
// getDescription: (context) =>
// AppLocalizations.of(context)!.exportSettingsSettingDescription,
// ),
// SettingAction(
// "Import",
// (context) => AppLocalizations.of(context)!.importSettingsSetting,
// (context) async {
// loadBackupFile(
// (data) async {
// appSettings.loadValueFromJson(json.decode(data));
// appSettings.callAllListeners();
// App.refreshTheme(context);
// await appSettings.save();
// if (context.mounted) setDigitalClockWidgetData(context);
// },
// );
// },
// searchTags: ["settings", "import", "backup", "load"],
// getDescription: (context) =>
// AppLocalizations.of(context)!.importSettingsSettingDescription,
// ),
// SettingPageLink("Export", (context) => AppLocalizations.of(context)!.exportSettingsSetting, BackupScreen()),
// SettingPageLink("Import", (context) => AppLocalizations.of(context)!.importSettingsSetting, BackupScreen()),
SettingAction(
"Export",
(context) => AppLocalizations.of(context)!.exportSettingsSetting,
(context) async {
saveBackupFile(json.encode(appSettings.valueToJson()), "settings");
},
searchTags: ["settings", "export", "backup", "save"],
getDescription: (context) =>
AppLocalizations.of(context)!.exportSettingsSettingDescription,
),
SettingAction(
"Import",
(context) => AppLocalizations.of(context)!.importSettingsSetting,
(context) async {
loadBackupFile(
(data) async {
appSettings.loadValueFromJson(json.decode(data));
appSettings.callAllListeners();
App.refreshTheme(context);
await appSettings.save();
if (context.mounted) setDigitalClockWidgetData(context);
},
);
},
searchTags: ["settings", "import", "backup", "load"],
getDescription: (context) =>
AppLocalizations.of(context)!.importSettingsSettingDescription,
),
],
),
],
Expand Down
23 changes: 10 additions & 13 deletions lib/settings/screens/backup_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@ import 'package:clock_app/widgets/logic/update_widgets.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

class SettingCheckBox extends StatelessWidget {
const SettingCheckBox(
class BackupOptionCheckBox extends StatelessWidget {
const BackupOptionCheckBox(
{super.key,
required this.settingItem,
required this.isChecked,
required this.onChanged});
required this.option, required this.onChanged});

final bool isChecked;
final BackupOption option;
final void Function(bool?) onChanged;

@override
Expand All @@ -33,11 +31,11 @@ class SettingCheckBox extends StatelessWidget {
Checkbox(
// checkColor: Colors.white,
// fillColor: MaterialStateProperty.resolveWith(getColor),
value: isChecked,
value: option.selected,
onChanged: onChanged,
),
Text(
settingItem.displayName(context),
option.getName(context),
style: Theme.of(context).textTheme.headlineMedium,
),
],
Expand Down Expand Up @@ -195,13 +193,12 @@ class _BackupExportScreenState extends State<BackupExportScreen> {
},
),
const SizedBox(height: 16),
...widget.settingGroup.settingItems.map(
(settingItem) => SettingCheckBox(
settingItem: settingItem,
isChecked: _settingsToRestore[settingItem.id] ?? false,
...backupOptions.map(
(option) => BackupOptionCheckBox(
option: option,
onChanged: (bool? value) {
setState(() {
_settingsToRestore[settingItem.id] = value!;
option.selected = value ?? false;
});
},
),
Expand Down
10 changes: 5 additions & 5 deletions lib/stopwatch/types/stopwatch.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import 'package:clock_app/common/types/json.dart';
import 'package:clock_app/common/types/timer_state.dart';
import 'package:clock_app/common/utils/duration.dart';
import 'package:clock_app/common/utils/id.dart';
import 'package:clock_app/common/utils/json_serialize.dart';
import 'package:clock_app/common/utils/logger.dart';
import 'package:clock_app/stopwatch/types/lap.dart';
import 'package:clock_app/timer/types/time_duration.dart';
import 'package:flutter/material.dart';

// All time units are in milliseconds
class ClockStopwatch extends JsonSerializable {
Expand Down Expand Up @@ -54,7 +54,7 @@ class ClockStopwatch extends JsonSerializable {
}

ClockStopwatch()
: _id = UniqueKey().hashCode,
: _id = getId(),
_elapsedMillisecondsOnPause = 0,
_startTime = DateTime(0),
_state = TimerState.stopped;
Expand All @@ -63,7 +63,7 @@ class ClockStopwatch extends JsonSerializable {
: _elapsedMillisecondsOnPause = 0,
_startTime = DateTime(0),
_state = TimerState.stopped,
_id = UniqueKey().hashCode;
_id = getId();

copyFrom(ClockStopwatch stopwatch) {
_elapsedMillisecondsOnPause = stopwatch._elapsedMillisecondsOnPause;
Expand Down Expand Up @@ -153,7 +153,7 @@ class ClockStopwatch extends JsonSerializable {

ClockStopwatch.fromJson(Json json) {
if (json == null) {
_id = UniqueKey().hashCode;
_id = getId();
return;
}
_elapsedMillisecondsOnPause = json['elapsedMillisecondsOnPause'] ?? 0;
Expand All @@ -163,7 +163,7 @@ class ClockStopwatch extends JsonSerializable {
_state = TimerState.values.firstWhere(
(e) => e.toString() == (json['state'] ?? ''),
orElse: () => TimerState.stopped);
_id = json['id'] ?? UniqueKey().hashCode;
_id = json['id'] ?? getId();
// _finishedLaps = [];
_laps = listFromString(json['laps'] ?? '[]');
updateFastestAndSlowestLap();
Expand Down
10 changes: 5 additions & 5 deletions lib/theme/types/theme_item.dart
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import 'package:clock_app/common/types/json.dart';
import 'package:clock_app/common/types/list_item.dart';
import 'package:clock_app/common/utils/id.dart';
import 'package:clock_app/settings/types/setting_group.dart';
import 'package:flutter/material.dart';

abstract class ThemeItem extends CustomizableListItem {
late int _id;
SettingGroup _settings;
bool _isDefault = false;

ThemeItem(SettingGroup defaultSettings, bool isDefault, [int? id])
: _id = id ?? UniqueKey().hashCode,
: _id = id ?? getId(),
_settings = defaultSettings,
_isDefault = isDefault;

ThemeItem.from(ThemeItem themeItem)
: _id = UniqueKey().hashCode,
: _id = getId(),
_isDefault = false,
_settings = themeItem.settings.copy();

Expand Down Expand Up @@ -45,10 +45,10 @@ abstract class ThemeItem extends CustomizableListItem {

ThemeItem.fromJson(Json json, SettingGroup settings) : _settings = settings {
if (json == null) {
_id = UniqueKey().hashCode;
_id = getId();
return;
}
_id = json['id'] ?? UniqueKey().hashCode;
_id = json['id'] ?? getId();
_isDefault = json['isDefault'] ?? false;
settings.loadValueFromJson(json['settings']);
}
Expand Down
Loading

0 comments on commit 1567d70

Please sign in to comment.