Skip to content

Commit

Permalink
Working on SQLite local database
Browse files Browse the repository at this point in the history
  • Loading branch information
dhekra-rouatbi committed Sep 23, 2022
1 parent ed219b5 commit 354cbbb
Show file tree
Hide file tree
Showing 21 changed files with 879 additions and 79 deletions.
2 changes: 2 additions & 0 deletions .zshrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export PATH=/opt/homebrew/bin:$PATH

2 changes: 1 addition & 1 deletion Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ target 'Tella' do
use_modular_headers!

# Pods for Tella
pod 'SQLCipher', ">= 4.5.2"

pod 'CryptoSwift', '~> 1.0'

target 'TellaTests' do
inherit! :search_paths
Expand Down
144 changes: 96 additions & 48 deletions Tella.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions Tella/Scenes/ContentView/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ struct ContentView: View {
.environmentObject(LockViewModel(unlockType: .new))
.eraseToAnyView()
}
}.onAppear {
setDebugLevel(level: .debug, for: .files)
setDebugLevel(level: .debug, for: .app)
setDebugLevel(level: .debug, for: .crypto)

}
.environmentObject(DeviceOrientationHelper())

Expand Down
2 changes: 0 additions & 2 deletions Tella/Scenes/MainView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ struct MainView: View {


init() {
setDebugLevel(level: .debug, for: .app)
setDebugLevel(level: .debug, for: .crypto)
setupApperance()
}

Expand Down
52 changes: 34 additions & 18 deletions Tella/Scenes/Settings/ViewModel/ServersViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,24 @@ class ServersViewModel: ObservableObject {
@Published var shouldShowError : Bool = true

@Published var errorMessage : String = ""




@Published var username : String = ""
@Published var password : String = ""

@Published var validUsername : Bool = false
@Published var validPassword : Bool = false

@Published var shouldShowLoginError : Bool = true

@Published var loginErrorMessage : String = ""

@Published var rootLinkIsActive : Bool = false


@Published var servers : [Server]?

func checkURL() { // To test

shouldShowError = serverURL != "https://"
Expand All @@ -44,24 +46,38 @@ class ServersViewModel: ObservableObject {
validURL = true
}
}

func login() { // To test

// shouldShowLoginError = (username != "dhekra" && password != "password")
//
// if username != "dhekra" && password != "password" {
// loginErrorMessage = "Error: The server URL is incorrect"
// validUsername = false
// validPassword = false
// } else {
// loginErrorMessage = ""
// validUsername = true
// validPassword = true
// }

// shouldShowLoginError = (username != "dhekra" && password != "password")
//
// if username != "dhekra" && password != "password" {
// loginErrorMessage = "Error: The server URL is incorrect"
// validUsername = false
// validPassword = false
// } else {
// loginErrorMessage = ""
// validUsername = true
// validPassword = true
// }
}

init(mainAppModel : MainAppModel) {
self.mainAppModel = mainAppModel
servers = mainAppModel.vaultManager.tellaData.getServers()
}

func addServer() {

let serverToAdd = Server(name: "Name", url: serverURL, username: username, password: password)

do {
let id = try mainAppModel.vaultManager.tellaData.addServer(server: Server(name: "Name", url: serverURL, username: username, password: password))
serverToAdd.id = id
self.servers?.append(serverToAdd)
} catch {

}
}
}
2 changes: 2 additions & 0 deletions Tella/Scenes/Settings/Views/Servers/ServerLoginView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ struct ServerLoginView: View {
nextButtonAction: .action) {
UIApplication.shared.endEditing()
serversViewModel.login()
serversViewModel.addServer()

showNextView = !serversViewModel.shouldShowLoginError
}

Expand Down
17 changes: 14 additions & 3 deletions Tella/Scenes/Settings/Views/Servers/ServersListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ struct ServersListView: View {

ContainerView {
VStack(spacing: 0) {

Spacer()
.frame(height: 8)

SettingsCardView (cardViewArray: [SettingsAddServerCardView().environmentObject(serversViewModel).eraseToAnyView(),
SettingsServerItemView(title: "CLEEN Foundation",action: showServerActionBottomSheet).eraseToAnyView(),
SettingsServerItemView(title: "Election monitoring").eraseToAnyView()])
SettingsCardView<AnyView> (cardViewArray: serversView())

Spacer()
}
}
Expand All @@ -35,6 +35,17 @@ struct ServersListView: View {
}
}

func serversView<T>() -> [T] {

var arrayView : [T] = [SettingsAddServerCardView().environmentObject(serversViewModel).eraseToAnyView() as! T]

serversViewModel.servers?.forEach({ server in
arrayView.append(SettingsServerItemView(title: server.username,action: showServerActionBottomSheet).eraseToAnyView() as! T)

})
return arrayView
}

func showServerActionBottomSheet() {
sheetManager.showBottomSheet(modalHeight: 176) {
ActionListBottomSheet(items: serverActionItems,
Expand Down
14 changes: 13 additions & 1 deletion Tella/Utils/Extensions/FileManagerExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,23 @@
import Foundation

extension FileManager {

func sizeOfFile(atPath path: String) -> Int64? {
guard let attrs = try? attributesOfItem(atPath: path) else {
return nil
}
return attrs[.size] as? Int64
}

class func documentDirectory(withPath path: String) -> URL? {
do {
return try FileManager.default
.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
.appendingPathComponent(path)
}
catch {
return nil
}
}

}
14 changes: 13 additions & 1 deletion Tella/VaultManager/CryptoManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class CryptoManager {
return "\(Self.metaPrivateKeyTagPrefix).\(keyID)"
}

private var metaPrivateKey: SecKey?
var metaPrivateKey: SecKey?

@RawValueUserDefaultsProperty("PasswordType", defaultValue: PasswordTypeEnum.tellaPassword)
var passwordType: PasswordTypeEnum
Expand Down Expand Up @@ -326,3 +326,15 @@ extension CryptoManager: CryptoManagerInterface {
}

}


extension SecKey {
func getString() -> String? {
var error:Unmanaged<CFError>?
if let cfdata = SecKeyCopyExternalRepresentation(self, &error) {
let data:Data = cfdata as Data
return data.base64EncodedString()
}
return nil
}
}
46 changes: 46 additions & 0 deletions Tella/VaultManager/DatabaseConstants.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Tella
//
// Copyright © 2022 INTERNEWS. All rights reserved.
//

import Foundation
import SQLite3

struct D {
/* DATABASE */
static let databaseName = "tella_vault.db"

/* DEFAULT TYPES FOR DATABASE */
static let integer = " INTEGER "
static let text = " TEXT ";
static let blob = " BLOB ";


/* DATABASE TABLES */

static let tServer = "t_server"
static let tReport = "t_report"
static let tReportFiles = "t_report_files"

/* DATABASE COLUMNS */

static let cId = "c_id"
static let cName = "c_name"
static let cURL = "c_url"
static let cUsername = "c_username"
static let cPassword = "c_password"


static let cTitle = "c_title"
static let cDescription = "c_description"
static let cDate = "c_date"
static let cStatus = "c_Status"
static let cServerId = "c_server_id"

static let cReportId = "c_report_id"
static let cVaultFileId = "c_vaultFile_id"


}


54 changes: 54 additions & 0 deletions Tella/VaultManager/DatabaseUtilities.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Tella
//
// Copyright © 2022 INTERNEWS. All rights reserved.
//

import Foundation
import SQLite3

extension TellaDataBase {



func cddl(_ columnName: String, _ columnType: String, primaryKey : Bool , autoIncrement : Bool) -> String {
return columnName + " " + columnType + (primaryKey ? " PRIMARY KEY " : "") + (autoIncrement ? " AUTOINCREMENT " : "");
}

func cddl(_ columnName: String, _ columnType: String) -> String {
return columnName + " " + columnType;
}

func cddl(_ columnName: String, _ columnType: String, _ notNull: Bool) -> String {
return (columnName) + " " + columnType + (notNull ? " NOT NULL" : "")
}

func cddl(_ columnName: String, _ columnType: String, _ notNull: Bool, _ defaultValue: Int) -> String {
return columnName + " " + columnType + (notNull ? " NOT NULL " : "") + "DEFAULT " + "\(defaultValue)";
}


func cddl(_ columnName: String, _ columnType: String, tableName : String , referenceKey : String) -> String {
return columnName + " " + columnType + ", FOREIGN KEY" + " (" + columnName + ") " + " REFERENCES " + tableName + "(" + referenceKey + ")";
}


func sq(_ text: String) -> String {
return " " + text + " ";
}

func objQuote(_ str: String) -> String {
return "'" + str + "'";
}

func objDoubleQuote(_ str: String) -> String {
return "\"" + str + "\"";
}



func comma() -> String {
return " , ";
}


}
20 changes: 20 additions & 0 deletions Tella/VaultManager/JoinCondition.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Tella
//
// Copyright © 2022 INTERNEWS. All rights reserved.
//

import Foundation


class JoinCondition {

var tableName :String
var firstItem : JoinItem
var secondItem : JoinItem

init(tableName: String, firstItem: JoinItem, secondItem: JoinItem) {
self.tableName = tableName
self.firstItem = firstItem
self.secondItem = secondItem
}
}
12 changes: 12 additions & 0 deletions Tella/VaultManager/KeyValue.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Tella
//
// Copyright © 2022 INTERNEWS. All rights reserved.
//

import Foundation


struct KeyValue {
var key : String
var value : Any
}
28 changes: 28 additions & 0 deletions Tella/VaultManager/Models/JoinItem.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Tella
//
// Copyright © 2022 INTERNEWS. All rights reserved.
//

import Foundation


class JoinItem {

var tableName: String
var columnName : String

init(tableName: String, columnName: String) {
self.tableName = tableName
self.columnName = columnName
}

func format() -> String {
return (objDoubleQuote(tableName) + "." + objDoubleQuote(columnName))
}

func objDoubleQuote(_ str: String) -> String {
return "\"" + str + "\"";
}


}
Loading

0 comments on commit 354cbbb

Please sign in to comment.