Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix problem in reactive #2

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions Podfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
# Uncomment the next line to define a global platform for your project
platform :ios, '8.0'
platform :ios, '13.0'

target 'mvvm-ios' do
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!

# Pods for mvvm-ios
pod 'Alamofire', '~> 4.5'
pod 'SwiftyJSON'
pod 'RxSwift'
pod 'RxCocoa'
pod 'RealmSwift'
Expand All @@ -18,8 +16,6 @@ target 'mvvm-ios-tests' do
use_frameworks!

# Pods for mvvm-ios
pod 'Alamofire', '~> 4.5'
pod 'SwiftyJSON'
pod 'RxSwift'
pod 'RxCocoa'
pod 'RealmSwift'
Expand Down
36 changes: 0 additions & 36 deletions mvvm-ios-tests/JSONParserTests.swift

This file was deleted.

41 changes: 0 additions & 41 deletions mvvm-ios-tests/TestDataUtils.swift

This file was deleted.

169 changes: 102 additions & 67 deletions mvvm-ios.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

26 changes: 1 addition & 25 deletions mvvm-ios/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}

func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}

func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


}

34 changes: 34 additions & 0 deletions mvvm-ios/Endpoint.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// Endpoint.swift
// mvvm-ios
//
// Created by Behnam on 11/7/22.
// Copyright © 2022 Behnam. All rights reserved.
//

import Foundation

class Constants {
static let baseUrl: String = "https://api.github.com/"
}

enum Endpoint: RequestProtocol {

case searchFromRepos

var baseUrl: String { Constants.baseUrl }
var method: Method { .get }
func getBody() -> Data? { nil }

var path: String {
return "search/repositories"
}

var queryParameters: [URLQueryItem]? {
[.init(name: "q", value: "language:swift"),
.init(name: "sort", value: "star"),
.init(name: "order", value: "desc"),
.init(name: "per_page", value: "20")]
}

}
18 changes: 18 additions & 0 deletions mvvm-ios/Method.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// Method.swift
// mvvm-ios
//
// Created by Behnam on 11/7/22.
// Copyright © 2022 Behnam. All rights reserved.
//

import Foundation

enum Method: String {

case get = "GET"
case post = "POST"
case put = "PUT"
case delete = "DELETE"

}
30 changes: 30 additions & 0 deletions mvvm-ios/Repo.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// Repo.swift
// mvvm-ios
//
// Created by Quang Nguyen on 9/21/17.
// Copyright © 2017 Aromajoin. All rights reserved.
//

import Foundation
import RealmSwift

class RepositoryDBModel: Object {
@objc dynamic var id: Int32 = -1
@objc dynamic var name: String = ""

override func isEqual(_ object: Any?) -> Bool {
guard object is RepositoryDBModel else {
return false
}
let repo = object as! RepositoryDBModel

return repo.id == self.id && repo.name == self.name
}

func toRepository() -> Repository {
.init(id: self.id, name: self.name)
}
}


26 changes: 26 additions & 0 deletions mvvm-ios/Repository.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// Repository.swift
// mvvm-ios
//
// Created by Behnam on 11/7/22.
// Copyright © 2022 Behnam. All rights reserved.
//

import Foundation


struct Repositories: Codable {
let items: [Repository]
}

struct Repository: Codable {
let id: Int32
let name: String

func toRepositoryDBModel() -> RepositoryDBModel {
let model = RepositoryDBModel()
model.id = self.id
model.name = self.name
return model
}
}
45 changes: 45 additions & 0 deletions mvvm-ios/RequestProtocol.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// RequestProtocol.swift
// mvvm-ios
//
// Created by Behnam on 11/7/22.
// Copyright © 2022 Behnam. All rights reserved.
//

import Foundation

typealias Header = [String: String]

protocol RequestProtocol {
var baseUrl: String { get }
var path: String { get }
var method: Method { get }
var header: Header? { get }
var queryParameters: [URLQueryItem]? { get }

func getBody() -> Data?
func getRequest() -> URLRequest
}

extension RequestProtocol {
func getRequest() -> URLRequest {
var request = URLRequest(url: getURL()!)

request.httpMethod = method.rawValue
request.allHTTPHeaderFields = header
request.httpBody = getBody()

return request
}

func getURL() -> URL? {
var urlComponent = URLComponents(string: baseUrl)
urlComponent?.path += path
urlComponent?.queryItems = queryParameters
return urlComponent?.url
}

var header: Header? {
return nil
}
}
13 changes: 13 additions & 0 deletions mvvm-ios/Resource.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// Resource.swift
// mvvm-ios
//
// Created by Behnam on 11/7/22.
// Copyright © 2022 Aromajoin. All rights reserved.
//

import Foundation

struct Resource<T: Decodable> {
let endpoint: Endpoint
}
24 changes: 24 additions & 0 deletions mvvm-ios/WebService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// WebService.swift
// mvvm-ios
//
// Created by Behnam on 11/7/22.
// Copyright © 2022 Behnam. All rights reserved.
//

import Foundation
import RxSwift

class WebService {

func request<T: Codable>(resource: Resource<T>) -> Observable<T?> {
Observable.just(resource.endpoint.getURL()!)
.flatMap { url -> Observable<(response: HTTPURLResponse, data: Data)> in
let request = resource.endpoint.getRequest()
return URLSession.shared.rx.response(request: request)
}.map({ (response: HTTPURLResponse, data: Data) -> T? in
return try? JSONDecoder().decode(T.self, from: data)
}).asObservable()
}

}
Loading