Skip to content

Commit

Permalink
Feature/merge vault to db (#100)
Browse files Browse the repository at this point in the history
* Create vault db

* Implement vault db queries

* Add Vault data source methods

* Adding a vault file to database

* Working on merging the vault files to database

* Working on merging the vault files to database

* Working on merging the vault files to database

* Working on merging the vault files to database

* Merge root file to database

* Refactor and clean code

* Refactor and clean code

* Merge root file to database

* Merge root file to database

* Merge root file to database

* Handle tella file types

* Uncomment code

* Refactor unlock action

* Update Tella file type function

* Fix typo

* Divide add files result functions

* Separate adding file completion to separate functions

* Remove unused code

* Add throws when opening the database

* Remove the single quotes around (tableName)

* Use Swift's native error handling instead of catching errors

* Move the rootId to the Vault constant file

* Update VaultFilesManager initialisation

* Update VaultFilesManager initialisation

* Refactor select query

* Use TimeInterval  instead of Double
  • Loading branch information
dhekra-rouatbi authored Dec 4, 2023
1 parent eaf3ddc commit a0bc5d9
Show file tree
Hide file tree
Showing 128 changed files with 3,301 additions and 2,262 deletions.
96 changes: 78 additions & 18 deletions Tella.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Tella/Application/AppViewState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ final class AppViewState: ObservableObject {
}

func resetToUnlock() {
homeViewModel.resetVaultManager()
homeViewModel.resetData()
viewStack = [.UNLOCK]
}

Expand All @@ -61,7 +61,7 @@ final class AppViewState: ObservableObject {
}

func resetApp() {
homeViewModel.keysInitialized() ? self.resetToUnlock() : self.resetToLock()
homeViewModel.vaultManager.keysInitialized() ? self.resetToUnlock() : self.resetToLock()
}

func initLanguage() {
Expand Down
2 changes: 1 addition & 1 deletion Tella/Application/TellaApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct TellaApp: App {

var body: some Scene {
WindowGroup {
ContentView(mainAppModel: appViewState.homeViewModel)
ContentView(mainAppModel: appViewState.homeViewModel, appViewState: appViewState)
.environmentObject(appViewState)
.onReceive(NotificationCenter.default.publisher(for: UIScreen.capturedDidChangeNotification)) { value in
appViewState.homeViewModel.shouldShowRecordingSecurityScreen = UIScreen.main.isCaptured
Expand Down
22 changes: 5 additions & 17 deletions Tella/Components/ImagePickerSheet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ struct ImagePickerCompletion {
case image
}
let type: MediaType
var image: UIImage? = nil
var videoURL: URL? = nil
var pathExtension: String? = nil
var referenceURL: URL? = nil
var imageURL: URL? = nil
var mediaURL: URL? = nil

}

// SwiftUI wrapper for ImagePickerController for <= iOS 14.0
Expand Down Expand Up @@ -59,21 +57,11 @@ class ImageCoordinator: NSObject, UINavigationControllerDelegate, UIImagePickerC
let mediaURL = info[UIImagePickerController.InfoKey.mediaURL] as? URL
let referenceURL = info[UIImagePickerController.InfoKey.referenceURL] as? URL
let imageURL = info[UIImagePickerController.InfoKey.imageURL] as? URL

if mediaType as! CFString == kUTTypeImage {
guard let unwrapImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else {
return
}
//completion(unwrapImage, nil,mediaURL?.pathExtension, referenceURL, imageURL)
completion(ImagePickerCompletion(type: .image,image: unwrapImage,
pathExtension: mediaURL?.pathExtension,
referenceURL: referenceURL,
imageURL: imageURL))
completion(ImagePickerCompletion(type: .image, referenceURL: referenceURL, mediaURL: imageURL))
} else if mediaType as! CFString == kUTTypeMovie {
guard let videoURL = info[UIImagePickerController.InfoKey.mediaURL] as? URL else {
return
}
completion(ImagePickerCompletion(type: .video, videoURL: videoURL, referenceURL: referenceURL))
//completion(nil, videoURL, nil, nil, nil)
completion(ImagePickerCompletion(type: .video, referenceURL: referenceURL, mediaURL: mediaURL))
}
}

Expand Down
17 changes: 5 additions & 12 deletions Tella/Components/ImportFilesProgressView/CancelImportView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import SwiftUI

struct CancelImportView: View {

@ObservedObject var mainAppModel: MainAppModel
@EnvironmentObject var mainAppModel: MainAppModel

var importFilesProgressProtocol : ImportFilesProgressProtocol

Expand All @@ -18,25 +18,18 @@ struct CancelImportView: View {
msgText: importFilesProgressProtocol.cancelMessage,
cancelText: importFilesProgressProtocol.exitCancelImportButtonTitle,
actionText: importFilesProgressProtocol.cancelImportButtonTitle) {
mainAppModel.vaultManager.progress.resume()
mainAppModel.cancelImportAndEncryption()
mainAppModel.vaultFilesManager?.cancelImportAndEncryption()
sheetManager.hide()

DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: {
mainAppModel.vaultManager.progress.stop()
})


} didCancelAction: {
mainAppModel.vaultManager.progress.resume()
sheetManager.hide()
}
}
}
}

//
struct CancelImportView_Previews: PreviewProvider {
static var previews: some View {
CancelImportView( mainAppModel: MainAppModel.stub(),
importFilesProgressProtocol: ImportFilesProgress())
CancelImportView( importFilesProgressProtocol: ImportFilesProgress())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ struct ImportFilesProgressView: View {

@EnvironmentObject var mainAppModel : MainAppModel
@EnvironmentObject var sheetManager: SheetManager
@ObservedObject var progress : ProgressFile

var importFilesProgressProtocol : ImportFilesProgressProtocol
var modalHeight : CGFloat = 215
Expand All @@ -33,12 +34,12 @@ struct ImportFilesProgressView: View {
.frame(height: 10)

if importFilesProgressProtocol.progressType == .number {
Text(String.init(format:importFilesProgressProtocol.progressMessage , mainAppModel.vaultManager.progress.progressFile.value))
Text(String.init(format:importFilesProgressProtocol.progressMessage , progress.progressFile))
.font(.custom(Styles.Fonts.regularFontName, size: 14))
.foregroundColor(.white)

} else {
Text(String.init(format:importFilesProgressProtocol.progressMessage + " " , Int(mainAppModel.vaultManager.progress.progress.value * 100)))
Text(String.init(format:importFilesProgressProtocol.progressMessage + " " , Int(progress.progress * 100)))

.font(.custom(Styles.Fonts.regularFontName, size: 14))
.foregroundColor(.white)
Expand All @@ -47,15 +48,14 @@ struct ImportFilesProgressView: View {
Spacer()
.frame(height: 8)

ProgressView("", value: mainAppModel.vaultManager.progress.progress.value, total: 1)
ProgressView("", value: progress.progress, total: 1)
.accentColor(.green)

Spacer()

HStack {
Spacer()
Button(importFilesProgressProtocol.cancelImportButtonTitle) {
mainAppModel.vaultManager.progress.pause()
showCancelImportView()
}
.foregroundColor(Color.white)
Expand All @@ -64,12 +64,9 @@ struct ImportFilesProgressView: View {
}
}
.padding(EdgeInsets(top: 25, leading: 25, bottom: 35, trailing: 25))
.onReceive(mainAppModel.vaultManager.progress.isFinishing) { isFinishing in
.onReceive( progress.$isFinishing) { isFinishing in
if isFinishing {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) {
sheetManager.hide()
// mainAppModel.vaultManager.clearTmpDirectory()
}
sheetManager.hide()
}
}
}
Expand All @@ -78,14 +75,15 @@ struct ImportFilesProgressView: View {
sheetManager.showBottomSheet( modalHeight: 152,
shouldHideOnTap: false,
content: {
CancelImportView(mainAppModel: mainAppModel,
importFilesProgressProtocol: importFilesProgressProtocol)
CancelImportView(importFilesProgressProtocol: importFilesProgressProtocol)
})
}
}

struct ImportFilesProgressView_Previews: PreviewProvider {
static var previews: some View {
ImportFilesProgressView(importFilesProgressProtocol: ImportFilesProgress())
ImportFilesProgressView(progress: ProgressFile(),
importFilesProgressProtocol: ImportFilesProgress())
.background(Styles.Colors.backgroundMain)
}
}
6 changes: 3 additions & 3 deletions Tella/Components/Tabs/PageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ class PageViewItem {

var title : String
var page : Pages
var number : String
var number : Int

init(title: String, page: Pages, number: String) {
init(title: String, page: Pages, number: Int) {
self.title = title
self.page = page
self.number = number
Expand All @@ -20,7 +20,7 @@ class PageViewItem {
public struct PageView: View {

@Binding var selectedOption: Pages
@Binding var pageViewItems : [PageViewItem]
var pageViewItems : [PageViewItem]

public var body: some View {
HStack(spacing: 20) {
Expand Down
8 changes: 4 additions & 4 deletions Tella/Components/Tabs/PageViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import SwiftUI
struct PageViewCell: View {

let title: String
let number: String
let number: Int

let page: Pages

Expand All @@ -21,13 +21,13 @@ struct PageViewCell: View {

let selected: Bool = page == selectedOption
VStack {
HStack {
HStack(spacing: 3) {
Text(title)
.font(.custom(Styles.Fonts.boldFontName, size: 15))
.foregroundColor(selected ? .white : .white.opacity(0.5))
.padding(.bottom, 1)
if !number.isEmpty {
Text(number)
if number > 0 {
Text("(\(number))")
.font(.custom(Styles.Fonts.boldFontName, size: 15))
.foregroundColor(Styles.Colors.yellow)
}
Expand Down
4 changes: 2 additions & 2 deletions Tella/Components/VaultFileImages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import SwiftUI

extension VaultFile {
extension VaultFileDB {
var gridImage: AnyView {
AnyView(
GeometryReader { geometryReader in
Expand All @@ -26,7 +26,7 @@ extension VaultFile {
}
}

extension VaultFile {
extension VaultFileDB {
var listImage: AnyView {
AnyView(
ZStack{
Expand Down
38 changes: 0 additions & 38 deletions Tella/Data/AuthenticationManager.swift

This file was deleted.

11 changes: 7 additions & 4 deletions Tella/Data/Database/Common/DataBaseHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,28 @@ class DataBaseHelper {

var dbPointer: OpaquePointer?

func openDatabases(key: String?) {
init(key: String?, databaseName: String) throws {

dbURL = FileManager.documentDirectory(withPath: D.databaseName)
dbURL = FileManager.documentDirectory(withPath:databaseName)

guard let key = key else { return }

if sqlite3_open(dbURL?.path, &dbPointer) != SQLITE_OK {
debugLog("Error opening database at \(dbURL?.absoluteString ?? "")!")
logDbErr("Error opening database")
logDbErr()
throw RuntimeError("Error opening database")

} else {
debugLog("Opening database at \(dbURL?.absoluteString ?? "")")
}

if (sqlite3_key(dbPointer, key, Int32(key.count)) != SQLITE_OK) {
logDbErr("Error setting key")
throw RuntimeError("Error setting key")
}
}

func logDbErr(_ msg: String) {
func logDbErr(_ msg: String = "") {
let errmsg = String(cString: sqlite3_errmsg(dbPointer)!)
debugLog("\(msg): \(errmsg)")
}
Expand Down
2 changes: 1 addition & 1 deletion Tella/Data/Database/Common/DatabaseConstants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ struct D {
/* DATABASE */
static let databaseName = "tella_vault.db"

/* DATABASE VERSIO */
/* DATABASE VERSION */

static let databaseVersion = 1

Expand Down
14 changes: 11 additions & 3 deletions Tella/Data/Database/Common/DatabaseUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import Foundation
import SQLite3

extension TellaDataBase {
extension DataBase {

func cddl(_ columnName: String, _ columnType: String, primaryKey : Bool , autoIncrement : Bool) -> String {
return columnName + " " + columnType + (primaryKey ? " PRIMARY KEY " : "") + (autoIncrement ? " AUTOINCREMENT " : "");
Expand Down Expand Up @@ -47,6 +47,14 @@ extension TellaDataBase {
func comma() -> String {
return " , ";
}



func parseDicToObjectOf<T:Codable>(type: T.Type, dic: Any) throws -> T {
let data = try JSONSerialization.data(withJSONObject: dic)
let decodedValues = try JSONDecoder().decode(T.self, from: data)
return decodedValues
}


}


24 changes: 24 additions & 0 deletions Tella/Data/Database/Common/KeyValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,34 @@ import Foundation
struct KeyValue {
var key : String
var value : Any?
var sqliteOperator : SQliteOperator

init(key: String, value: Any? = nil, sqliteOperator: SQliteOperator = .empty) {
self.key = key
self.value = value
self.sqliteOperator = sqliteOperator
}

}


struct KeyValues {
var key : String
var value : [Any]

var sqliteOperator : SQliteOperator


init(key: String, value: [Any], sqliteOperator: SQliteOperator = .empty) {
self.key = key
self.value = value
self.sqliteOperator = sqliteOperator
}
}

enum SQliteOperator: String {
case empty = " "
case and = " AND "
case or = " OR "
}

Loading

0 comments on commit a0bc5d9

Please sign in to comment.