Skip to content

Commit

Permalink
Switched to sqlite and added flutter login screen
Browse files Browse the repository at this point in the history
  • Loading branch information
prijindal committed May 3, 2024
1 parent 6c6bddb commit b4abceb
Show file tree
Hide file tree
Showing 34 changed files with 1,387 additions and 771 deletions.
10 changes: 5 additions & 5 deletions lib/components/calendars.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class CalendarsSubPage extends StatefulWidget {
required this.habbit,
});

final List<HabbitEntry>? entries;
final Habbit? habbit;
final List<HabbitEntryData>? entries;
final HabbitData? habbit;

@override
State<CalendarsSubPage> createState() => _CalendarsSubPageState();
Expand All @@ -26,7 +26,7 @@ class _CalendarsSubPageState extends State<CalendarsSubPage> {
CalendarFormat _calendarformat = CalendarFormat.month;

List<DateTime> _firstAndLast() {
final entries = List<HabbitEntry>.from(widget.entries ?? [])
final entries = List<HabbitEntryData>.from(widget.entries ?? [])
..sort((a, b) {
return a.creationTime.difference(b.creationTime).inSeconds;
});
Expand All @@ -46,7 +46,7 @@ class _CalendarsSubPageState extends State<CalendarsSubPage> {
return _firstAndLast()[1];
}

List<HabbitEntry> _getEventsForDay(DateTime day) {
List<HabbitEntryData> _getEventsForDay(DateTime day) {
return widget.entries!
.where((element) => isSameDay(day, element.creationTime.toLocal()))
.toList();
Expand Down Expand Up @@ -104,7 +104,7 @@ class _CalendarsSubPageState extends State<CalendarsSubPage> {
);
}

Widget _buildEvents(List<HabbitEntry> entries) {
Widget _buildEvents(List<HabbitEntryData> entries) {
return ListView.builder(
shrinkWrap: true,
padding: const EdgeInsets.fromLTRB(0, 0, 0, 80),
Expand Down
4 changes: 2 additions & 2 deletions lib/components/counter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class CounterSubPage extends StatefulWidget {
required this.entries,
required this.habbit,
});
final List<HabbitEntry>? entries;
final Habbit? habbit;
final List<HabbitEntryData>? entries;
final HabbitData? habbit;

@override
State<CounterSubPage> createState() => _CounterSubPageState();
Expand Down
17 changes: 12 additions & 5 deletions lib/components/deleteentrydialog.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import 'package:drift/drift.dart';
import 'package:flutter/material.dart';

import '../helpers/stats.dart';
import '../models/core.dart';
import '../models/database.dart';
import '../models/drift.dart';

class DeleteEntryDialog extends StatelessWidget {
const DeleteEntryDialog({
super.key,
required this.entry,
});

final HabbitEntry entry;
final HabbitEntryData entry;

@override
Widget build(BuildContext context) {
Expand All @@ -26,9 +27,15 @@ class DeleteEntryDialog extends StatelessWidget {
),
TextButton(
onPressed: () async {
await MyDatabase.instance.writeAsync(() {
entry.deletionTime = DateTime.now();
});
(MyDatabase.instance.update(MyDatabase.instance.habbitEntry)
..where((tbl) => tbl.id.equals(entry.id)))
.write(
HabbitEntryCompanion(
deletionTime: Value(
DateTime.now(),
),
),
);
if (context.mounted) {
Navigator.of(context).pop<bool>(true);
}
Expand Down
32 changes: 21 additions & 11 deletions lib/components/deletehabbitdialog.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import 'package:drift/drift.dart';
import 'package:flutter/material.dart';

import '../models/core.dart';
import '../models/database.dart';
import '../models/drift.dart';

class DeleteHabbitDialog extends StatelessWidget {
const DeleteHabbitDialog({
super.key,
required this.habbit,
});

final Habbit habbit;
final HabbitData habbit;

@override
Widget build(BuildContext context) {
Expand All @@ -25,15 +26,24 @@ class DeleteHabbitDialog extends StatelessWidget {
),
TextButton(
onPressed: () async {
final entries = MyDatabase.instance
.query<HabbitEntry>(r'habbit.id == $0', [habbit.id]);

await MyDatabase.instance.writeAsync(() {
for (var element in entries) {
element.deletionTime = DateTime.now();
}
habbit.deletionTime = DateTime.now();
});
(MyDatabase.instance.update(MyDatabase.instance.habbitEntry)
..where((tbl) => tbl.habbit.equals(habbit.id)))
.write(
HabbitEntryCompanion(
deletionTime: Value(
DateTime.now(),
),
),
);
(MyDatabase.instance.update(MyDatabase.instance.habbit)
..where((tbl) => tbl.id.equals(habbit.id)))
.write(
HabbitCompanion(
deletionTime: Value(
DateTime.now(),
),
),
);
if (context.mounted) {
Navigator.of(context).pop<bool>(true);
}
Expand Down
30 changes: 14 additions & 16 deletions lib/components/entryform.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import 'package:date_field/date_field.dart';
import 'package:drift/drift.dart' as drift;
import 'package:flutter/material.dart';
import 'package:realm/realm.dart';

import '../models/core.dart';
import '../models/database.dart';
import '../models/drift.dart';

class EntryDialogForm extends StatefulWidget {
const EntryDialogForm({
Expand All @@ -15,17 +15,17 @@ class EntryDialogForm extends StatefulWidget {

final DateTime? creationTime;
final String? description;
final ObjectId? habbit;
final String habbit;

@override
State<EntryDialogForm> createState() => _EntryDialogFormState();

static Future<void> editEntry({
required BuildContext context,
required ObjectId? habbitId,
required HabbitEntry entry,
required String habbitId,
required HabbitEntryData entry,
}) async {
final editedData = await showDialog<HabbitEntry>(
final editedData = await showDialog<HabbitEntryCompanion>(
context: context,
builder: (BuildContext context) {
return EntryDialogForm(
Expand All @@ -36,10 +36,9 @@ class EntryDialogForm extends StatefulWidget {
},
);
if (editedData != null) {
editedData.id = entry.id;
await MyDatabase.instance.writeAsync(
() => MyDatabase.instance.add(editedData, update: true),
);
(MyDatabase.instance.update(MyDatabase.instance.habbitEntry)
..where((tbl) => tbl.id.equals(entry.id)))
.write(editedData);
}
}
}
Expand Down Expand Up @@ -91,12 +90,11 @@ class _EntryDialogFormState extends State<EntryDialogForm> {
),
TextButton(
onPressed: () {
Navigator.of(context).pop<HabbitEntry>(
HabbitEntry(
ObjectId(),
_selectedDate,
description: _descriptionFieldController.text,
habbit: MyDatabase.instance.find(widget.habbit),
Navigator.of(context).pop<HabbitEntryCompanion>(
HabbitEntryCompanion(
creationTime: drift.Value(_selectedDate),
description: drift.Value(_descriptionFieldController.text),
habbit: drift.Value(widget.habbit),
),
);
},
Expand Down
7 changes: 3 additions & 4 deletions lib/components/habbit_entry_tile.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:flutter/material.dart';
import 'package:realm/realm.dart';

import '../helpers/stats.dart';
import '../models/core.dart';
Expand All @@ -14,8 +13,8 @@ class HabbitEntryTile extends StatelessWidget {
required this.habbit,
});

final HabbitEntry entry;
final ObjectId habbit;
final HabbitEntryData entry;
final String habbit;

Future<bool?> _confirmDelete(BuildContext context) {
return showDialog<bool>(
Expand All @@ -39,7 +38,7 @@ class HabbitEntryTile extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Dismissible(
key: Key(entry.id.toString()),
key: Key(entry.id),
background: deleteDismissible,
secondaryBackground: deleteDismissible,
confirmDismiss: (direction) => _confirmDelete(context),
Expand Down
41 changes: 19 additions & 22 deletions lib/components/habbitform.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import 'package:drift/drift.dart' as drift;
import 'package:flutter/material.dart';
import 'package:realm/realm.dart';

import '../models/config.dart';
import '../models/core.dart';
import '../models/database.dart';
import '../models/drift.dart';

class HabbitDialogForm extends StatefulWidget {
const HabbitDialogForm({
Expand All @@ -24,28 +24,27 @@ class HabbitDialogForm extends StatefulWidget {

static Future<void> editEntry({
required BuildContext context,
required Habbit? habbit,
required String habbitId,
required HabbitData? habbit,
}) async {
final habbit0 = habbit;
if (habbit0 == null) {
if (habbit == null) {
throw StateError("habbit should not be null");
}
final editedData = await showDialog<Habbit>(
final editedData = await showDialog<HabbitCompanion>(
context: context,
builder: (BuildContext context) {
return HabbitDialogForm(
name: habbit0.name,
description: habbit0.description,
config: habbit0.config,
hidden: habbit0.hidden,
name: habbit.name,
description: habbit.description,
config: habbit.config,
hidden: habbit.hidden,
);
},
);
if (editedData != null) {
editedData.id = habbit0.id;
MyDatabase.instance.writeAsync(() {
MyDatabase.instance.add<Habbit>(editedData, update: true);
});
(MyDatabase.instance.update(MyDatabase.instance.habbit)
..where((tbl) => tbl.id.equals(habbitId)))
.write(editedData);
}
}
}
Expand Down Expand Up @@ -124,14 +123,12 @@ class _HabbitDialogFormState extends State<HabbitDialogForm> {
),
TextButton(
onPressed: () {
Navigator.of(context).pop<Habbit>(
Habbit(
ObjectId(),
_nameFieldController.text,
DateTime.now(),
description: _descriptionFieldController.text,
config: _habbitConfig.code,
hidden: _hidden,
Navigator.of(context).pop<HabbitCompanion>(
HabbitCompanion(
name: drift.Value(_nameFieldController.text),
description: drift.Value(_descriptionFieldController.text),
config: drift.Value(_habbitConfig.code),
hidden: drift.Value(_hidden),
),
);
},
Expand Down
Loading

0 comments on commit b4abceb

Please sign in to comment.