Skip to content

Commit 56beb84

Browse files
committed
[feat]: basic data setup
1 parent f008fbc commit 56beb84

File tree

3 files changed

+70
-13
lines changed

3 files changed

+70
-13
lines changed

timer/Sources/timer/Entry.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ import Foundation
1010

1111
/// Structure for practice exercise
1212
struct Entry: Codable {
13-
/// Identifier
14-
var id: String?
13+
/// Name of exercise
14+
var name: String
1515

1616
/// Subitems list
1717
var items: [Entry]?
1818

19-
/// Name of exercise
20-
var name: String?
19+
/// This is final folder for exercises, the subfolders won't be processed
20+
var isFinal: Bool
2121

2222
/// PDF file to open
2323
var pdf: String?

timer/Sources/timer/Exercises.swift

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77

88
import Foundation
99
import Files
10+
import ScriptToolkit
1011

1112
class Exercises {
12-
lazy var mainEntry: Entry = { Entry() }()
13+
lazy var mainEntry: Entry = { Entry(name: "Root", isFinal: false) }()
14+
15+
// MARK: - Init entries
1316

1417
func initialize() throws {
1518
let interestFolder = try Folder(path: Constants.mainPath).subfolder(named: interest)
@@ -21,13 +24,17 @@ class Exercises {
2124
let exerciseFilePath = folder.path.appendingPathComponent(path: "exercise.json")
2225

2326
for subfolder in folder.subfolders {
27+
if subfolder.name == "Ignore" {
28+
continue
29+
}
30+
2431
try initEntries(folder: subfolder)
2532
}
2633

2734
let entry = Entry(
28-
id: folder.path(relativeTo: Constants.mainFolder),
29-
items: nil,
3035
name: folder.name,
36+
items: nil,
37+
isFinal: false,
3138
totalTime: nil,
3239
lastPracticeDate: nil,
3340
rating: nil,
@@ -40,12 +47,12 @@ class Exercises {
4047

4148
let jsonData = try! encoder.encode(entry)
4249
let jsonString = String(data: jsonData, encoding: .utf8)!
43-
print(jsonString)
4450

4551
let exerciseFileURL = URL(fileURLWithPath: exerciseFilePath)
4652
try jsonString.write(to: exerciseFileURL, atomically: true, encoding: .utf8)
4753
}
4854

55+
// MARK: - Open entries
4956

5057
func openEntries() throws {
5158
let interestFolder = try Folder(path: Constants.mainPath).subfolder(named: interest)
@@ -56,6 +63,10 @@ class Exercises {
5663
@discardableResult func openEntries(folder: Folder) throws -> Entry {
5764
let exerciseFilePath = folder.path.appendingPathComponent(path: "exercise.json")
5865

66+
guard FileManager.default.locationExists(path: exerciseFilePath, kind: .file) else {
67+
throw ScriptError.fileNotFound(message: exerciseFilePath)
68+
}
69+
5970
let jsonData = try Data(contentsOf: URL(fileURLWithPath: exerciseFilePath))
6071
var entry = try JSONDecoder().decode(Entry.self, from: jsonData)
6172

@@ -66,15 +77,59 @@ class Exercises {
6677

6778
var entries = [Entry]()
6879

69-
for subfolder in folder.subfolders {
70-
entries.append(try openEntries(folder: subfolder))
80+
if !entry.isFinal {
81+
for subfolder in folder.subfolders {
82+
if subfolder.name == "Ignore" {
83+
continue
84+
}
85+
86+
entries.append(try openEntries(folder: subfolder))
87+
}
88+
89+
entry.items = entries
7190
}
72-
73-
entry.items = entries
74-
91+
7592
return entry
7693
}
7794

95+
// MARK: - Save entries
96+
97+
func saveEntries() throws {
98+
let interestFolder = try Folder(path: Constants.mainPath).subfolder(named: interest)
99+
100+
try saveEntries(folder: interestFolder, entry: mainEntry)
101+
}
102+
103+
func saveEntries(folder: Folder, entry: Entry) throws {
104+
let exerciseFilePath = folder.path.appendingPathComponent(path: "exercise.json")
105+
106+
if let unwrappedItems = entry.items {
107+
for item in unwrappedItems {
108+
if item.name == "Ignore" {
109+
continue
110+
}
111+
112+
try saveEntries(folder: folder.subfolder(named: item.name), entry: item)
113+
}
114+
}
115+
116+
var mutableEntry = entry
117+
if !mutableEntry.isFinal {
118+
mutableEntry.items = nil
119+
}
120+
121+
let encoder = JSONEncoder()
122+
encoder.outputFormatting = .prettyPrinted
123+
124+
let jsonData = try! encoder.encode(mutableEntry)
125+
let jsonString = String(data: jsonData, encoding: .utf8)!
126+
127+
let exerciseFileURL = URL(fileURLWithPath: exerciseFilePath)
128+
try jsonString.write(to: exerciseFileURL, atomically: true, encoding: .utf8)
129+
}
130+
}
131+
132+
extension Exercises {
78133
func openPDF(file: String, page: Int?) {
79134
let scriptSource: String
80135

timer/Sources/timer/main.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ do {
3737

3838
try exercises.openEntries()
3939

40+
try exercises.saveEntries()
41+
4042
// // Prepare plan
4143
// try scheduler.preparePlan()
4244
// scheduler.presentPlan()

0 commit comments

Comments
 (0)