Skip to content

Commit

Permalink
第一可用版本
Browse files Browse the repository at this point in the history
  • Loading branch information
mmoaay committed Jan 5, 2017
1 parent 516678a commit 998e182
Show file tree
Hide file tree
Showing 15 changed files with 573 additions and 519 deletions.
Binary file removed Demo/mbnetwork_download.gif
Binary file not shown.
4 changes: 2 additions & 2 deletions Example/MBNetwork/Form/BaseForm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ extension MBFormable {
}
}

extension MBFormable where Self : MBUploadFormable {
public func headers() -> [String : String] {
extension MBFormable where Self: MBUploadFormable {
public func headers() -> [String: String] {
return ["accessToken":"xxx", "fileName":"xxx"];
}
}
2 changes: 1 addition & 1 deletion Example/MBNetwork/Form/WeatherForm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct WeatherForm: MBRequestFormable {
var city = "shanghai"

func parameters() -> [String: Any] {
return ["city":city]
return ["city": city]
}

var url = "https://raw.githubusercontent.com/tristanhimmelman/AlamofireObjectMapper/2ee8f34d21e8febfdefb2b3a403f18a43818d70a/sample_keypath_json"
Expand Down
90 changes: 55 additions & 35 deletions Example/MBNetwork/Main.storyboard

Large diffs are not rendered by default.

17 changes: 8 additions & 9 deletions Example/MBNetwork/ViewController/DownloadViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,33 @@

import UIKit
import MBNetwork
import SnapKit
import Alamofire

class DownloadViewController: UIViewController, MBRequestable {

@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var progress: UIProgressView!

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
}

@IBAction func download(_ sender: Any) {
let load = LoadConfig(container: imageView, mask:MBEyeLoading())

self.download(ImageDownloadForm()).responseData { (response:DownloadResponse<Data>) in
self.download(ImageDownloadForm()).load(load:load).progress(progress: progress).responseData { (response:DownloadResponse<Data>) in
switch response.result {
case .success(let data):
let image = UIImage(data: data)
self.imageView.snp.updateConstraints({ (make:ConstraintMaker) in
make.width.equalTo(self.imageView.snp.height).multipliedBy(image!.size.width/image!.size.height)
})
self.imageView.image = image
self.imageView.image = UIImage(data: data)
break
case .failure:
break
}
}

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
Expand Down
4 changes: 2 additions & 2 deletions Example/MBNetwork/ViewController/LoadableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ class LoadableViewController: UIViewController, MBRequestable {

@IBAction func loadCustom(_ sender: AnyObject) {
let load = LoadConfig(container: view, mask:MBEyeLoading(), inset: UIEdgeInsetsMake(30+64, 15, UIScreen.main.bounds.height-64-(44*4+30+15*3), 15))
request(WeatherForm()).load(load:load)
request(WeatherForm()).load(load: load)
}

@IBAction func loadViewController(_ sender: AnyObject) {
request(WeatherForm()).load(load:MBLoadType.default(container: view))
request(WeatherForm()).load(load: MBLoadType.default(container: view))
}

@IBAction func loadNavigationController(_ sender: AnyObject) {
Expand Down
3 changes: 2 additions & 1 deletion Example/MBNetwork/ViewController/UploadViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import AlamofireObjectMapper
import Alamofire

class UploadViewController: UIViewController, MBRequestable {
@IBOutlet var button: UIButton!

override func viewDidLoad() {
super.viewDidLoad()
Expand All @@ -37,7 +38,7 @@ class UploadViewController: UIViewController, MBRequestable {

@IBAction func uploadPressed(_ sender: Any) {
let completion:((UploadRequest) -> Void)? = { upload in
upload.load(load: MBLoadType.default(container: self)).inform(error: BaseError(), inform: MBMessageType.alertController(title: "Notice", message: "Upload successfully", actions: [UIAlertAction(title: "Ok", style: UIAlertActionStyle.cancel, handler: nil)], container: self))
upload.load(load: self.button).inform(error: BaseError(), inform: MBMessageType.alertController(title: "Notice", message: "Upload successfully", actions: [UIAlertAction(title: "Ok", style: UIAlertActionStyle.cancel, handler: nil)], container: self))
}
self.upload(UploadMultiFormDataForm(), completion: completion)
}
Expand Down
4 changes: 2 additions & 2 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ DEPENDENCIES:

EXTERNAL SOURCES:
MBNetwork:
:path: "../"
:path: ../

SPEC CHECKSUMS:
Alamofire: aa2e09d871c9160ac53c90e83c68064a94e3dfbe
Expand All @@ -29,4 +29,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: 28d0da2a1d92f2769ac2994e7b5809a9bd041454

COCOAPODS: 1.2.0.beta.1
COCOAPODS: 1.2.0.beta.3
4 changes: 2 additions & 2 deletions Example/Pods/Manifest.lock

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

901 changes: 460 additions & 441 deletions Example/Pods/Pods.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.

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

2 changes: 1 addition & 1 deletion MBNetwork.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'MBNetwork'
s.version = '0.1.0'
s.version = '0.2.0'
s.summary = 'MBNetwork - Network request can be easier'

# This description is used to generate tags and improve search results.
Expand Down
24 changes: 16 additions & 8 deletions MBNetwork/Classes/Response/MBResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public extension DataRequest {
if let err = response.error {
warn.show(error: err.localizedDescription)
}
}).responseObject(queue: nil, keyPath: nil, mapToObject: nil, context: nil, completionHandler: { (response:DataResponse<T>) in
}).responseObject(queue: nil, keyPath: nil, mapToObject: nil, context: nil) { (response:DataResponse<T>) in
if let err = response.result.value {
if let code = err.code {
if true == error.successCodes.contains(code) {
Expand All @@ -36,7 +36,7 @@ public extension DataRequest {
}
}
}
})
}
}


Expand All @@ -49,25 +49,25 @@ public extension DataRequest {
@discardableResult
func inform<T: MBJSONErrorable>(error: T, inform: MBInformable = MBMessageType.none) -> Self {

return responseObject(queue: nil, keyPath: nil, mapToObject: nil, context: nil, completionHandler: { (response:DataResponse<T>) in
return responseObject(queue: nil, keyPath: nil, mapToObject: nil, context: nil) { (response:DataResponse<T>) in
if let err = response.result.value {
if let code = err.code {
if true == error.successCodes.contains(code) {
inform.show()
}
}
}
})
}
}
}

public extension DataRequest {
@discardableResult
func load(load: MBLoadable = MBLoadType.none) -> Self {
load.begin()
return response(completionHandler: { (response:DefaultDataResponse) in
return response { (response: DefaultDataResponse) in
load.end()
})
}
}
}

Expand All @@ -79,13 +79,21 @@ public extension DownloadRequest {
progress.progress(prog)
})
}

@discardableResult
func load(load: MBLoadable = MBLoadType.none) -> Self {
load.begin()
return response { (response: DefaultDownloadResponse) in
load.end()
}
}
}

public extension UploadRequest {
@discardableResult
func progress(progress: MBProgressable) -> Self {
return uploadProgress(closure: { (prog: Progress) in
return uploadProgress { (prog: Progress) in
progress.progress(prog)
})
}
}
}
33 changes: 20 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@
- `MBMessageable`: Message protocol.
- `MBWarnable`: Warn protocol. Conforming to this protocol to customize the way of warning messages displayed when error occured.
- `MBInformable`: Inform protocol. Conforming to this protocol to customize the way of inform messages displayed when request done successfully
- `MBErrorable`: Error protocol. Conforming to this protocol to customize the error config info.
- `MBJSONErrorable`: Error protocol for the server. Conforming to this protocol to customize the error config info from the server.
- `MBErrorable`: Error protocol. Conforming to this protocol to customize the error configuration.
- `MBJSONErrorable`: Error protocol for JSON data. Conforming to this protocol to customize the error configuration for JSON data.

Mostly you don't need to care much about these protocols, because we already have many **DEFAULT** implementations for them. However if you want to customize something, you just need to conform to these protocols and do what you want. Here is some default implementations for there protcols:
Mostly you don't need to care much about these protocols, because we already have many **DEFAULT** implementations for them. However if you want to customize something, you just need to conform to these protocols and do what you want. Here is some default implementations for these protcols:

- `MBLoadType`: Enum that conforms to `MBLoadable` protocol, using `case default(container:MBContainable)` case to show `MBMaskView` on the container when requesting.
- `MBMessageType`: Enum that conforms to `MBMessageable` protocol, using `alertController(title: String, message: String? , actions: [UIAlertAction], container: MBContainable)` case to show alertController.
Expand All @@ -56,7 +56,7 @@ Mostly you don't need to care much about these protocols, because we already hav
- `UITableViewCell+MBContainable`: Extending UITableViewCell to conform to `MBContainable` protocol.
- `UIViewController+MBContainable`: Extending UIViewController to conform to `MBContainable` protocol.
- `MBActivityIndicator`: Default mask for UITableViewCell and UIButton
- `MBMaskView`: Default mask for other.
- `MBMaskView`: Default mask for others.

## Features

Expand Down Expand Up @@ -90,9 +90,9 @@ And you can also have extension for specified protocol

``` swift

extension MBFormable where Self : MBUploadFormable {
public func headers() -> [String : String] {
return ["accessToken":"xxx", "fileName":"xxx"];
extension MBFormable where Self: MBUploadFormable {
public func headers() -> [String: String] {
return ["accessToken":"xxx", "file":"xxx"];
}
}
```
Expand All @@ -105,7 +105,7 @@ struct WeatherForm: MBRequestFormable {
var city = "shanghai"

public func parameters() -> [String: Any] {
return ["city":city]
return ["city": city]
}

var url = "https://raw.githubusercontent.com/tristanhimmelman/AlamofireObjectMapper/2ee8f34d21e8febfdefb2b3a403f18a43818d70a/sample_keypath_json"
Expand Down Expand Up @@ -151,7 +151,7 @@ func load(load: MBLoadable = MBLoadType.none) -> Self
![](https://github.com/mmoaay/MBNetwork/blob/master/Demo/mbnetwork_loadable_uiviewcontroller.gif)

``` swift
request(WeatherForm()).load(load:MBLoadType.default(container: view))
request(WeatherForm()).load(load: MBLoadType.default(container: view))
```

#### Show mask on UINavigationController
Expand Down Expand Up @@ -215,10 +215,10 @@ Then we can use it as followed:

``` swift
let load = LoadConfig(container: view, mask:MBEyeLoading(), inset: UIEdgeInsetsMake(30+64, 15, UIScreen.main.bounds.height-64-(44*4+30+15*3), 15))
request(WeatherForm()).load(load:load)
request(WeatherForm()).load(load: load)
```

This is the most powful usage of the `MBLoadable` protocol. In this way you can customized everything the `MBLoadable` protocol has.
This is the most powerful usage of the `MBLoadable` protocol. In this way you can customized everything the `MBLoadable` protocol has.

#### Show mask on UITableView & UIScrollView

Expand Down Expand Up @@ -275,6 +275,7 @@ And then we can use it as followed:
request(WeatherForm()).warn(error: WeatherError(), warn: MBMessageType.alertController(title: "Warning", message: "Network unavailable", actions: [UIAlertAction(title: "Ok", style: UIAlertActionStyle.cancel, handler: nil)], container: self))
```

> **Notice**: We only have `warn` for JSON format response now.
### Show inform message if success

Expand All @@ -292,6 +293,8 @@ And then we can use it as followed:
request(WeatherForm()).inform(error: WeatherInformError(), inform: MBMessageType.alertController(title: "Notice", message: "Load successfully", actions: [UIAlertAction(title: "Ok", style: UIAlertActionStyle.cancel, handler: nil)], container: self))
```

> **Notice**: We only have `inform ` for JSON format response now.
### JSON to Object

```swift
Expand All @@ -312,7 +315,9 @@ All the method mentioned above can be called in a chained manner, such as follow

```swift
let load = LoadConfig(container: view, mask:MBEyeLoading(), inset: UIEdgeInsetsMake(30+64, 15, UIScreen.main.bounds.height-64-(44*4+30+15*3), 15))
request(WeatherForm()).load(load:load).progress(progress: progress).warn(error: WeatherError(), warn: MBMessageType.alertController(title: "Warning", message: "Network unavailable", actions: [UIAlertAction(title: "Ok", style: UIAlertActionStyle.cancel, handler: nil)], container: self)).inform(error: WeatherInformError(), inform: MBMessageType.alertController(title: "Notice", message: "Load successfully", actions: [UIAlertAction(title: "Ok", style: UIAlertActionStyle.cancel, handler: nil)], container: self))
let warn = MBMessageType.alertController(title: "Warning", message: "Network unavailable", actions: [UIAlertAction(title: "Ok", style: UIAlertActionStyle.cancel, handler: nil)], container: self)
let inform = MBMessageType.alertController(title: "Notice", message: "Load successfully", actions: [UIAlertAction(title: "Ok", style: UIAlertActionStyle.cancel, handler: nil)], container: self)
request(WeatherForm()).load(load:load).progress(progress: progress).warn(error: WeatherError(), warn: warn).inform(error: WeatherInformError(), inform: inform)
```

## Bonus
Expand All @@ -321,7 +326,9 @@ request(WeatherForm()).load(load:load).progress(progress: progress).warn(error:

![](https://github.com/mmoaay/MBNetwork/blob/master/Demo/MBEyeLoading.gif)

You can check the file `MBEyeloading` in example project.
We've written this motion effect when implementing the customized loading, and it's all implementing with `CAAnimationGroup`.

If interested, you can check the file `MBEyeloading` in example project.

## Example

Expand Down

0 comments on commit 998e182

Please sign in to comment.