Skip to content
Merged
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
2 changes: 1 addition & 1 deletion lib/pdf_book/view/pdf_book_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6171,7 +6171,7 @@ class _PdfBookScreenState extends State<PdfBookScreen>
return;
}
try {
await _personalNotesRepository.repositionNote(
await _personalNotesRepository.moveNote(
bookId: note.bookId,
noteId: note.id,
categoryId: widget.tab.book.categoryId,
Expand Down
16 changes: 16 additions & 0 deletions lib/personal_notes/bloc/personal_notes_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,18 @@ class PersonalNotesBloc extends Bloc<PersonalNotesEvent, PersonalNotesState> {
StartCreatingPersonalNote event,
Emitter<PersonalNotesState> emit,
) {
final isSameSession =
state.isCreatingNewNote &&
state.newNoteBookId == event.bookId &&
state.newNoteLineNumber == event.lineNumber &&
state.newNotePdfAnchor == event.pdfAnchor;
emit(
state.copyWith(
isCreatingNewNote: true,
newNoteRequestToken: state.newNoteRequestToken + 1,
newNoteSessionToken: isSameSession
? state.newNoteSessionToken
: state.newNoteSessionToken + 1,
clearExpandRequest: true,
clearNewNoteSourceSelection: true,
clearNewNotePdfAnchor: true,
Expand All @@ -277,6 +285,14 @@ class PersonalNotesBloc extends Bloc<PersonalNotesEvent, PersonalNotesState> {
CancelCreatingPersonalNote event,
Emitter<PersonalNotesState> emit,
) {
// השלמת כתיבה ישנה אינה מבטלת יצירה חדשה שכבר ממתינה בתור האירועים.
if ((event.sessionToken != null &&
event.sessionToken != state.newNoteSessionToken) ||
(event.bookId != null && event.bookId != state.newNoteBookId) ||
(event.lineNumber != null &&
event.lineNumber != state.newNoteLineNumber)) {
return;
}
emit(state.copyWith(clearNewNoteData: true));
}

Expand Down
13 changes: 12 additions & 1 deletion lib/personal_notes/bloc/personal_notes_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,18 @@ class StartCreatingPersonalNote extends PersonalNotesEvent {
}

class CancelCreatingPersonalNote extends PersonalNotesEvent {
const CancelCreatingPersonalNote();
final int? sessionToken;
final String? bookId;
final int? lineNumber;

const CancelCreatingPersonalNote({
this.sessionToken,
this.bookId,
this.lineNumber,
});

@override
List<Object?> get props => [sessionToken, bookId, lineNumber];
}

class UpdateSearchQuery extends PersonalNotesEvent {
Expand Down
7 changes: 7 additions & 0 deletions lib/personal_notes/bloc/personal_notes_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class PersonalNotesState extends Equatable {
final bool isCreatingNewNote;
// בקשה זהה עדיין צריכה לחשוף עורך שנגלל מחוץ למסך.
final int newNoteRequestToken;
// חשיפה חוזרת משמרת את היצירה; ביטול או מעבר מיקום מתחילים יצירה אחרת.
final int newNoteSessionToken;
final String? newNoteBookId;
final int? newNoteLineNumber;
final String? newNoteReferenceText;
Expand Down Expand Up @@ -57,6 +59,7 @@ class PersonalNotesState extends Equatable {
this.expandRequestToken = 0,
this.isCreatingNewNote = false,
this.newNoteRequestToken = 0,
this.newNoteSessionToken = 0,
this.newNoteBookId,
this.newNoteLineNumber,
this.newNoteReferenceText,
Expand Down Expand Up @@ -87,6 +90,7 @@ class PersonalNotesState extends Equatable {
expandRequestToken = 0,
isCreatingNewNote = false,
newNoteRequestToken = 0,
newNoteSessionToken = 0,
newNoteBookId = null,
newNoteLineNumber = null,
newNoteReferenceText = null,
Expand Down Expand Up @@ -116,6 +120,7 @@ class PersonalNotesState extends Equatable {
int? expandRequestToken,
bool? isCreatingNewNote,
int? newNoteRequestToken,
int? newNoteSessionToken,
String? newNoteBookId,
int? newNoteLineNumber,
String? newNoteReferenceText,
Expand Down Expand Up @@ -157,6 +162,7 @@ class PersonalNotesState extends Equatable {
? false
: (isCreatingNewNote ?? this.isCreatingNewNote),
newNoteRequestToken: newNoteRequestToken ?? this.newNoteRequestToken,
newNoteSessionToken: newNoteSessionToken ?? this.newNoteSessionToken,
newNoteBookId: clearNewNoteData
? null
: (newNoteBookId ?? this.newNoteBookId),
Expand Down Expand Up @@ -217,6 +223,7 @@ class PersonalNotesState extends Equatable {
expandRequestToken,
isCreatingNewNote,
newNoteRequestToken,
newNoteSessionToken,
newNoteBookId,
newNoteLineNumber,
newNoteReferenceText,
Expand Down
21 changes: 21 additions & 0 deletions lib/personal_notes/repository/personal_notes_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ class PersonalNotesRepository {
required String noteId,
}) => _service.deleteNote(bookId: bookId, noteId: noteId);

/// מסיים לאחר המחיקה; המאזינים לשינויים מרעננים בנפרד.
Future<void> removeNote({required String noteId}) =>
_service.removeNote(noteId: noteId);

Future<List<PersonalNote>> repositionNote({
required String bookId,
required String noteId,
Expand All @@ -190,6 +194,23 @@ class PersonalNotesRepository {
);
}

Future<PersonalNote> moveNote({
required String bookId,
required String noteId,
required int lineNumber,
PdfNoteAnchor? pdfAnchor,
int? categoryId,
}) async {
final bookContent = await _loadBookContent(bookId, categoryId: categoryId);
return _service.moveNote(
bookId: bookId,
bookContent: bookContent,
noteId: noteId,
lineNumber: lineNumber,
pdfAnchor: pdfAnchor,
);
}

/// תוכן הספר של מפתח ההערות [bookId], כפי שההערות מעוגנות אליו.
Future<String> loadBookContent(String bookId, {int? categoryId}) =>
_loadBookContent(bookId, categoryId: categoryId, refresh: true);
Expand Down
76 changes: 65 additions & 11 deletions lib/personal_notes/services/personal_note_draft_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,26 @@ class PersonalNoteDraftService {
/// ההגדרות יחריג אותה — טיוטה היא תוכן הערה אישית, לא הגדרה.
static const keyPrefix = 'personal_note_draft:';

// גרסה לתחרות בין פעולות אסינכרוניות באותו תהליך; אינה חלק מהקובץ.
// מופעי שירות שונים חולקים מפתחות טיוטה, ולכן חולקים גם את הגרסאות.
static final _versions = <String, Object>{};
static const _anyVersion = Object();

Object? draftVersion({
required String bookId,
int? categoryId,
int? lineNumber,
String? noteId,
PdfNoteAnchor? pdfAnchor,
}) =>
_versions[_key(
bookId,
categoryId: categoryId,
lineNumber: lineNumber,
noteId: noteId,
pdfAnchor: pdfAnchor,
)];

String _key(
String bookId, {
int? categoryId,
Expand Down Expand Up @@ -139,6 +159,7 @@ class PersonalNoteDraftService {
noteId: noteId,
pdfAnchor: draft.pdfAnchor,
);
final version = _versions[key] = Object();
final normalizedDraft = PersonalNoteDraft(
content: draft.content,
contentPlain: draft.contentPlain,
Expand All @@ -157,6 +178,8 @@ class PersonalNoteDraftService {
categoryId: categoryId,
lineNumber: lineNumber,
);
final legacyVersion = _versions[legacyKey];
final legacyRaw = Settings.getValue<String>(legacyKey);
final legacyDraft = _readDraft(legacyKey);
if (legacyDraft?.pdfAnchor != null) {
final pointKey = _key(
Expand All @@ -171,10 +194,19 @@ class PersonalNoteDraftService {
jsonEncode(legacyDraft.toJson()),
);
}
await Settings.setValue<String?>(legacyKey, null);
if (identical(_versions[legacyKey], legacyVersion) &&
Settings.getValue<String>(legacyKey) == legacyRaw) {
await Settings.setValue<String?>(legacyKey, null);
}
}
}
await Settings.setValue<String>(key, jsonEncode(normalizedDraft.toJson()));
// העברה ממפתח PDF ישן עשויה להמתין. כתיבה/ניקוי חדשים גוברים עליה.
if (identical(_versions[key], version)) {
await Settings.setValue<String>(
key,
jsonEncode(normalizedDraft.toJson()),
);
}
}

Future<void> clearDraft({
Expand All @@ -183,6 +215,7 @@ class PersonalNoteDraftService {
int? lineNumber,
String? noteId,
PdfNoteAnchor? pdfAnchor,
Object? expectedVersion = _anyVersion,
}) async {
final key = _key(
bookId,
Expand All @@ -191,21 +224,42 @@ class PersonalNoteDraftService {
noteId: noteId,
pdfAnchor: pdfAnchor,
);
if (!identical(expectedVersion, _anyVersion) &&
!identical(_versions[key], expectedVersion)) {
return;
}
final version = _versions[key];
final clearingVersion = _versions[key] = Object();
if (noteId == null &&
pdfAnchor == null &&
_readDraft(key)?.pdfAnchor != null) {
// ביטול כתיבת טקסט שממתינה להעברת PDF אינו מוחק את ה-PDF הישן.
_versions.remove(key);
return;
}
await Settings.setValue<String?>(key, null);
if (noteId == null && pdfAnchor != null) {
final legacyKey = _key(
bookId,
categoryId: categoryId,
lineNumber: lineNumber,
);
if (_readDraft(legacyKey)?.pdfAnchor == pdfAnchor) {
await Settings.setValue<String?>(legacyKey, null);
final legacyKey = noteId == null && pdfAnchor != null
? _key(bookId, categoryId: categoryId, lineNumber: lineNumber)
: null;
try {
// שתי המחיקות מתחילות לפני ההמתנה. כך writer חדש אינו פוגש
// PDF שכבר נוקה בנקודה אבל עדיין ממתין למחיקת המפתח הישן שלו.
await Future.wait([
Settings.setValue<String?>(key, null),
if (legacyKey != null && _readDraft(legacyKey)?.pdfAnchor == pdfAnchor)
Settings.setValue<String?>(legacyKey, null),
]);
// עורך שנפתח בזמן הניקוי מזהה שהצילום שלו כבר אינו טיוטה פעילה.
if (identical(_versions[key], clearingVersion)) _versions.remove(key);
} catch (_) {
// כשל מחיקה מאפשר ניסיון חוזר, בלי להחליף גרסה שנכתבה בינתיים.
if (identical(_versions[key], clearingVersion)) {
if (version == null) {
_versions.remove(key);
} else {
_versions[key] = version;
}
}
rethrow;
}
}

Expand Down
32 changes: 28 additions & 4 deletions lib/personal_notes/services/personal_notes_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -226,20 +226,45 @@ class PersonalNotesService {
required String bookId,
required String noteId,
}) async {
await _database.deleteNote(noteId);
await removeNote(noteId: noteId);
return await _database.loadNotes(bookId);
}

/// אישור המחיקה אינו תלוי בקריאת הרשימה שאחריה.
Future<void> removeNote({required String noteId}) =>
_database.deleteNote(noteId);

Future<List<PersonalNote>> repositionNote({
required String bookId,
required String bookContent,
required String noteId,
required int lineNumber,
PdfNoteAnchor? pdfAnchor,
}) async {
await moveNote(
bookId: bookId,
bookContent: bookContent,
noteId: noteId,
lineNumber: lineNumber,
pdfAnchor: pdfAnchor,
);
return _database.loadNotes(bookId);
}

/// מחזיר את ההערה לאחר כתיבת המיקום, בלי לטעון את יתר הערות הספר.
Future<PersonalNote> moveNote({
required String bookId,
required String bookContent,
required String noteId,
required int lineNumber,
PdfNoteAnchor? pdfAnchor,
}) async {
final note = await _database.getNote(noteId);
if (note == null) {
return await _database.loadNotes(bookId);
throw StateError(NotesMessages.noteNoLongerExists);
}
if (note.bookId != bookId) {
throw StateError(NotesMessages.noteMovedToDifferentBook);
}

final lines = splitBookContentIntoLines(bookContent);
Expand Down Expand Up @@ -272,8 +297,7 @@ class PersonalNotesService {
);

await _database.updateNote(updatedNote);

return await _database.loadNotes(bookId);
return updatedNote;
}

PersonalNote _reconcileLocation(
Expand Down
4 changes: 2 additions & 2 deletions lib/personal_notes/view/personal_notes_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1517,7 +1517,7 @@ class _PersonalNotesManagerScreenState
if (shouldDelete == true) {
if (!mounted) return;
try {
await _repository.deleteNote(bookId: note.bookId, noteId: note.id);
await _repository.removeNote(noteId: note.id);
if (mounted) UiSnack.show(NotesMessages.noteDeleted);
} catch (error) {
if (mounted) UiSnack.showError(NotesMessages.noteDeleteFailed(error));
Expand Down Expand Up @@ -1546,7 +1546,7 @@ class _PersonalNotesManagerScreenState
if (newLine != null) {
if (!mounted) return;
try {
await _repository.repositionNote(
await _repository.moveNote(
bookId: note.bookId,
noteId: note.id,
lineNumber: newLine,
Expand Down
Loading
Loading