Skip to content

Commit

Permalink
use ColorConverter on colors in the subject model to maintain the Col…
Browse files Browse the repository at this point in the history
…or type instead of using ints
  • Loading branch information
user5522 committed Dec 23, 2023
1 parent 429c834 commit 816b91a
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 43 deletions.
9 changes: 4 additions & 5 deletions lib/components/subject_management/subject_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ class SubjectScreen extends HookConsumerWidget {
Days.values[isSubjectNull ? columnIndex! : subject!.day.index]);
final rotationWeek =
useState(isSubjectNull ? RotationWeeks.none : subject!.rotationWeek);
final color =
useState(isSubjectNull ? Colors.black : Color(subject!.color));
final color = useState(isSubjectNull ? Colors.black : subject!.color);

final label = useState(subject?.label ?? "");
final location = useState(subject?.location ?? "");
Expand All @@ -66,7 +65,7 @@ class SubjectScreen extends HookConsumerWidget {
id: id,
label: label.value,
location: location.value,
color: color.value.value,
color: color.value,
startTime: startTime.value,
endTime: endTime.value,
day: day.value,
Expand Down Expand Up @@ -199,14 +198,14 @@ class SubjectScreen extends HookConsumerWidget {
onChanged: (value) {
label.value = value;
if (autoCompleteColor) {
color.value = Color(subjects
color.value = subjects
.firstWhere(
(subj) =>
label.value.toLowerCase().trim() ==
subj.label.toLowerCase().trim(),
orElse: () => basicSubject,
)
.color);
.color;
}
},
),
Expand Down
9 changes: 4 additions & 5 deletions lib/components/widgets/day_view_subject_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@ class DayViewSubjectBuilder extends ConsumerWidget {
final rotationWeeks = ref.watch(settingsProvider).rotationWeeks;
final hideTransparentSubject =
ref.watch(settingsProvider).hideTransparentSubject;
Color labelColor = Color(subject.color).computeLuminance() > .7
? Colors.black
: Colors.white;
Color subLabelsColor = Color(subject.color).computeLuminance() > .7
Color labelColor =
subject.color.computeLuminance() > .7 ? Colors.black : Colors.white;
Color subLabelsColor = subject.color.computeLuminance() > .7
? Colors.black.withOpacity(.6)
: Colors.white.withOpacity(.75);

String label = subject.label;
String? location = subject.location;
Color color = Color(subject.color);
Color color = subject.color;
String? note = subject.note;

final hideTransparentSubjects =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,18 @@ class OverlappingSubjBuilder extends ConsumerWidget {
),
child: Row(
children: List.generate(subjects.length, (i) {
Color labelColor = Color(subjects[i].color).computeLuminance() > .7
Color labelColor = subjects[i].color.computeLuminance() > .7
? Colors.black
: Colors.white;
Color subLabelsColor =
Color(subjects[i].color).computeLuminance() > .7
? Colors.black.withOpacity(.6)
: Colors.white.withOpacity(.75);
Color subLabelsColor = subjects[i].color.computeLuminance() > .7
? Colors.black.withOpacity(.6)
: Colors.white.withOpacity(.75);

int endTimeHour = subjects[i].endTime.hour;
int startTimeHour = subjects[i].startTime.hour;
String label = subjects[i].label;
String? location = subjects[i].location;
Color color = Color(subjects[i].color);
Color color = subjects[i].color;

int subjHeight = endTimeHour - startTimeHour;

Expand Down
2 changes: 1 addition & 1 deletion lib/components/widgets/grid_view_subject_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class SubjectBuilder extends ConsumerWidget {

String label = subject.label;
String? location = subject.location;
Color color = Color(subject.color);
Color color = subject.color;

final hideTransparentSubjects =
hideTransparentSubject && color.opacity == Colors.transparent.opacity;
Expand Down
8 changes: 4 additions & 4 deletions lib/constants/basic_subject.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import 'package:timetable/db/database.dart';

/// A basic subject with no additional information.
/// Used for the color autocomplete feature.
final basicSubject = SubjectData(
const basicSubject = SubjectData(
id: 0,
label: " ",
location: "",
color: Colors.black.value,
startTime: const TimeOfDay(hour: 8, minute: 0),
endTime: const TimeOfDay(hour: 18, minute: 0),
color: Colors.black,
startTime: TimeOfDay(hour: 8, minute: 0),
endTime: TimeOfDay(hour: 18, minute: 0),
day: Days.monday,
rotationWeek: RotationWeeks.all,
note: "",
Expand Down
26 changes: 25 additions & 1 deletion lib/db/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:drift/drift.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:timetable/constants/rotation_weeks.dart';
import 'package:timetable/constants/days.dart';
import 'package:timetable/db/converters/color_converter.dart';
import 'package:timetable/db/models/subject.dart';
import 'package:timetable/db/connection/native.dart';
import 'package:timetable/db/converters/time_of_day_converter.dart';
Expand All @@ -14,7 +15,30 @@ class AppDatabase extends _$AppDatabase {
AppDatabase() : super(openConnection());

@override
int get schemaVersion => 1;
int get schemaVersion => 2;

@override
MigrationStrategy get migration {
return MigrationStrategy(
onCreate: (Migrator m) async {
await m.createAll();
},
onUpgrade: (Migrator m, int from, int to) async {
if (from < 2) {
// colors changed in v2 so they use the ColorConverter
// to maintain the Color type instead of using int in v1.
await m.alterTable(
TableMigration(
subject,
columnTransformer: {
subject.color: subject.color,
},
),
);
}
},
);
}

static final StateProvider<AppDatabase> provider = StateProvider((ref) {
final database = AppDatabase();
Expand Down
41 changes: 21 additions & 20 deletions lib/db/database.g.dart

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

5 changes: 4 additions & 1 deletion lib/db/models/subject.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import 'package:drift/drift.dart';
import 'package:timetable/constants/days.dart';
import 'package:timetable/constants/rotation_weeks.dart';
import 'package:timetable/db/converters/color_converter.dart';
import 'package:timetable/db/converters/time_of_day_converter.dart';

class Subject extends Table {
IntColumn get id => integer().autoIncrement()();
TextColumn get label => text()();
TextColumn get location => text().nullable()();
TextColumn get note => text().nullable()();
IntColumn get color => integer()();
// changed in v2 so it uses the ColorConverter
// to maintain the Color type instead of using int.
IntColumn get color => integer().map(const ColorConverter())();
IntColumn get rotationWeek => intEnum<RotationWeeks>()();
IntColumn get day => intEnum<Days>()();
TextColumn get startTime => text().map(const TimeOfDayConverter())();
Expand Down

0 comments on commit 816b91a

Please sign in to comment.