Last active
August 10, 2021 01:15
-
-
Save Neko3000/403c2cce95d035b269f0d9d4dff6db27 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ColorPalette.swift | |
// Monotone | |
// | |
// Created by Xueliang Chen on 2020/10/31. | |
// | |
import Foundation | |
import UIKit | |
import Hue | |
class ColorPalette{ | |
// MARK: - Static Methods | |
public static func colorWithDarkMode(darkColor:UIColor, lightColor:UIColor) -> UIColor{ | |
var color:UIColor? | |
if #available(iOS 13.0, *){ | |
color = UIColor.init(dynamicProvider: { (traitCollection:UITraitCollection) -> UIColor in | |
if(traitCollection.userInterfaceStyle == .dark){ | |
return darkColor | |
} | |
else{ | |
return lightColor | |
} | |
}) | |
} | |
else{ | |
color = lightColor | |
} | |
return color! | |
} | |
public static func colorWithDarkMode(darkColorHex:String, lightColorHex:String) -> UIColor{ | |
var color:UIColor? | |
if #available(iOS 13.0, *){ | |
color = UIColor.init(dynamicProvider: { (traitCollection:UITraitCollection) -> UIColor in | |
if(traitCollection.userInterfaceStyle == .dark){ | |
return UIColor(hex: darkColorHex) | |
} | |
else{ | |
return UIColor(hex: lightColorHex) | |
} | |
}) | |
} | |
else{ | |
color = UIColor(hex: lightColorHex) | |
} | |
return color! | |
} | |
public static func colorWithUserInterfaceStyle(color: UIColor, with userInterfaceStyle: UIUserInterfaceStyle) -> UIColor{ | |
let traitCollection: UITraitCollection = UITraitCollection.init(userInterfaceStyle: userInterfaceStyle) | |
return color.resolvedColor(with: traitCollection) | |
} | |
// MARK: - Color Varibles | |
public static var colorBlack:UIColor{ | |
return self.colorWithDarkMode(darkColorHex: "#ffffff", lightColorHex: "#000000") | |
} | |
public static var colorGrayLighter:UIColor{ | |
return self.colorWithDarkMode(darkColorHex: "#242424", lightColorHex: "#ededed") | |
} | |
public static var colorGrayLight:UIColor{ | |
return self.colorWithDarkMode(darkColorHex: "#4a4a4a", lightColorHex: "#9b9b9b") | |
} | |
public static var colorGrayNormal:UIColor{ | |
return self.colorWithDarkMode(darkColorHex: "#999999", lightColorHex: "#999999") | |
} | |
public static var colorGrayHeavy:UIColor{ | |
return self.colorWithDarkMode(darkColorHex: "#9b9b9b", lightColorHex: "#4a4a4a") | |
} | |
public static var colorGrayHeavier:UIColor{ | |
return self.colorWithDarkMode(darkColorHex: "#ededed", lightColorHex: "#242424") | |
} | |
public static var colorWhite:UIColor{ | |
return self.colorWithDarkMode(darkColorHex: "#000000", lightColorHex: "#ffffff") | |
} | |
public static var colorForeground:UIColor{ | |
return self.colorWithDarkMode(darkColorHex: "#101010", lightColorHex: "#ffffff") | |
} | |
public static var colorBackground:UIColor{ | |
return self.colorWithDarkMode(darkColorHex: "#000000", lightColorHex: "#f7f7f7") | |
} | |
public static var colorShadow:UIColor{ | |
return UIColor.black.alpha(0.09) | |
} | |
public static var colorOverlayer:UIColor{ | |
return self.colorWithDarkMode(darkColor: UIColor.black.alpha(0.6), lightColor: UIColor.black.alpha(0.4)) | |
} | |
public static var colorDenim:UIColor{ | |
return self.colorWithDarkMode(darkColorHex: "#3c5080", lightColorHex: "#3c5080") | |
} | |
public static var colorGreen:UIColor{ | |
return self.colorWithDarkMode(darkColorHex: "#2cb191", lightColorHex: "#2cb191") | |
} | |
public static var colorRed:UIColor{ | |
return self.colorWithDarkMode(darkColorHex: "#bb055a", lightColorHex: "#bb055a") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NetworkManager{ | |
// MARK: - Single Skeleton | |
public static let shared = NetworkManager() | |
init() { | |
// | |
} | |
let domain: String = "https://api.unsplash.com/" | |
// MARK: Headers | |
private var headers : HTTPHeaders{ | |
get{ | |
var tokenType: String = "" | |
var accessToken: String = "" | |
if(AuthManager.shared.credential != nil){ | |
tokenType = AuthManager.shared.credential!.tokenType | |
accessToken = AuthManager.shared.credential!.accessToken | |
} | |
else{ | |
tokenType = "Client-ID" | |
accessToken = AppManager.shared.credntial.accessKey | |
} | |
return [ | |
"Authorization" : "\(tokenType) \(accessToken)" | |
] | |
} | |
} | |
// MARK: Request. | |
public func request(request:BaseRequest, method:HTTPMethod) -> Observable<[String: Any]>{ | |
let endPoint = (request.api ?? "").addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) | |
let url = self.domain + (endPoint ?? "") | |
return Observable.create { (observer) -> Disposable in | |
let request = AF.request(url, method: method, parameters: request.toJSON(), headers: self.headers) | |
.response{ (response) in | |
switch(response.result){ | |
case .success(let data): | |
if(response.response?.statusCode == 200 || response.response?.statusCode == 201){ | |
do{ | |
var json = try JSON(data: data!) | |
// If the json is a root array without any keys, put the root array into result key. | |
if(json.arrayObject != nil){ | |
json = JSON(["results": json.arrayObject]) | |
} | |
observer.onNext(json.dictionaryObject ?? [String :Any]()) | |
observer.onCompleted() | |
} | |
catch{ | |
print("Could not decode success result from \(url), the error is \(error.localizedDescription)") | |
observer.onCompleted() | |
} | |
} | |
else{ | |
do{ | |
let json = try JSON(data: data!) | |
let error = NetworkError(errorStrs: json["errors"].arrayObject as! [String]) | |
MessageCenter.shared.showMessage(title: NSLocalizedString("uns_network_error_title", | |
comment: "Oops, there was a problem on network..."), | |
body: error.localizedDescription, | |
theme: .error) | |
observer.onError(error) | |
} | |
catch{ | |
print("Could not decode failure errors from \(url), the error is \(error.localizedDescription)") | |
observer.onError(error) | |
} | |
} | |
break | |
case .failure(let error): | |
print("\(error.localizedDescription)") | |
MessageCenter.shared.showMessage(title: NSLocalizedString("uns_network_error_title", | |
comment: "Oops, there was a problem of authentication..."), | |
body: error.localizedDescription, | |
theme: .error) | |
observer.onError(error) | |
break | |
} | |
} | |
return Disposables.create { | |
request.cancel() | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PhotoCreateCollectionViewModel: BaseViewModel, ViewModelStreamable{ | |
// MARK: - Input | |
struct Input { | |
var title: BehaviorRelay<String> = BehaviorRelay<String>(value: "") | |
var description: BehaviorRelay<String> = BehaviorRelay<String>(value: "") | |
var isPrivate: BehaviorRelay<Bool> = BehaviorRelay<Bool>(value: false) | |
var submitAction: Action<Void, Collection?>? | |
} | |
public var input: Input = Input() | |
// MARK: - Output | |
struct Output { | |
var collection: BehaviorRelay<Collection?> = BehaviorRelay<Collection?>(value: nil) | |
} | |
public var output: Output = Output() | |
// ... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PhotoListHeaderView: BaseView { | |
// MARK: - Public | |
public let searchQuery: BehaviorRelay<String?> = BehaviorRelay<String?>(value: nil) | |
public let listOrderBy: BehaviorRelay<ListOrderBy?> = BehaviorRelay<ListOrderBy?>(value: nil) | |
public let listOrderBys: BehaviorRelay<[ListOrderBy]> = BehaviorRelay<[ListOrderBy]>(value: ListOrderBy.allCases) | |
public let topic: BehaviorRelay<UnsplashTopic?> = BehaviorRelay<UnsplashTopic?>(value: nil) | |
public let topics: BehaviorRelay<[UnsplashTopic]> = BehaviorRelay<[UnsplashTopic]>(value: UnsplashTopic.allCases) | |
// MARK: - Controls | |
private var searchTextField: UITextField! | |
private var segmentedControl: HMSegmentedControl! | |
// MARK: - Private | |
private let disposeBag: DisposeBag = DisposeBag() | |
// .... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ListPhotosRequest: BaseRequest { | |
override var api: String?{ | |
get{ | |
return "photos" | |
} | |
} | |
public var page: Int? | |
public var perPage: Int? | |
public var orderBy: String? | |
override func mapping(map: Map) { | |
page <- map["page"] | |
perPage <- map["per_page"] | |
orderBy <- map["order_by"] | |
} | |
} | |
class ListPhotosResponse: BaseResponse{ | |
public var results: [Photo]? | |
override func mapping(map: Map) { | |
results <- map["results"] | |
} | |
} | |
public func listPhotos(page:Int? = 1, | |
perPage:Int? = 10, | |
orderBy:String? = "latest") -> Observable<[Photo]>{ | |
let request: ListPhotosRequest = ListPhotosRequest() | |
request.page = page | |
request.orderBy = orderBy | |
request.perPage = perPage | |
return Observable.create { (observer) -> Disposable in | |
NetworkManager.shared.request(request: request, method: .get).subscribe { (json) in | |
let response = ListPhotosResponse(JSON: json) | |
let photos = response!.results! | |
observer.onNext(photos) | |
observer.onCompleted() | |
} onError: { (error) in | |
observer.onError(error) | |
}.disposed(by: self.disposeBag) | |
return Disposables.create() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//... | |
case .license: | |
return (key:"license", | |
title:NSLocalizedString("uns_agreement_license_title", comment: "License"), | |
content: Bundle.main.url(forResource: "license_" + (Locale.current.languageCode == "zh" ? "cn" : "en"), | |
withExtension: "html")) | |
// ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment