Skip to content

Commit

Permalink
violate
Browse files Browse the repository at this point in the history
  • Loading branch information
Zafer Çalışkan committed Feb 28, 2024
1 parent 5fcf1d8 commit 9a9b92c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 25 deletions.
8 changes: 6 additions & 2 deletions SOLID-Practice/Managers/CoreDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@

import UIKit

enum CoreDataError: Error {
case fetchError
}

final class CoreDataManager {


}

extension CoreDataManager: RepositoryProtocol {
func getJokes() -> [JokeModel] {
func getJokes() throws -> [JokeModel] {
do {
guard let context = (UIApplication.shared.delegate as? AppDelegate)?.persistentContainer.viewContext else { return []}

Expand All @@ -29,7 +33,7 @@ extension CoreDataManager: RepositoryProtocol {

return jokeList
}catch {
return []
throw CoreDataError.fetchError
}
}

Expand Down
33 changes: 20 additions & 13 deletions SOLID-Practice/Managers/SqliteManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import Foundation
import SQLite3

enum SqliteError: Error {
case fetchError
}

final class SqliteManager {

Expand Down Expand Up @@ -70,23 +73,27 @@ extension SqliteManager: RepositoryProtocol {
}
}

func getJokes() -> [JokeModel] {
func getJokes() throws -> [JokeModel] {
var mainList = [JokeModel]()

let query = "SELECT * FROM jokes;"
var statement : OpaquePointer? = nil
if sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK{
while sqlite3_step(statement) == SQLITE_ROW {
let id = String(describing: String(cString: sqlite3_column_text(statement, 0)))
let value = String(describing: String(cString: sqlite3_column_text(statement, 1)))

let model = JokeModel(id: id, value: value)


mainList.append(model)
do {
let query = "SELECT * FROM jokes;"
var statement : OpaquePointer? = nil
if sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK{
while sqlite3_step(statement) == SQLITE_ROW {
let id = String(describing: String(cString: sqlite3_column_text(statement, 0)))
let value = String(describing: String(cString: sqlite3_column_text(statement, 1)))

let model = JokeModel(id: id, value: value)


mainList.append(model)
}
}
return mainList
}catch {
throw SqliteError.fetchError
}
return mainList
}

func getJoke(id: String) -> JokeModel? {
Expand Down
2 changes: 1 addition & 1 deletion SOLID-Practice/Protocols/RepositoryProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation

protocol RepositoryProtocol {
func getJokes() -> [JokeModel]
func getJokes() throws -> [JokeModel]
func getJoke(id: String) -> JokeModel?
func getJokeEntity(id: String) -> [JokeEntity]?
func saveJoke(item: JokeModel)
Expand Down
21 changes: 12 additions & 9 deletions SOLID-Practice/Screens/Home/ViewModel/HomeViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,18 @@ final class HomeViewModel {

func getFavoriteJoke() {
// let jokeList = coreDataManager.getJokes()
let jokeList = sqliteManager.getJokes()

jokes.removeAll(where: { cell in
cell == .favorite()
})

jokes.append(.favorite(jokeList))

delegate?.reloadTableView()
do {
let jokeList = try sqliteManager.getJokes()
jokes.removeAll(where: { cell in
cell == .favorite()
})

jokes.append(.favorite(jokeList))

delegate?.reloadTableView()
}catch {
print(error.localizedDescription)
}
}

func getJoke(id: String) -> JokeModel? {
Expand Down

0 comments on commit 9a9b92c

Please sign in to comment.