Created
July 25, 2017 06:22
-
-
Save roytornado/9be99575334061d2524f7806a877bd3d to your computer and use it in GitHub Desktop.
Flow Example
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
@IBAction func confirm() { | |
var dataBucket = [String: Any]() | |
if dataHolder.addedImages.count >= 1 { | |
dataBucket["images"] = dataHolder.addedImages | |
} | |
if let audioUrl = dataHolder.audioUrl { | |
dataBucket["targetAudioUrlForUpload"] = audioUrl | |
dataBucket["targetAudioDurationForUpload"] = "\(dataHolder.audioDuration)" | |
} | |
if DataUtils.isValidString(src: inputTextView.text) { | |
dataBucket["content"] = inputTextView.text | |
} | |
dataBucket["published"] = publishSwitch.isOn | |
let flow = Flow() | |
flow.setDataBucket(dataBucket: dataBucket) | |
flow.setWillStartBlock(block: { (flow) in | |
self.showLoading() | |
}) | |
if dataHolder.addedImages.count >= 1 { | |
flow.addGrouped(operationCreator: UploadImageOp.self, dispatcher: FlowDataArrayDispatcher(inputKey: "images", outputKey: "imageJsons")) | |
} | |
if let _ = dataHolder.audioUrl { | |
flow.add(operation: UploadAudioOp()) | |
} | |
flow.add(operation: UploadS5Op()) | |
flow.setDidFinishBlock(block: { (flow) in | |
self.hideLoading() | |
if flow.isSuccess { | |
self.performSegue(withIdentifier: "GoS5Finish", sender: self) | |
} else { | |
self.show(message: flow.failedInfo as! String) | |
} | |
}) | |
flow.start() | |
} | |
class UploadImageOp: FlowOperation, FlowOperationCreator { | |
static func create() -> FlowOperation { | |
return UploadImageOp() | |
} | |
override var primaryInputParamKey: String { return "targetImageForUpload" } | |
override var primaryOutputParamKey: String { return "uploadedImageJson" } | |
override func mainLogic() { | |
guard let image = getData(name: primaryInputParamKey) as? UIImage else { return } | |
NetworkManager.shared.uploadImage(URLString: Api.resourcesImages, image: image, imageParamName: "file", fileName: "image") { | |
networkResult in | |
if networkResult.isSuccess { | |
print("\(self.displayName) \(networkResult.asNSDictionary)") | |
self.setData(name: self.primaryOutputParamKey, value: networkResult.asNSDictionary) | |
} else { | |
self.isFinishedWithFailedOperation = true | |
self.failedInfo = networkResult.message | |
} | |
self.finishWithAsynchronous() | |
} | |
startWithAsynchronous() | |
} | |
} | |
class UploadAudioOp: FlowOperation { | |
override func mainLogic() { | |
guard let audioUrl = getData(name: "targetAudioUrlForUpload") as? URL else { return } | |
guard let audioDuration = getData(name: "targetAudioDurationForUpload") as? String else { return } | |
let path = Api.resourcesAudios + "?length=\(audioDuration)" | |
NetworkManager.shared.uploadAudio(URLString: path, audioUrl: audioUrl, audioParamName: "file", fileName: "audio") { | |
networkResult in | |
if networkResult.isSuccess { | |
print("\(self.displayName) \(networkResult.asNSDictionary)") | |
self.setData(name: "uploadedAudioJson", value: networkResult.asNSDictionary) | |
} else { | |
self.isFinishedWithFailedOperation = true | |
self.failedInfo = networkResult.message | |
} | |
self.finishWithAsynchronous() | |
} | |
startWithAsynchronous() | |
} | |
} | |
class UploadS5Op: FlowOperation { | |
override func mainLogic() { | |
var dict = [String: Any]() | |
var params = [String: Any]() | |
if let imageJsons = getData(name: "imageJsons", isOptional: true) as? [Any] { | |
dict["photo"] = imageJsons | |
} | |
if let audioJson = getData(name: "uploadedAudioJson", isOptional: true) { | |
dict["sound_record"] = audioJson | |
} | |
if let content = getData(name: "content", isOptional: true) as? String { | |
dict["content"] = content | |
} | |
if let published = getData(name: "published") as? Bool { | |
dict["published"] = published | |
} | |
params["step5"] = dict | |
NetworkManager.shared.put(Api.profileMe, parameters: params) { | |
result in | |
if result.isSuccess { | |
print("\(self.displayName) \(result.asNSDictionary)") | |
AppManager.shared.profile = Profile.parseOne(dict: result.asNSDictionary) | |
} else { | |
self.isFinishedWithFailedOperation = true | |
self.failedInfo = result.message | |
} | |
self.finishWithAsynchronous() | |
} | |
startWithAsynchronous() | |
} | |
} |
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
func confirm() { | |
if AccountManager.sharedInstance.currentAccount!.featuredPostIDs.contains(selected) { | |
showAlert(text: TXT_PROFILE_Feature_Post_Already_Added) | |
return | |
} | |
var dataBucket = [String: Any]() | |
dataBucket["postId"] = selected | |
dataBucket["postType"] = currentType | |
let flow = Flow() | |
flow.setDataBucket(dataBucket: dataBucket) | |
flow.setWillStartBlock() { flow in | |
self.showBlockLoading() | |
} | |
flow.add(operation: AddFeaturePostOp()) | |
flow.add(operation: LoadProfileOp()) | |
flow.setDidFinishBlock() { flow in | |
self.hideBlockLoading() | |
if flow.isSuccess { | |
self.back() | |
} else { | |
self.showAlert(text: flow.failedInfo as! String) | |
} | |
} | |
flow.start() | |
} | |
func upload() { | |
var dataBucket = [String: Any]() | |
dataBucket["images"] = Array(selected!) | |
let flow = Flow() | |
flow.setDataBucket(dataBucket: dataBucket) | |
flow.setWillStartBlock() { flow in | |
self.showBlockLoading() | |
} | |
flow.addGrouped(operationCreator: UploadImageAssetOp.self, dispatcher: FlowDataArrayDispatcher(inputKey: "images", outputKey: "imageJsons")) | |
flow.add(operation: UpdateProfileGalleryOp()) | |
flow.setDidFinishBlock() { flow in | |
self.hideBlockLoading() | |
if flow.isSuccess { | |
self.back() | |
} else { | |
self.showAlert(text: flow.failedInfo as! String) | |
} | |
} | |
flow.start() | |
} | |
func upload() { | |
var dataBucket = [String: Any]() | |
dataBucket["targetImageForUpload"] = Array(selected!).first! | |
let flow = Flow() | |
flow.setDataBucket(dataBucket: dataBucket) | |
flow.setWillStartBlock() { flow in | |
self.showBlockLoading() | |
} | |
flow.add(operation: UploadImageAssetOp()) | |
flow.add(operation: UpdateProfileImageOp()) | |
flow.setDidFinishBlock() { flow in | |
self.hideBlockLoading() | |
if flow.isSuccess { | |
self.back() | |
} else { | |
self.showAlert(text: flow.failedInfo as! String) | |
} | |
} | |
flow.start() | |
} | |
func loginFacebook(accessToken: AccessToken) { | |
showBlockLoading() | |
Flow() | |
.setDataBucket(dataBucket: [ | |
"accessToken": accessToken.authenticationToken | |
]) | |
.add(operation: LoginWithFacebookOp()) | |
.add(operation: LoadProfileOp()) | |
.add(operation: LoadGlobalDataOp()) | |
.setWillStartBlock { flow in | |
self.showBlockLoading() | |
} | |
.setDidFinishBlock { flow in | |
self.hideBlockLoading() | |
if flow.isSuccess { | |
self.checkAccount() | |
} else { | |
self.showAlert(text: flow.failedInfo as! String) | |
} | |
} | |
.start() | |
} | |
@IBAction func loginClicked(_ sender: AnyObject) { | |
guard let email = emailField.text, email.isValidEmail else { | |
showAlert(text: TXT_MSG_Email_Not_Valid) | |
return | |
} | |
guard let password = passwordField.text, password.isValidField else { | |
showAlert(text: TXT_MSG_Input_Password) | |
return | |
} | |
Flow() | |
.setDataBucket(dataBucket: [ | |
"email": email, | |
"password": password | |
]) | |
.add(operation: LoginWithEmailOp()) | |
.add(operation: LoadProfileOp()) | |
.add(operation: LoadGlobalDataOp()) | |
.setWillStartBlock { flow in | |
self.showBlockLoading() | |
} | |
.setDidFinishBlock { flow in | |
self.hideBlockLoading() | |
if flow.isSuccess { | |
self.checkAccount() | |
} else { | |
self.showAlert(text: flow.failedInfo as! String) | |
} | |
} | |
.start() | |
} |
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
func loadGroups() { | |
Flow() | |
.setDataBucket(dataBucket: ["childId": childId]) | |
.setDidFinishBlock(block: defaultFlowDidFinishBlock(successBlock: { (flow) in | |
if let groups = flow.dataBucket["childGroups"] as? [Group] { | |
self.groups = groups | |
} | |
self.fillCells() | |
})) | |
.add(operation: LoadChildGroupOp()) | |
.start() | |
} | |
func sendPaint(image: UIImage) { | |
Flow() | |
.setDataBucket(dataBucket: ["targetImageForUpload": image]) | |
.setWillStartBlock(block: defaultFlowWillStartBlock()) | |
.setDidFinishBlock(block: defaultFlowDidFinishBlock() { (flow) in | |
guard let chatroom = self.chatroom else { return } | |
guard let uploadedImageJson = flow.dataBucket["uploadedImageJson"] as? NSDictionary else { return } | |
guard let resourceId = uploadedImageJson.getString(fullPath: "resource_id") else { return } | |
let sigMessage = SIGMessage(senderId: AppDelegateInstance.currentChildren!.child_id, roomId: chatroom.chatroomId, type: .image, content: resourceId) | |
sigMessage.roomType = .group | |
print("sendPaint \(resourceId)") | |
AppDelegateInstance.chatManager.sig?.sendMessage(message: sigMessage) | |
}) | |
.add(operation: UploadPaintOp()) | |
.start() | |
} | |
func sendImage(image: UIImage) { | |
Flow() | |
.setDataBucket(dataBucket: ["targetImageForUpload": image]) | |
.setWillStartBlock(block: defaultFlowWillStartBlock()) | |
.setDidFinishBlock(block: defaultFlowDidFinishBlock() { (flow) in | |
guard let chatroom = self.chatroom else { return } | |
guard let uploadedImageJson = flow.dataBucket["uploadedImageJson"] as? NSDictionary else { return } | |
guard let resourceId = uploadedImageJson.getString(fullPath: "resource_id") else { return } | |
let sigMessage = SIGMessage(senderId: AppDelegateInstance.currentChildren!.child_id, roomId: chatroom.chatroomId, type: .image, content: resourceId) | |
sigMessage.roomType = .group | |
print("sendImage \(resourceId)") | |
AppDelegateInstance.chatManager.sig?.sendMessage(message: sigMessage) | |
}) | |
.add(operation: UploadImageOp()) | |
.start() | |
} | |
@IBAction func createGroup() { | |
guard let image = dataHolder.image else { showInfoRequiredAlert(); return } | |
guard let _ = dataHolder.name else { showInfoRequiredAlert(); return } | |
guard let _ = dataHolder.lang else { showInfoRequiredAlert(); return } | |
Flow() | |
.setDataBucket(dataBucket: ["targetImageForUpload": image, "targetGroupDataHolder": dataHolder]) | |
.setWillStartBlock(block: defaultFlowWillStartBlock()) | |
.setDidFinishBlock(block: defaultFlowDidFinishBlock() { (flow) in | |
AppDelegateInstance.chatManager.refreshGroupChatrooms() | |
self.showSuccessAlert() | |
}) | |
.add(operation: UploadImageOp()) | |
.add(operation: CreateGroupOp()) | |
.start() | |
} | |
func sendAudio(audioUrl: URL, duration: String) { | |
Flow() | |
.setDataBucket(dataBucket: ["targetAudioUrlForUpload": audioUrl, "targetAudioDurationForUpload": duration]) | |
.setWillStartBlock(block: defaultFlowWillStartBlock()) | |
.setDidFinishBlock(block: defaultFlowDidFinishBlock() { (flow) in | |
guard let chatroom = self.chatroom else { return } | |
guard let uploadedImageJson = flow.dataBucket["uploadedAudioJson"] as? NSDictionary else { return } | |
guard let resourceId = uploadedImageJson.getString(fullPath: "resource_id") else { return } | |
let sigMessage = SIGMessage(senderId: AppDelegateInstance.currentChildren!.child_id, roomId: chatroom.chatroomId, type: .audio, content: resourceId) | |
sigMessage.roomType = .comment | |
sigMessage.commentType = "video" | |
print("sendAudio \(resourceId)") | |
AppDelegateInstance.chatManager.sig?.sendMessage(message: sigMessage) | |
}) | |
.add(operation: UploadAudioOp()) | |
.start() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment