Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion lib/common/navigation/app_bars/notes/notes_app_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ import '../../../preferences/watched_preferences.dart';
import '../../../widgets/notes/note_tile.dart';
import '../../../widgets/placeholders/empty_placeholder.dart';

/// How long the search waits for the user to stop typing.
const _searchDebounce = Duration(milliseconds: 300);

/// The minimum number of characters of a search.
const _searchMinimumLength = 2;

/// The number of searches started, to skip the outdated ones.
var _searchGeneration = 0;

/// The latest search started.
Future<List<NoteTile>>? _latestSearch;

/// Notes list and bin's app bar.
///
/// Contains:
Expand Down Expand Up @@ -96,8 +108,34 @@ class NotesAppBar extends ConsumerWidget {
}

/// Searches for the notes that match the [search].
///
/// The search waits for the user to stop typing for [_searchDebounce] and needs at least [_searchMinimumLength]
/// characters. If a newer search starts meanwhile, the results of the newer one are returned instead, so that the
/// results of an outdated search never replace the ones of the current text.
Future<List<NoteTile>> searchNotes(String? search) async {
if (search == null || search.isEmpty) {
final generation = ++_searchGeneration;
final searchFuture = _search(search, generation);
_latestSearch = searchFuture;

Future<List<NoteTile>> latestSearch;
List<NoteTile> results;
do {
latestSearch = _latestSearch!;
results = await latestSearch;
} while (latestSearch != _latestSearch);

return results;
}

/// Performs the search of the [search] if it's still the latest one (its [generation]) after the debounce.
Future<List<NoteTile>> _search(String? search, int generation) async {
if (search == null || search.trim().length < _searchMinimumLength) {
return [];
}

await Future<void>.delayed(_searchDebounce);

if (generation != _searchGeneration) {
return [];
}

Expand Down
41 changes: 40 additions & 1 deletion lib/common/widgets/notes/note_tile.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:gap/gap.dart';
Expand Down Expand Up @@ -26,6 +28,12 @@ import 'dismissible/available_dismissible.dart';
import 'dismissible/deleted_dismissible.dart';
import 'note_tile_labels_list.dart';

/// In the search view, the offset of the first match from which the content preview starts near it.
const _searchPreviewMaximumOffset = 80;

/// In the search view, the number of characters shown before the first match when the preview starts near it.
const _searchPreviewContext = 30;

/// List tile that displays the main info about a note.
///
/// A custom `ListTile` widget is created because the default one doesn't allow not displaying a title.
Expand Down Expand Up @@ -191,6 +199,37 @@ class _NoteTileState extends ConsumerState<NoteTile> {
toggleSelectNote(ref, note: widget.note);
}

/// Returns the content preview to show in the search view for the [search].
///
/// If the first match of the [search] is too far for the preview, the preview starts a few characters before it,
/// so that the match is visible and highlighted.
String searchPreview(String search) {
final preview = widget.note.contentPreview;
final lowerPreview = preview.toLowerCase();

final indexes = search
.toLowerCase()
.split(' ')
.where((word) => word.isNotEmpty)
.map((word) => lowerPreview.indexOf(word))
.where((index) => index >= 0);

if (indexes.isEmpty) {
return preview;
}

final firstMatch = indexes.reduce(min);

if (firstMatch < _searchPreviewMaximumOffset) {
return preview;
}

final lineStart = preview.lastIndexOf('\n', firstMatch) + 1;
final start = max(lineStart, firstMatch - _searchPreviewContext);

return '…${preview.substring(start)}';
}

@override
Widget build(BuildContext context) {
final showTitlesOnly = PreferenceKey.showTitlesOnly.preferenceOrDefault;
Expand Down Expand Up @@ -278,7 +317,7 @@ class _NoteTileState extends ConsumerState<NoteTile> {
if (showContent)
widget.search != null
? SubstringHighlight(
text: widget.note.contentPreview,
text: searchPreview(widget.search!),
terms: widget.search?.split(' '),
maxLines: maximumContentPreviewLines,
overflow: TextOverflow.ellipsis,
Expand Down
51 changes: 0 additions & 51 deletions lib/models/note/index/note_index.dart

This file was deleted.

25 changes: 18 additions & 7 deletions lib/services/database_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,31 @@ class DatabaseService {

DatabaseService._internal();

/// The schemas of the collections of the database.
static const schemas = [
NoteSchema,
PlainTextNoteSchema,
RichTextNoteSchema,
MarkdownNoteSchema,
ChecklistNoteSchema,
LabelSchema,
];

/// The name of the database.
static const databaseName = 'materialnotes';

/// Isar database instance.
late final Isar database;

/// The directory of the database.
late final String databaseDirectory;

/// Ensures the service is initialized.
Future<void> ensureInitialized() async {
final databaseName = 'materialnotes';
final databaseDirectory = (await getApplicationDocumentsDirectory()).path;
databaseDirectory = (await getApplicationDocumentsDirectory()).path;

// Initialize the database
database = await Isar.open(
[NoteSchema, PlainTextNoteSchema, RichTextNoteSchema, MarkdownNoteSchema, ChecklistNoteSchema, LabelSchema],
name: databaseName,
directory: databaseDirectory,
);
database = await Isar.open(schemas, name: databaseName, directory: databaseDirectory);

// Initialize the models services
await LabelsService().ensureInitialized();
Expand Down
Loading
Loading