Skip to content

Commit

Permalink
Close #275 allow user to change story tile favorite icon (#276)
Browse files Browse the repository at this point in the history
  • Loading branch information
theachoem authored Feb 21, 2025
1 parent ef00bcf commit c010d2b
Show file tree
Hide file tree
Showing 19 changed files with 518 additions and 78 deletions.
33 changes: 17 additions & 16 deletions bin/localization/data.csv

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions lib/core/databases/adapters/objectbox/entities.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class StoryObjectBox extends BaseObjectBox {

bool? starred;
String? feeling;

// deprecated
// TODO: removed after May 2025
bool? showDayCount;

@Property(type: PropertyType.date)
Expand All @@ -52,6 +55,7 @@ class StoryObjectBox extends BaseObjectBox {

// for query
String? metadata;
String? preferences;

@override
String? lastSavedDeviceId;
Expand All @@ -76,6 +80,7 @@ class StoryObjectBox extends BaseObjectBox {
required this.tags,
required this.assets,
required this.metadata,
required this.preferences,
this.lastSavedDeviceId,
this.permanentlyDeletedAt,
});
Expand Down
7 changes: 5 additions & 2 deletions lib/core/databases/adapters/objectbox/stories_box.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:storypad/core/constants/app_constants.dart';
import 'package:storypad/core/databases/adapters/objectbox/base_box.dart';
Expand Down Expand Up @@ -180,12 +181,12 @@ class StoriesBox extends BaseBox<StoryObjectBox, StoryDbModel> {
Map<String, dynamic>? options = map['options'];

Iterable<PathType> types = PathType.values.where((e) => e.name == object.type);

return StoryDbModel(
type: types.isNotEmpty ? types.first : PathType.docs,
id: object.id,
starred: object.starred,
feeling: object.feeling,
showDayCount: object.showDayCount ?? false,
year: object.year,
month: object.month,
day: object.day,
Expand All @@ -196,6 +197,7 @@ class StoriesBox extends BaseBox<StoryObjectBox, StoryDbModel> {
createdAt: object.createdAt,
tags: object.tags,
assets: object.assets,
preferences: StoryDbConstructorService.decodePreferences(object),
rawChanges: object.changes,
movedToBinAt: object.movedToBinAt,
lastSavedDeviceId: object.lastSavedDeviceId,
Expand Down Expand Up @@ -257,13 +259,14 @@ class StoriesBox extends BaseBox<StoryObjectBox, StoryDbModel> {
assets: story.assets?.toSet().toList(),
starred: story.starred,
feeling: story.feeling,
showDayCount: story.showDayCount,
showDayCount: null,
createdAt: story.createdAt,
updatedAt: story.updatedAt,
movedToBinAt: story.movedToBinAt,
metadata: story.latestChange?.safeMetadata,
changes: StoryDbConstructorService.changesToRawChanges(story),
permanentlyDeletedAt: null,
preferences: story.preferences != null ? jsonEncode(story.preferences?.toJson()) : null,
);
}
}
2 changes: 1 addition & 1 deletion lib/core/databases/legacy/storypad_legacy_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class StorypadLegacyDatabase {
id: storypadStory.createOn.millisecondsSinceEpoch,
starred: storypadStory.isFavorite,
feeling: storypadStory.feeling,
showDayCount: false,
preferences: null,
year: storypadStory.forDate.year,
month: storypadStory.forDate.month,
day: storypadStory.forDate.day,
Expand Down
40 changes: 32 additions & 8 deletions lib/core/databases/models/story_db_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:copy_with_extension/copy_with_extension.dart';
import 'package:storypad/core/databases/adapters/objectbox/stories_box.dart';
import 'package:storypad/core/databases/models/base_db_model.dart';
import 'package:storypad/core/databases/models/story_content_db_model.dart';
import 'package:storypad/core/databases/models/story_preferences_db_model.dart';
import 'package:storypad/core/services/story_db_constructor_service.dart';
import 'package:storypad/core/services/story_helper.dart';
import 'package:storypad/core/types/path_type.dart';
Expand Down Expand Up @@ -42,9 +43,6 @@ class StoryDbModel extends BaseDbModel {
final bool? starred;
final String? feeling;

@JsonKey(name: 'show_day_count')
final bool? _showDayCount;

@JsonKey(fromJson: tagsFromJson)
final List<String>? tags;
final List<int>? assets;
Expand All @@ -63,9 +61,9 @@ class StoryDbModel extends BaseDbModel {

@override
final DateTime updatedAt;

final DateTime? movedToBinAt;
final String? lastSavedDeviceId;
final StoryPreferencesDbModel? preferences;

DateTime get displayPathDate {
return DateTime(
Expand All @@ -87,7 +85,6 @@ class StoryDbModel extends BaseDbModel {
required this.id,
required this.starred,
required this.feeling,
required bool? showDayCount,
required this.year,
required this.month,
required this.day,
Expand All @@ -96,14 +93,15 @@ class StoryDbModel extends BaseDbModel {
required this.second,
required this.updatedAt,
required this.createdAt,
required this.preferences,
required this.tags,
required this.assets,
required this.movedToBinAt,
required this.allChanges,
required this.lastSavedDeviceId,
this.rawChanges,
this.latestChange,
}) : _showDayCount = showDayCount {
}) {
// By default, `allChanges` is null when fetched from the database for speed.
// For backups, only `allChanges` is included, while `latestChange` and `rawChanges` are excluded.
// This means that when converting a cloud backup JSON to a model,
Expand All @@ -112,7 +110,8 @@ class StoryDbModel extends BaseDbModel {
}

Duration get dateDifferentCount => DateTime.now().difference(displayPathDate);
bool get showDayCount => _showDayCount ?? false;
bool get showDayCount => preferences?.showDayCount ?? false;
String? get starIcon => preferences?.starIcon;

bool get viewOnly => unarchivable || inBins;

Expand Down Expand Up @@ -179,6 +178,17 @@ class StoryDbModel extends BaseDbModel {
));
}

Future<StoryDbModel?> updateStarIcon({
required String starIcon,
}) async {
if (!editable) return null;

return db.set(copyWithPreferences(
starIcon: starIcon,
updatedAt: DateTime.now(),
));
}

Future<StoryDbModel?> archive() async {
if (!archivable) return null;

Expand All @@ -205,6 +215,20 @@ class StoryDbModel extends BaseDbModel {
));
}

StoryDbModel copyWithPreferences({
bool? showDayCount,
String? starIcon,
DateTime? updatedAt,
}) {
return copyWith(
updatedAt: updatedAt ?? this.updatedAt,
preferences: (preferences ?? StoryPreferencesDbModel.create()).copyWith(
starIcon: starIcon ?? preferences?.starIcon,
showDayCount: showDayCount ?? preferences?.showDayCount,
),
);
}

factory StoryDbModel.fromNow() {
final now = DateTime.now();
return StoryDbModel.fromDate(now);
Expand All @@ -227,7 +251,7 @@ class StoryDbModel extends BaseDbModel {
id: now.millisecondsSinceEpoch,
starred: false,
feeling: null,
showDayCount: false,
preferences: StoryPreferencesDbModel.create(),
latestChange: StoryContentDbModel.create(),
allChanges: null,
updatedAt: now,
Expand Down
31 changes: 17 additions & 14 deletions lib/core/databases/models/story_db_model.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions lib/core/databases/models/story_preferences_db_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:copy_with_extension/copy_with_extension.dart';
import 'package:storypad/core/databases/models/base_db_model.dart';

part 'story_preferences_db_model.g.dart';

@CopyWith()
@JsonSerializable()
class StoryPreferencesDbModel extends BaseDbModel {
final String? starIcon;
final bool? showDayCount;

StoryPreferencesDbModel({
required this.showDayCount,
required this.starIcon,
});

@override
int get id => 0;

@override
DateTime? get updatedAt => null;

factory StoryPreferencesDbModel.create() {
return StoryPreferencesDbModel(
showDayCount: false,
starIcon: null,
);
}

@override
Map<String, dynamic> toJson() => _$StoryPreferencesDbModelToJson(this);
factory StoryPreferencesDbModel.fromJson(Map<String, dynamic> json) => _$StoryPreferencesDbModelFromJson(json);
}
Loading

0 comments on commit c010d2b

Please sign in to comment.