refactor(browser): only get bookmarks when opening the browser

Fixes #9440
This commit is contained in:
Jonathan Rainville 2023-02-28 15:29:09 -05:00
parent f10299ba6f
commit 72d40346f6
6 changed files with 41 additions and 15 deletions

View File

@ -9,6 +9,9 @@ method delete*(self: AccessInterface) {.base.} =
method load*(self: AccessInterface) {.base.} =
raise newException(ValueError, "No implementation available")
method onActivated*(self: AccessInterface) {.base.} =
raise newException(ValueError, "No implementation available")
method isLoaded*(self: AccessInterface): bool {.base.} =
raise newException(ValueError, "No implementation available")

View File

@ -16,6 +16,7 @@ type
view: View
viewVariant: QVariant
moduleLoaded: bool
bookmarksLoaded: bool
controller: Controller
proc newModule*(delegate: delegate_interface.AccessInterface, events: EventEmitter, bookmarkService: bookmark_service.Service): Module =
@ -24,6 +25,7 @@ proc newModule*(delegate: delegate_interface.AccessInterface, events: EventEmitt
result.view = view.newView(result)
result.viewVariant = newQVariant(result.view)
result.moduleLoaded = false
result.bookmarksLoaded = false
result.controller = controller.newController(result, events, bookmarkService)
method delete*(self: Module) =
@ -36,14 +38,20 @@ method load*(self: Module) =
self.controller.init()
self.view.load()
method isLoaded*(self: Module): bool =
return self.moduleLoaded
method onActivated*(self: Module) =
if self.bookmarksLoaded:
return
method viewDidLoad*(self: Module) =
let bookmarks = self.controller.getBookmarks()
for b in bookmarks:
self.view.addItem(initItem(b.name, b.url, b.imageUrl))
self.bookmarksLoaded = true
method isLoaded*(self: Module): bool =
return self.moduleLoaded
method viewDidLoad*(self: Module) =
self.moduleLoaded = true
self.delegate.bookmarkDidLoad()

View File

@ -7,6 +7,9 @@ method delete*(self: AccessInterface) {.base.} =
method load*(self: AccessInterface) {.base.} =
raise newException(ValueError, "No implementation available")
method onActivated*(self: AccessInterface) {.base.} =
raise newException(ValueError, "No implementation available")
method isLoaded*(self: AccessInterface): bool {.base.} =
raise newException(ValueError, "No implementation available")

View File

@ -69,6 +69,9 @@ method load*(self: Module) =
self.dappsModule.load()
self.view.load()
method onActivated*(self: Module) =
self.bookmarkModule.onActivated()
method isLoaded*(self: Module): bool =
return self.moduleLoaded

View File

@ -669,8 +669,12 @@ method activeSectionSet*[T](self: Module[T], sectionId: string) =
echo "main-module, incorrect section id: ", sectionId
return
if sectionId == conf.COMMUNITIESPORTAL_SECTION_ID:
self.communitiesModule.onActivated()
case sectionId:
of conf.COMMUNITIESPORTAL_SECTION_ID:
self.communitiesModule.onActivated()
of conf.BROWSER_SECTION_ID:
self.browserSectionModule.onActivated()
self.view.model().setActiveSection(sectionId)
self.view.activeSectionSet(item)

View File

@ -19,6 +19,7 @@ const SIGNAL_BOOKMARK_UPDATED* = "bookmarkUpdated"
type
Service* = ref object of RootObj
bookmarks: Table[string, BookmarkDto] # [url, BookmarkDto]
bookmarksFetched: bool
events: EventEmitter
type R = Result[BookmarkDto, string]
@ -38,18 +39,9 @@ proc newService*(events: EventEmitter): Service =
result = Service()
result.events = events
result.bookmarks = initTable[string, BookmarkDto]()
result.bookmarksFetched = false
proc init*(self: Service) =
try:
let response = backend.getBookmarks()
for bookmark in response.result.getElems().mapIt(it.toBookmarkDto()):
if not bookmark.removed:
self.bookmarks[bookmark.url] = bookmark
except Exception as e:
let errDescription = e.msg
error "error: ", errDescription
self.events.on(SignalType.Message.event) do(e: Args):
var receivedData = MessageSignal(e)
if receivedData.bookmarks.len > 0:
@ -77,7 +69,20 @@ proc init*(self: Service) =
self.events.emit(SIGNAL_BOOKMARK_ADDED, BookmarkArgs(bookmark: self.bookmarks[url]))
proc fetchBookmarks*(self: Service) =
# TODO later we can make this async, but it's not worth it for now
try:
let response = backend.getBookmarks()
for bookmark in response.result.getElems().mapIt(it.toBookmarkDto()):
if not bookmark.removed:
self.bookmarks[bookmark.url] = bookmark
self.bookmarksFetched = true
except Exception as e:
error "error fetching bookmarks: ", msg=e.msg
proc getBookmarks*(self: Service): seq[BookmarkDto] =
if not self.bookmarksFetched:
self.fetchBookmarks()
return toSeq(self.bookmarks.values)
proc storeBookmark*(self: Service, url, name: string): R =