Skip to content

Commit a743186

Browse files
Improve bookmarking
1 parent 24fd06a commit a743186

4 files changed

Lines changed: 23 additions & 26 deletions

File tree

lib/models/json_to_bible.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@ abstract class JsonToBible {
2222
}
2323

2424
String bookmarkIconHTML(String verseId, List<String> bookmarks) {
25-
bool isBookmarked = bookmarks.contains(verseId.replaceAll('primary-', '').replaceAll('secondary-', ''));
25+
verseId = verseId.replaceAll('primary-', '').replaceAll('secondary-', '');
26+
27+
bool isBookmarked = bookmarks.contains(verseId);
28+
2629
String svg =
27-
"""<svg id="$verseId-svg" class="svg ${isBookmarked ? "bookmarked" : ""}" style="fill: ${verseId.textToHslColor()} !important;" xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 256 256"><path d="M184,32H72A16,16,0,0,0,56,48V224a8,8,0,0,0,12.24,6.78L128,193.43l59.77,37.35A8,8,0,0,0,200,224V48A16,16,0,0,0,184,32Zm0,177.57-51.77-32.35a8,8,0,0,0-8.48,0L72,209.57V48H184Z"></path></svg>""";
30+
"""<svg class="svg $verseId-svg ${isBookmarked ? "bookmarked" : ""}" style="fill: ${verseId.textToHslColor()} !important;" xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 256 256"><path d="M184,32H72A16,16,0,0,0,56,48V224a8,8,0,0,0,12.24,6.78L128,193.43l59.77,37.35A8,8,0,0,0,200,224V48A16,16,0,0,0,184,32Zm0,177.57-51.77-32.35a8,8,0,0,0-8.48,0L72,209.57V48H184Z"></path></svg>""";
2831
return svg;
2932
}
3033

lib/ui/views/bookmarks/bookmarks_viewmodel.dart

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,9 @@ class BookmarksViewModel extends BaseViewModel {
2828
var [bookCode, chapter, verse] = bookmarkId.split('-');
2929

3030
_biblesService.setBook(bookCode);
31+
_biblesService.setChapter(int.parse(chapter));
32+
_biblesService.setVerse(int.parse(verse));
3133

32-
if (viewBy == ViewBy.section) {
33-
_biblesService.setChapter(int.parse(chapter));
34-
_biblesService.setVerse(int.parse(verse));
35-
} else {
36-
_biblesService.setChapter(int.parse(chapter));
37-
}
3834
_navigationService.clearStackAndShow(Routes.readerView);
3935
}
4036

lib/ui/views/navigation_sections_chapters/navigation_sections_chapters_viewmodel.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ class NavigationSectionsChaptersViewModel extends BaseViewModel {
8686
_biblesService.addBookToRecentHistory(bookCode);
8787

8888
_biblesService.setChapter(index);
89+
_biblesService.setVerse(1);
8990

9091
_navigationService.clearStackAndShow(Routes.readerView);
9192
}

lib/ui/views/reader/reader_viewmodel.dart

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -133,21 +133,12 @@ class ReaderViewModel extends ReactiveViewModel {
133133
themeName = CurrentTheme.contrast.name;
134134
}
135135

136-
// If viewBy is 'section', jump to a specific verse. Otherwise
137-
// for 'chapter' we fallback on the first verse of the chapter.
138136
// When scrolling is linked, only ``primaryScrollToId`` is used
139137
// so that even with different contents lengths the primary reader
140-
// area will be scrolled to the right place.
141-
String scrollToId = '$bookCode-$chapterNumber-1';
142-
String primaryScrollToId;
143-
String secondaryScrollToId;
144-
if (viewBy == ViewBy.section) {
145-
scrollToId = '$bookCode-$chapterNumber-$verseNumber';
146-
} else {
147-
scrollToId = '$bookCode-$chapterNumber-1';
148-
}
149-
primaryScrollToId = 'primary-$scrollToId';
150-
secondaryScrollToId = 'secondary-$scrollToId';
138+
// area will still be scrolled to the right place.
139+
String scrollToId = '$bookCode-$chapterNumber-$verseNumber';
140+
String primaryScrollToId = 'primary-$scrollToId';
141+
String secondaryScrollToId = 'secondary-$scrollToId';
151142

152143
log('Primary ID->: $primaryScrollToId | Secondary ID->: $secondaryScrollToId');
153144

@@ -516,14 +507,20 @@ class ReaderViewModel extends ReactiveViewModel {
516507
}
517508

518509
Future<void> setBookmark(String bookmarkId) async {
519-
// Update the icon
520-
await webviewController.runJavaScript('document.getElementById("$bookmarkId-svg").classList.toggle("bookmarked");');
521-
522-
List<String> existingBookmarks = await _settingsService.getBookmarks();
523-
524510
// Save ids without a specific reader area indentifier.
525511
bookmarkId = bookmarkId.replaceAll('primary-', '').replaceAll('secondary-', '');
526512

513+
// Update the icons in both reader areas.
514+
await webviewController.runJavaScript('''
515+
var svgElements = [...document.getElementsByClassName("$bookmarkId-svg")];
516+
517+
svgElements.forEach((ele) => {
518+
ele.classList.toggle("bookmarked");
519+
});
520+
''');
521+
522+
List<String> existingBookmarks = await _settingsService.getBookmarks();
523+
527524
// If the bookmark already exists, remove it.
528525
if (existingBookmarks.contains(bookmarkId)) {
529526
existingBookmarks.remove(bookmarkId);

0 commit comments

Comments
 (0)