Created
December 8, 2020 13:53
-
-
Save lazyvar/9dcb57b8c678f3e0080f1252a6308823 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
import YPImagePicker | |
struct NewPost { | |
/// YPMediaItem translates to either a UIImage for photo or Data/URL for video | |
var media: Either<[YPMediaItem], [NewPost.Medium]> | |
struct Medium { | |
let url: String | |
let thumbnailUrl: String? | |
let mediumType: Post.Medium.MediumType | |
} | |
} | |
enum BackendAPI { | |
func post(_ newPost: NewPost) -> Observable<NetworkResult<Post>> { | |
let media: Observable<[NewPost.Medium]> = { | |
switch post.media { | |
case .left(let mediaItems): | |
return Observable<NewPost.Medium>.combineLatest(mediaItems.map { mediaItem -> Observable<NewPost.Medium> in | |
switch mediaItem { | |
case .photo(p: let photo): return StorageService.upload(photo) | |
case .video(v: let video): return StorageService.upload(video) | |
} | |
}) | |
case .right(let media): | |
return Observable.just(media) | |
} | |
}() | |
return media.flatMap { media in | |
return requestObject(.newPost(post, media: media)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment