Added FRPPhotoViewModel.

This commit is contained in:
Ash Furrow
2013-10-22 19:07:21 -04:00
parent ed5c5c3d83
commit cd238a498c
5 changed files with 110 additions and 25 deletions

View File

@@ -8,13 +8,14 @@
#import <UIKit/UIKit.h>
@class FRPPhotoModel;
@class FRPPhotoViewModel;
@interface FRPPhotoViewController : UIViewController
-(instancetype)initWithPhotoModel:(FRPPhotoModel *)photoModel index:(NSInteger)photoIndex;
-(instancetype)initWithViewModel:(FRPPhotoViewModel *)viewModel index:(NSInteger)photoIndex;
@property (nonatomic, readonly) NSInteger photoIndex;
@property (nonatomic, readonly) FRPPhotoModel *photoModel;
@property (nonatomic, readonly) FRPPhotoViewModel *viewModel;
@end

View File

@@ -10,6 +10,7 @@
// Model
#import "FRPPhotoModel.h"
#import "FRPPhotoViewModel.h"
// Utilities
#import "FRPPhotoImporter.h"
@@ -19,7 +20,7 @@
// Private assignment
@property (nonatomic, assign) NSInteger photoIndex;
@property (nonatomic, strong) FRPPhotoModel *photoModel;
@property (nonatomic, strong) FRPPhotoViewModel *viewModel;
// Private properties
@property (nonatomic, weak) UIImageView *imageView;
@@ -28,12 +29,12 @@
@implementation FRPPhotoViewController
-(instancetype)initWithPhotoModel:(FRPPhotoModel *)photoModel index:(NSInteger)photoIndex
-(instancetype)initWithViewModel:(FRPPhotoViewModel *)viewModel index:(NSInteger)photoIndex
{
self = [self init];
if (!self) return nil;
self.photoModel = photoModel;
self.viewModel = viewModel;
self.photoIndex = photoIndex;
return self;
@@ -48,27 +49,26 @@
// Configure subviews
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
RAC(imageView, image) = [RACObserve(self.photoModel, fullsizedData) map:^id(id value) {
return [UIImage imageWithData:value];
}];
RAC(imageView, image) = self.viewModel.photoImageSignal;
imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:imageView];
self.imageView = imageView;
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (!self.presentedViewController) {
[SVProgressHUD show];
// Fetch data
[[FRPPhotoImporter fetchPhotoDetails:self.photoModel] subscribeError:^(NSError *error) {
[SVProgressHUD showErrorWithStatus:@"Error"];
} completed:^{
[SVProgressHUD dismiss];
}];
}
@weakify(self);
[[self rac_signalForSelector:@selector(viewDidAppear:)] subscribeNext:^(id x) {
@strongify(self);
if (!self.presentedViewController) {
[SVProgressHUD show];
[self.viewModel.viewDidAppearCommand execute:nil];
}
}];
[self.viewModel.viewDidAppearCommand.executionSignals subscribeCompleted:^{
[SVProgressHUD dismiss];
}];
[self.viewModel.viewDidAppearCommand.errors subscribeNext:^(id x) {
[SVProgressHUD showErrorWithStatus:@"Error"];
}];
}
@end

20
FRP/FRPPhotoViewModel.h Normal file
View File

@@ -0,0 +1,20 @@
//
// FRPPhotoViewModel.h
// FRP
//
// Created by Ash Furrow on 10/22/2013.
// Copyright (c) 2013 Ash Furrow. All rights reserved.
//
#import <Foundation/Foundation.h>
@class FRPPhotoModel;
@interface FRPPhotoViewModel : NSObject
-(instancetype)initWithPhotoModel:(FRPPhotoModel *)photoModel;
@property (nonatomic, readonly) RACCommand *viewDidAppearCommand;
@property (nonatomic, readonly) RACSignal *photoImageSignal;
@end

50
FRP/FRPPhotoViewModel.m Normal file
View File

@@ -0,0 +1,50 @@
//
// FRPPhotoViewModel.m
// FRP
//
// Created by Ash Furrow on 10/22/2013.
// Copyright (c) 2013 Ash Furrow. All rights reserved.
//
#import "FRPPhotoViewModel.h"
//Utilities
#import "FRPPhotoImporter.h"
@interface FRPPhotoViewModel ()
@property (nonatomic, strong) RACCommand *viewDidAppearCommand;
@property (nonatomic, strong) FRPPhotoModel *photoModel;
@property (nonatomic, strong) RACSignal *photoImageSignal;
@end
@implementation FRPPhotoViewModel
-(instancetype)initWithPhotoModel:(FRPPhotoModel *)photoModel {
self = [self init];
if (!self) return nil;
self.photoModel = photoModel;
@weakify(self);
self.viewDidAppearCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
@strongify(self);
// Fetch data
[[FRPPhotoImporter fetchPhotoDetails:self.photoModel] subscribeError:^(NSError *error) {
[subscriber sendError:nil];
} completed:^{
[subscriber sendCompleted];
}];
}];
}];
self.photoImageSignal = [RACObserve(self.photoModel, fullsizedData) map:^id(id value) {
return [UIImage imageWithData:value];
}];
return self;
}
@end