Skip to content

Commit

Permalink
Add notebook and children page in drawer
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed Apr 30, 2024
1 parent 84f465f commit b807aba
Show file tree
Hide file tree
Showing 11 changed files with 197 additions and 74 deletions.
83 changes: 24 additions & 59 deletions api/lib/models/event/item/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,11 @@ class CalendarItemDatabaseService extends CalendarItemService
}
if (start != null) {
where = where == null ? 'start >= ?' : '$where AND start >= ?';
whereArgs = whereArgs == null
? [start.secondsSinceEpoch]
: [...whereArgs, start.secondsSinceEpoch];
whereArgs = [...?whereArgs, start.secondsSinceEpoch];
}
if (end != null) {
where = where == null ? 'end <= ?' : '$where AND end <= ?';
whereArgs = whereArgs == null
? [end.secondsSinceEpoch]
: [...whereArgs, end.secondsSinceEpoch];
whereArgs = [...?whereArgs, end.secondsSinceEpoch];
}
if (date != null) {
var startCalendarItem = date.onlyDate();
Expand All @@ -91,24 +87,15 @@ class CalendarItemDatabaseService extends CalendarItemService
where = where == null
? '(start BETWEEN ? AND ? OR end BETWEEN ? AND ? OR (start <= ? AND end >= ?))'
: '$where AND (start BETWEEN ? AND ? OR end BETWEEN ? AND ? OR (start <= ? AND end >= ?))';
whereArgs = whereArgs == null
? [
startCalendarItem.secondsSinceEpoch,
endCalendarItem.secondsSinceEpoch,
startCalendarItem.secondsSinceEpoch,
endCalendarItem.secondsSinceEpoch,
startCalendarItem.secondsSinceEpoch,
endCalendarItem.secondsSinceEpoch,
]
: [
...whereArgs,
startCalendarItem.secondsSinceEpoch,
endCalendarItem.secondsSinceEpoch,
startCalendarItem.secondsSinceEpoch,
endCalendarItem.secondsSinceEpoch,
startCalendarItem.secondsSinceEpoch,
endCalendarItem.secondsSinceEpoch,
];
whereArgs = [
...?whereArgs,
startCalendarItem.secondsSinceEpoch,
endCalendarItem.secondsSinceEpoch,
startCalendarItem.secondsSinceEpoch,
endCalendarItem.secondsSinceEpoch,
startCalendarItem.secondsSinceEpoch,
endCalendarItem.secondsSinceEpoch,
];
}
if (pending) {
where = where == null
Expand All @@ -119,60 +106,38 @@ class CalendarItemDatabaseService extends CalendarItemService
where = where == null
? '(name LIKE ? OR description LIKE ?)'
: '$where AND (name LIKE ? OR description LIKE ?)';
whereArgs = whereArgs == null
? ['%$search%', '%$search%']
: [...whereArgs, '%$search%', '%$search%'];
whereArgs = [...?whereArgs, '%$search%', '%$search%'];
}
if (groupId != null) {
where = where == null ? 'groupId = ?' : '$where AND groupId = ?';
whereArgs = whereArgs == null
? [groupId.fullBytes]
: [...whereArgs, groupId.fullBytes];
final statement = "(groupId = ? OR events.groupId = ?)";
where = where == null ? statement : '$where AND $statement';
whereArgs = [...?whereArgs, groupId.fullBytes, groupId.fullBytes];
}
if (placeId != null) {
where = where == null ? 'placeId = ?' : '$where AND placeId = ?';
whereArgs = whereArgs == null
? [placeId.fullBytes]
: [...whereArgs, placeId.fullBytes];
final statement = "(placeId = ? OR events.placeId = ?)";
where = where == null ? statement : '$where AND $statement';
whereArgs = [...?whereArgs, placeId.fullBytes, placeId.fullBytes];
}
if (eventId != null) {
where = where == null ? 'eventId = ?' : '$where AND eventId = ?';
whereArgs = whereArgs == null
? [eventId.fullBytes]
: [...whereArgs, eventId.fullBytes];
whereArgs = [...?whereArgs, eventId.fullBytes];
}
final result = await db?.query(
"calendarItems LEFT JOIN events ON events.id = calendarItems.eventId",
columns: [
"events.*",
"calendarItems.runtimeType AS calendarItemruntimeType",
"calendarItems.id AS calendarItemid",
"calendarItems.name AS calendarItemname",
"calendarItems.description AS calendarItemdescription",
"calendarItems.location AS calendarItemlocation",
"calendarItems.eventId AS calendarItemeventId",
"calendarItems.start AS calendarItemstart",
"calendarItems.end AS calendarItemend",
"calendarItems.status AS calendarItemstatus",
"calendarItems.repeatType AS calendarItemrepeatType",
"calendarItems.interval AS calendarIteminterval",
"calendarItems.variation AS calendarItemvariation",
"calendarItems.count AS calendarItemcount",
"calendarItems.until AS calendarItemuntil",
"calendarItems.exceptions AS calendarItemexceptions",
"calendarItems.autoGroupId AS calendarItemautoGroupId",
"calendarItems.searchStart AS calendarItemsearchStart",
"calendarItems.autoDuration AS calendarItemautoDuration",
"calendarItems.*",
],
where: where,
whereArgs: whereArgs,
);
const itemsPrefix = "calendarItems.";
return result?.map((e) {
return ConnectedModel<CalendarItem, Event?>(
CalendarItem.fromDatabase(Map.fromEntries(e.entries
.where((element) => element.key.startsWith('calendarItem'))
.map((e) => MapEntry(
e.key.substring("calendarItem".length), e.value)))),
.where((element) => element.key.startsWith(itemsPrefix))
.map((e) =>
MapEntry(e.key.substring(itemsPrefix.length), e.value)))),
e['id'] == null ? null : Event.fromDatabase(e),
);
}).toList() ??
Expand Down
5 changes: 5 additions & 0 deletions api/lib/models/note/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ class NoteDatabaseService extends NoteService with TableService {
int offset = 0,
int limit = 50,
Multihash? parent,
Multihash? notebook,
Set<Multihash> labels = const {},
Set<NoteStatus?> statuses = const {
NoteStatus.todo,
Expand All @@ -191,6 +192,10 @@ class NoteDatabaseService extends NoteService with TableService {
where == null ? 'parentId IS NULL' : '$where AND parentId IS NULL';
}
}
if (notebook != null) {
where = where == null ? 'notebookId = ?' : '$where AND notebookId = ?';
whereArgs = [...?whereArgs, notebook.fullBytes];
}
var statusStatement =
"status IN (${statuses.whereNotNull().map((e) => "'${e.name}'").join(',')})";
if (statuses.contains(null)) {
Expand Down
1 change: 1 addition & 0 deletions api/lib/models/note/service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ abstract class NoteService extends ModelService {
int offset = 0,
int limit = 50,
Multihash? parent,
Multihash? notebook,
Set<NoteStatus?> statuses = const {
NoteStatus.todo,
NoteStatus.inProgress,
Expand Down
4 changes: 4 additions & 0 deletions api/lib/services/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,7 @@ Multihash createUniqueMultihash() {
List.generate(8, (i) => random.nextInt(255)));
return Multihash(uuid);
}

Multihash createEmptyMultihash() {
return Multihash(Uint8List.fromList([]));
}
3 changes: 2 additions & 1 deletion app/lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -294,5 +294,6 @@
"standard": "Standard",
"highContrast": "High contrast",
"labels": "Labels",
"systemLocale": "System locale"
"systemLocale": "System locale",
"notebooks": "Notebooks"
}
64 changes: 64 additions & 0 deletions app/lib/pages/notes/navigator/children.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
part of 'drawer.dart';

class _NoteChildrenView extends StatefulWidget {
final SourcedModel<Notebook?>? notebook;
final Multihash? parent;

const _NoteChildrenView({
this.parent,
this.notebook,
});

@override
State<_NoteChildrenView> createState() => _NoteChildrenViewState();
}

class _NoteChildrenViewState extends State<_NoteChildrenView> {
late final SourcedPagingController<Note> _controller;

@override
void initState() {
super.initState();

_controller = SourcedPagingController(
context.read<FlowCubit>(),
);
_controller.addFetchListener((source, service, offset, limit) async {
final notebook = widget.notebook;
if (notebook != null && notebook.source != source) return [];
return service.note?.getNotes(
offset: offset,
limit: limit,
parent: widget.parent,
notebook: notebook?.source == source ? notebook?.model?.id : null,
);
});
}

@override
void dispose() {
super.dispose();
_controller.dispose();
}

@override
void didUpdateWidget(covariant _NoteChildrenView oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.parent != widget.parent) {
_controller.refresh();
}
}

@override
Widget build(BuildContext context) {
return PagedListView(
pagingController: _controller,
builderDelegate: buildMaterialPagedDelegate<SourcedModel<Note>>(
_controller,
(ctx, item, index) => ListTile(
title: Text(item.model.name),
),
),
);
}
}
53 changes: 45 additions & 8 deletions app/lib/pages/notes/navigator/drawer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,72 @@ import 'package:flow/pages/notes/label.dart';
import 'package:flow/widgets/builder_delegate.dart';
import 'package:flow_api/models/label/model.dart';
import 'package:flow_api/models/model.dart';
import 'package:flow_api/models/note/model.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
import 'package:lib5/lib5.dart';
import 'package:material_leap/material_leap.dart';
import 'package:material_leap/widgets.dart';
import 'package:phosphor_flutter/phosphor_flutter.dart';

part 'children.dart';
part 'labels.dart';
part 'notebooks.dart';

class NotesNavigatorDrawer extends StatelessWidget {
final Multihash? selectedLabel;
final String source;
final Notebook? notebook;
final Multihash? note, selectedLabel;
final LabelChangedCallback? onLabelChanged;

const NotesNavigatorDrawer({
super.key,
this.source = '',
this.note,
this.notebook,
this.selectedLabel,
this.onLabelChanged,
});

@override
Widget build(BuildContext context) {
return ListView(
children: [
_NoteLabelsView(
onChanged: onLabelChanged,
selected: selectedLabel,
),
],
final sourcedNotebook =
notebook == null ? null : SourcedModel(source, notebook);
return DefaultTabController(
length: 2,
child: Column(
children: [
_NoteLabelsView(
onChanged: onLabelChanged,
selected: selectedLabel,
),
TabBar(tabs: [
HorizontalTab(
label: Text(AppLocalizations.of(context).notes),
icon: const PhosphorIcon(PhosphorIconsLight.article),
),
HorizontalTab(
label: Text(AppLocalizations.of(context).notebooks),
icon: const PhosphorIcon(PhosphorIconsLight.fileArchive),
),
]),
Expanded(
child: TabBarView(
children: [
_NoteChildrenView(
parent: note,
notebook: sourcedNotebook,
),
_NotebooksView(
model: sourcedNotebook,
),
],
),
),
],
),
);
}
}
41 changes: 41 additions & 0 deletions app/lib/pages/notes/navigator/notebooks.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
part of 'drawer.dart';

class _NotebooksView extends StatefulWidget {
final SourcedModel<Notebook?>? model;

const _NotebooksView({
this.model,
});

@override
State<_NotebooksView> createState() => _NotebooksViewState();
}

class _NotebooksViewState extends State<_NotebooksView> {
late final SourcedPagingController<Notebook> _controller;

@override
void initState() {
super.initState();

_controller = SourcedPagingController(
context.read<FlowCubit>(),
);
_controller.addFetchListener((source, service, offset, limit) async {
return [];
});
}

@override
Widget build(BuildContext context) {
return PagedListView(
pagingController: _controller,
builderDelegate: buildMaterialPagedDelegate<SourcedModel<Notebook>>(
_controller,
(ctx, item, index) => ListTile(
title: Text(item.model.name),
),
),
);
}
}
7 changes: 6 additions & 1 deletion app/lib/pages/notes/note.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ class NoteDialog extends StatefulWidget {
final String? source;
final Note? note;
final bool create;
const NoteDialog({super.key, this.create = false, this.note, this.source});
const NoteDialog({
super.key,
this.create = false,
this.note,
this.source,
});

@override
State<NoteDialog> createState() => _NoteDialogState();
Expand Down
Loading

0 comments on commit b807aba

Please sign in to comment.