Skip to content

Commit

Permalink
lots of small clean up, use 1 minute chunks, improve purging, some mo…
Browse files Browse the repository at this point in the history
…re protection
  • Loading branch information
jasonjmcghee committed Dec 28, 2023
1 parent 7814d2b commit 413fee6
Show file tree
Hide file tree
Showing 6 changed files with 279 additions and 138 deletions.
8 changes: 4 additions & 4 deletions rem.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -765,8 +765,8 @@
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
ASSETCATALOG_COMPILER_INCLUDE_ALL_APPICON_ASSETS = YES;
CODE_SIGN_ENTITLEMENTS = rem/rem.entitlements;
CODE_SIGN_IDENTITY = "";
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "";
CODE_SIGN_IDENTITY = "Apple Development";
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
CODE_SIGN_STYLE = Manual;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 1;
Expand Down Expand Up @@ -802,8 +802,8 @@
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
ASSETCATALOG_COMPILER_INCLUDE_ALL_APPICON_ASSETS = YES;
CODE_SIGN_ENTITLEMENTS = rem/rem.entitlements;
CODE_SIGN_IDENTITY = "";
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "";
CODE_SIGN_IDENTITY = "Apple Development";
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
CODE_SIGN_STYLE = Manual;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 1;
Expand Down
79 changes: 57 additions & 22 deletions rem/DB.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,16 @@ class DatabaseManager {
lastFrameId = getLastFrameId()
}

private func connect() {
if let savedir = RemFileManager.shared.getSaveDir() {
db = try! Connection("\(savedir)/db.sqlite3")
} else {
db = try! Connection("db.sqlite3")
func purge() {
do {
try db.run(self.videoChunks.drop(ifExists: true))
try db.run(self.frames.drop(ifExists: true))
try db.run(self.allText.drop(ifExists: true))
} catch {
print("Failed to delete tables")
}
}

func reconnect() {
self.connect()

createTables()
currentChunkId = getCurrentChunkId()
lastFrameId = getLastFrameId()
}
Expand Down Expand Up @@ -132,35 +132,55 @@ class DatabaseManager {
}

func getFrame(forIndex index: Int64) -> (offsetIndex: Int64, filePath: String)? {
let query = frames.join(videoChunks, on: chunkId == videoChunks[id]).filter(frames[id] == index).limit(1)
if let frame = try! db.pluck(query) {
return (frame[self.offsetIndex], frame[self.filePath])
do {
let query = frames.join(videoChunks, on: chunkId == videoChunks[id]).filter(frames[id] == index).limit(1)
if let frame = try db.pluck(query) {
return (frame[self.offsetIndex], frame[self.filePath])
}

// let justFrameQuery = frames.filter(frames[id] === index).limit(1)
// try! db.run(justFrameQuery.delete())
} catch {
return nil
}

return nil
}

// Function to retrieve the file path of a video chunk by its index
func getVideoChunkPath(byIndex index: Int64) -> String? {
let query = videoChunks.filter(chunkId == index)
if let chunk = try! db.pluck(query) {
return chunk[filePath]
do {
let query = videoChunks.filter(chunkId == index)
if let chunk = try db.pluck(query) {
return chunk[filePath]
}
} catch {
return nil
}
return nil
}

// Function to get the timestamp of the last inserted frame
private func getLastFrameTimestamp() -> Date? {
let query = frames.select(timestamp).order(id.desc).limit(1)
if let lastFrame = try! db.pluck(query) {
return lastFrame[timestamp]
do {
let query = frames.select(timestamp).order(id.desc).limit(1)
if let lastFrame = try db.pluck(query) {
return lastFrame[timestamp]
}
} catch {
return nil
}
return nil
}

private func getLastFrameIndexFromDB() -> Int64? {
let query = frames.select(id).order(id.desc).limit(1)
if let lastFrame = try! db.pluck(query) {
return lastFrame[id]
do {
let query = frames.select(id).order(id.desc).limit(1)
if let lastFrame = try db.pluck(query) {
return lastFrame[id]
}
} catch {
return nil
}
return nil
}
Expand All @@ -183,6 +203,18 @@ class DatabaseManager {
return lastFrameId
}

func getLastAccessibleFrame() -> Int64 {
do {
let query = frames.join(videoChunks, on: chunkId == videoChunks[id]).select(frames[id]).order(frames[id].desc).limit(1)
if let lastFrame = try db.pluck(query) {
return lastFrame[id]
}
} catch {
return 0
}
return 0
}

func search(searchText: String, limit: Int = 9) -> [(frameId: Int64, fullText: String, applicationName: String?, timestamp: Date)] {
let query = allText
.join(frames, on: frames[id] == allText[frameId])
Expand Down Expand Up @@ -230,6 +262,9 @@ class DatabaseManager {
guard let frameData = DatabaseManager.shared.getFrame(forIndex: index) else { return nil }

let videoURL = URL(fileURLWithPath: frameData.filePath)
if !FileManager.default.fileExists(atPath: videoURL.path) {
return nil
}

return extractFrame(from: videoURL, frameOffset: frameData.offsetIndex)
}
Expand All @@ -246,7 +281,7 @@ class DatabaseManager {
let aI = try generator.copyCGImage(at: a, actualTime: nil)
return aI
} catch {
print("Error extracting frame: \(error)")
print("Error extracting frame \(videoURL): \(error)")
return nil
}
}
Expand Down
16 changes: 13 additions & 3 deletions rem/Search.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,17 @@ struct SearchBar: View {
.padding(.leading, 12)
}
)
.onSubmit(onSearch) // Trigger search when user submits
.onSubmit {
Task {
onSearch()
}
} // Trigger search when user submits
.onChange(of: text) {
debounceTimer?.invalidate()
debounceTimer = Timer.scheduledTimer(withTimeInterval: 0.3, repeats: false) { _ in
onSearch()
Task {
onSearch()
}
}
} // Trigger search when text changes
.onAppear {
Expand Down Expand Up @@ -247,7 +253,11 @@ struct ResultsView: View {
.padding()
}
}
.onAppear(perform: loadRecentResults)
.onAppear {
Task {
loadRecentResults()
}
}
}

private func performSearch() {
Expand Down
8 changes: 1 addition & 7 deletions rem/TextMerger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,13 @@ class TextMerger {
var linesSeen = Set<String>()
for text in texts {
let cleanedText = cleanText(text)
if mergedText.isEmpty {
mergedText = cleanedText
continue
}
for line in self.segmentText(text) {
for line in self.segmentText(cleanedText) {
if !linesSeen.contains(line) {
mergedText += "\(line)\n"
linesSeen.insert(line)
}
}
// mergedText = mergeTwoTexts(text1: mergedText, text2: cleanedText)
}
// return compressDocument(mergedText)
return mergedText
}

Expand Down
Loading

0 comments on commit 413fee6

Please sign in to comment.