Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
MQZHot committed Oct 11, 2017
0 parents commit 1f60742
Show file tree
Hide file tree
Showing 98 changed files with 13,899 additions and 0 deletions.
426 changes: 426 additions & 0 deletions DaisyNet.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?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>SchemeUserState</key>
<dict>
<key>DaisyNet.xcscheme</key>
<dict>
<key>orderHint</key>
<integer>4</integer>
</dict>
</dict>
</dict>
</plist>
10 changes: 10 additions & 0 deletions DaisyNet.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<Bucket
type = "0"
version = "2.0">
</Bucket>
46 changes: 46 additions & 0 deletions DaisyNet/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// AppDelegate.swift
// DaisyNet
//
// Created by mengqingzheng on 2017/10/10.
// Copyright © 2017年 MQZHot. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: 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:.
}


}

93 changes: 93 additions & 0 deletions DaisyNet/Assets.xcassets/AppIcon.appiconset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
{
"images" : [
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "29x29",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "29x29",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "40x40",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "40x40",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "60x60",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "60x60",
"scale" : "3x"
},
{
"idiom" : "ipad",
"size" : "20x20",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "20x20",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "29x29",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "29x29",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "40x40",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "40x40",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "76x76",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "76x76",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "83.5x83.5",
"scale" : "2x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
31 changes: 31 additions & 0 deletions DaisyNet/Base.lproj/LaunchScreen.storyboard
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13196" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="01J-lp-oVM">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13173"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="EHf-IW-A2E">
<objects>
<viewController id="01J-lp-oVM" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="L0C-Ao-enU"/>
<viewControllerLayoutGuide type="bottom" id="0oz-Kd-xxg"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="Ze5-6b-2t3">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="iYj-Kq-Ea1" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="53" y="375"/>
</scene>
</scenes>
</document>
30 changes: 30 additions & 0 deletions DaisyNet/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13196" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13173"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="tne-QT-ifu">
<objects>
<viewController id="BYZ-38-t0r" customClass="ViewController" customModule="DaisyNet" customModuleProvider="target" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="fQV-9x-kZW"/>
<viewControllerLayoutGuide type="bottom" id="H5g-yi-Nr7"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
</objects>
</scene>
</scenes>
</document>
142 changes: 142 additions & 0 deletions DaisyNet/DaisyNet/DaisyNet.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
//
// DaisyNet.swift
// ZoneHot
//
// Created by mengqingzheng on 2017/10/10.
// Copyright © 2017年 MQZHot. All rights reserved.
//

import UIKit
import Alamofire
import Cache

class DaisyNet {

private var storage: Storage?

init() {
let diskConfig = DiskConfig(name: "ZCache")
let memoryConfig = MemoryConfig(expiry: .never)
do {
storage = try Storage(diskConfig: diskConfig, memoryConfig: memoryConfig)
} catch {
DaisyLog(error)
}
}

open func request(_ url: String,
method: HTTPMethod = .get,
params: Dictionary<String, Any>?,
cache: Bool = false,
completion: @escaping (Alamofire.Result<Any>)->()) {
let cacheKey = self.cacheKey(url: url, params: params)
if cache {
self.object(forKey: cacheKey) { cacheJson in
completion(Alamofire.Result.success(cacheJson))
}
}

Alamofire.request(url, method: method, parameters: params).responseJSON { response in
switch response.result {
case .success(let value):
if cache {
self.setObject(response.data, forKey: cacheKey)
}
completion(Alamofire.Result.success(value))
case .failure(let error):
completion(Alamofire.Result.failure(error))
}
}
}

/// 清除所有缓存
///
/// - Parameter completion: 完成闭包
open func removeAllCache(completion: @escaping (Bool)->()) {
storage?.async.removeAll(completion: { result in
switch result {
case .value: completion(true)
case .error: completion(false)
}
})
}

/// 根据key值清除缓存
///
/// - Parameters:
/// - forKey: key
/// - completion: 完成回调
func removeObjectCache(forKey: String, completion: @escaping (Bool)->()) {
storage?.async.removeObject(forKey: forKey, completion: { result in
switch result {
case .value: completion(true)
case .error: completion(false)
}
})
}

/// 读取缓存
private func object(forKey key: String, completion: @escaping (Any)->()) {
storage?.async.object(ofType: Data.self, forKey: key, completion: { result in
switch result {
case .value(let data):
if let json = try? JSONSerialization.jsonObject(with: data, options: []) {
completion(json)
}
case .error(let error):
DaisyLog(error)
}
})
}

/// 异步存储
private func setObject<T: Codable>(_ object: T, forKey: String) {
storage?.async.setObject(object, forKey: forKey, completion: { result in
switch result {
case .value: DaisyLog("saved successfully")
case .error(let error): DaisyLog(error)
}
})
}

/// 将参数字典转换成字符串
private func cacheKey(url: String, params: Dictionary<String, Any>?) -> String {
guard let params = params,
let stringData = try? JSONSerialization.data(withJSONObject: params, options: []),
let paramString = String(data: stringData, encoding: String.Encoding.utf8) else {
return url
}
let str = "\(url)" + "\(paramString)"
return str
}
}

public enum Effect<Value> {
case success(Value)
case failure(Error)

public var value: Value? {
switch self {
case .success(let value):
return value
case .failure:
return nil
}
}
public var error: Error? {
switch self {
case .success:
return nil
case .failure(let error):
return error
}
}
}

// MARK: - log日志
func DaisyLog<T>( _ message: T, file: String = #file, method: String = #function, line: Int = #line){
#if DEBUG
print("\((file as NSString).lastPathComponent)[\(line)], \(method): \(message)")
#endif
}

Loading

0 comments on commit 1f60742

Please sign in to comment.