Skip to content

Commit

Permalink
Merge pull request #1 from iqbalnurhaq/main
Browse files Browse the repository at this point in the history
Setup Core Module
  • Loading branch information
iqbalnurhaq authored Jan 11, 2023
2 parents 9963b0f + b30b131 commit 8cf8eec
Show file tree
Hide file tree
Showing 16 changed files with 329 additions and 1 deletion.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
xcuserdata/
DerivedData/
.swiftpm/config/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 CeXup

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
33 changes: 33 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// swift-tools-version: 5.7
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "Core",
platforms: [.iOS(.v15), .macOS(.v10_15)],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "Core",
targets: ["Core"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
.package(name: "Realm", url: "https://github.com/realm/realm-cocoa.git", from: "10.5.2")
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "Core",
dependencies: [
.product(name: "RealmSwift", package: "Realm"),
]
),
.testTarget(
name: "CoreTests",
dependencies: ["Core"]),
]
)
1 change: 0 additions & 1 deletion README.md

This file was deleted.

16 changes: 16 additions & 0 deletions Sources/Core/Data/DataSource.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// File.swift
//
//
// Created by Iqbal Nur Haq on 03/01/23.
//

import Foundation
import Combine

public protocol DataSource {
associatedtype Request
associatedtype Response

func execute(request: Request?) -> AnyPublisher<Response, Error>
}
20 changes: 20 additions & 0 deletions Sources/Core/Data/LocaleDataSource.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// File.swift
//
//
// Created by Iqbal Nur Haq on 03/01/23.
//

import Foundation
import Combine

public protocol LocaleDataSource {
associatedtype Request
associatedtype Response

func list(request: Request?) -> AnyPublisher<[Response], Error>
func add(entities: [Response]) -> AnyPublisher<Bool, Error>
func addOne(entity: Response) -> AnyPublisher<Bool, Error>
func get(id: String?) -> AnyPublisher<Response, Error>
func update(id: Int, entity: Response) -> AnyPublisher<Bool, Error>
}
16 changes: 16 additions & 0 deletions Sources/Core/Data/Repository/Repository.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// File.swift
//
//
// Created by Iqbal Nur Haq on 03/01/23.
//

import Foundation
import Combine

public protocol Repository {
associatedtype Request
associatedtype Response

func execute(request: Request?) -> AnyPublisher<Response, Error>
}
23 changes: 23 additions & 0 deletions Sources/Core/Domain/UseCase/Interactor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// File.swift
//
//
// Created by Iqbal Nur Haq on 03/01/23.
//

import Foundation
import Combine

public struct Interactor<Request, Response, R: Repository>: UseCase
where R.Request == Request, R.Response == Response {

private let _repository: R

public init(repository: R) {
_repository = repository
}

public func execute(request: Request?) -> AnyPublisher<Response, Error> {
_repository.execute(request: request)
}
}
16 changes: 16 additions & 0 deletions Sources/Core/Domain/UseCase/UseCase.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// File.swift
//
//
// Created by Iqbal Nur Haq on 03/01/23.
//

import Foundation
import Combine

public protocol UseCase {
associatedtype Request
associatedtype Response

func execute(request: Request?) -> AnyPublisher<Response, Error>
}
36 changes: 36 additions & 0 deletions Sources/Core/Extension/CustomError+Ext.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// File.swift
//
//
// Created by Iqbal Nur Haq on 03/01/23.
//

import Foundation

public enum URLError: LocalizedError {
case invalidRequest
case invalidResponse
case addressUnreachable(URL)
case custom(String)

public var errorDescription: String? {
switch self {
case .invalidRequest: return "Request is null."
case .invalidResponse: return "The server responded with garbage."
case .addressUnreachable(let url): return "\(url.absoluteString) is unreachable."
case .custom(let message): return "\(message)"
}
}
}

public enum DataBaseError: LocalizedError {
case invalidInstance
case requestFailed

public var errorDescription: String? {
switch self {
case .invalidInstance: return "Database can't instance."
case .requestFailed: return "Your request failed."
}
}
}
22 changes: 22 additions & 0 deletions Sources/Core/Extension/Result+Ext.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// File.swift
//
//
// Created by Iqbal Nur Haq on 03/01/23.
//

import Foundation
import RealmSwift

extension Results {
public func toArray<T>(ofType: T.Type) -> [T] {
var array = [T]()
for index in 0 ..< count {
if let result = self[index] as? T {
array.append(result)
}
}

return array
}
}
18 changes: 18 additions & 0 deletions Sources/Core/Mapper.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// File.swift
//
//
// Created by Iqbal Nur Haq on 04/01/23.
//

import Foundation

public protocol Mapper {
associatedtype Request
associatedtype Response
associatedtype Entity
associatedtype Domain

func transformResponseToEntity(request: Request?, response: Response) -> Entity
func transformEntityToDomain(entity: Entity) -> Domain
}
48 changes: 48 additions & 0 deletions Sources/Core/Presentation/Presenter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// File.swift
//
//
// Created by Iqbal Nur Haq on 05/01/23.
//

import Foundation
import SwiftUI
import Combine

public class Presenter<Request, Response, Interactor: UseCase>: ObservableObject
where
Interactor.Request == Request,
Interactor.Response == Response {

private var cancellables: Set<AnyCancellable> = []
private let _useCase: Interactor

@Published public var item: Response?
@Published public var errorMessage: String = ""
@Published public var isLoading: Bool = false
@Published public var isError: Bool = false

public init(useCase: Interactor){
_useCase = useCase
}

public func execute(request: Request?) {
isLoading = true
_useCase.execute(request: request)
.receive(on: RunLoop.main)
.sink(receiveCompletion: { completion in
switch completion {
case .failure(let error):
self.errorMessage = error.localizedDescription
self.isError = true
self.isLoading = false
case .finished:
self.isLoading = false
}
}, receiveValue: { item in
self.item = item
})
.store(in: &cancellables)
}

}
32 changes: 32 additions & 0 deletions Sources/Core/Utils/Prefs.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// File.swift
//
//
// Created by Iqbal Nur Haq on 11/01/23.
//

import Foundation

public class Prefs
{
private let defaults = UserDefaults.standard

private let keyAccessTokenPrefs = "tokenPrefs"

public var accessTokenPrefs: String {
set {
defaults.setValue(newValue, forKey: keyAccessTokenPrefs)
}
get {
return defaults.string(forKey: keyAccessTokenPrefs) ?? ""
}
}

public class var shared: Prefs {
struct Static {
static let instance = Prefs()
}

return Static.instance
}
}
11 changes: 11 additions & 0 deletions Tests/CoreTests/CoreTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import XCTest
@testable import Core

final class CoreTests: XCTestCase {
func testExample() throws {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct
// results.
XCTAssertEqual(Core().text, "Hello, World!")
}
}

0 comments on commit 8cf8eec

Please sign in to comment.