Skip to content

Commit

Permalink
save on close
Browse files Browse the repository at this point in the history
  • Loading branch information
g.maksutenko committed Nov 16, 2021
1 parent b481d9f commit ecb4454
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 29 deletions.
4 changes: 1 addition & 3 deletions src/Load.nim
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const DEFAULT_CONTENT = """
]
"""


type PageSave* = object
pageName*: string
pageContent*: JsonNode
Expand Down Expand Up @@ -61,7 +62,6 @@ func getGroupsFromPage*(page: PageSave): seq[GroupSave] =
# debugEcho groupName
# debugEcho groupContent
result.add GroupSave(groupName: groupName, groupContent: groupContent)


func getTasksFromGroup*(group: GroupSave): seq[TaskSave] =
assert group.groupContent.kind == JArray
Expand All @@ -73,5 +73,3 @@ func getTasksFromGroup*(group: GroupSave): seq[TaskSave] =
note: task["note"].getStr,
done: task["done"].getBool
)

# readSaveFromFS().getPages()[0].getGroupsFromPage()[0].getTasksFromGroup.echo
6 changes: 5 additions & 1 deletion src/Save.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ proc getAllPages(tabView: TabView): seq[TabPage] =
for i in 0..<n:
result.add tabView.getNthPage(i)

proc save*(btn: Button, tabView: TabView): void =
proc save*(tabView: TabView) =
let allPages = tabView.getAllPages()
var jsonPages: seq[JsonNode]
for tabPage in allPages:
Expand All @@ -25,3 +25,7 @@ proc save*(btn: Button, tabView: TabView): void =
let savePath = getHomeDir() / SAVE_DIR_NAME
discard existsOrCreateDir(savePath)
writeFile(savePath / "state.json", pretty(% jsonPages))


proc saveBtnPressed*(btn: Button, tabView: TabView) =
save(tabView)
32 changes: 7 additions & 25 deletions src/TaskManager.nim
Original file line number Diff line number Diff line change
@@ -1,31 +1,26 @@
import gintro/[gtk4, gobject, gio, pango, adw, glib, gdk4]
import std/with
import Page
import Types
import AddNewPageRevealer
import Utils
import Save
import Load

proc addNewPage(btn: Button, tabView: TabView) =
let page = tabView.append newLabel($tabView.nPages)
page.title = $tabView.nPages
proc windowOnClose(self: adw.ApplicationWindow, tabView: TabView): bool =
save(tabView)
return gdk4.EVENT_PROPAGATE


proc activate(app: gtk4.Application) =
adw.init()
let loadedPages = readSaveFromFS().getPages()
let
window = adw.newApplicationWindow(app)
header = adw.newHeaderBar()
# addNewPageBtn = newButtonFromIconName("list-add-symbolic")
saveToJsonPageBtn = newFlatBtnWithIcon("document-save-symbolic")

mainBox = newBox(Orientation.vertical, 0)

tabBar = newTabBar()

tabView = newTabView()
# taskPage1 = createPage()

for loadedPage in loadedPages:
let loadedGroups = loadedPage.getGroupsFromPage()
Expand All @@ -34,28 +29,15 @@ proc activate(app: gtk4.Application) =
vexpand = true
hexpand = true
let page = tabView.append taskPage
page.title = loadedPage.pageName

page.title = loadedPage.pageName.cstring

# add to buttons to header
addRevealerWithEntryToHeaderBar(header, tabView)

header.packStart saveToJsonPageBtn

# save to JSON connecting





saveToJsonPageBtn.connect("clicked", save, tabView)



saveToJsonPageBtn.connect("clicked", saveBtnPressed, tabView)
tabBar.view = tabView
# addNewPageBtn.connect("clicked", addNewPage, tabView)
# window.connect("close_request", windowOnClose, page)

window.connect("close_request", windowOnClose, tabView)

with mainBox:
append header
Expand Down

0 comments on commit ecb4454

Please sign in to comment.