Skip to content

Instantly share code, notes, and snippets.

@appleios
Created November 9, 2015 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save appleios/ee876418dc0368bd1865 to your computer and use it in GitHub Desktop.
Save appleios/ee876418dc0368bd1865 to your computer and use it in GitHub Desktop.
kvo controller and etc
//
// VIOSMessageHeaderView.h
// vipole
//
// Created by Aziz Latypov on 09/11/15.
// Copyright © 2015 vipole. All rights reserved.
//
#import <UIKit/UIKit.h>
@protocol VIOSMessageHeaderViewModelProtocol;
@interface VIOSMessageHeaderView : UITableViewHeaderFooterView
@property (strong, nonatomic) _Nullable id<VIOSMessageHeaderViewModelProtocol> viewModel;
- (_Nonnull instancetype)initWithCoder:( NSCoder * _Nonnull )aDecoder andViewModel:(_Nullable id<VIOSMessageHeaderViewModelProtocol>)viewModel NS_DESIGNATED_INITIALIZER;
- (_Nullable instancetype)initWithReuseIdentifier:(nullable NSString *)reuseIdentifier andViewModel:(_Nullable id<VIOSMessageHeaderViewModelProtocol>)viewModel NS_DESIGNATED_INITIALIZER;
// outlets
@property (strong, nonatomic) UILabel * _Nullable nameLabel;
@property (strong, nonatomic) UILabel * _Nullable dateLabel;
@end
//
// VIOSMessageHeaderView.m
// vipole
//
// Created by Aziz Latypov on 09/11/15.
// Copyright © 2015 vipole. All rights reserved.
//
#import "VIOSMessageHeaderView.h"
#import "VIOSMessageHeaderViewModel.h"
#import "FBKVOController.h"
#import "NSObject+FBKVOController.h"
#import "NSObject+PropertyName.h"
@implementation VIOSMessageHeaderView
#pragma mark - Init -
- (_Nonnull instancetype)initWithCoder:( NSCoder * _Nonnull )aDecoder andViewModel:(_Nullable id<VIOSMessageHeaderViewModelProtocol>)viewModel
{
self = [super initWithCoder:aDecoder];
if (self) {
[self configureWithViewModel:viewModel];
}
return self;
}
- (_Nullable instancetype)initWithReuseIdentifier:(nullable NSString *)reuseIdentifier andViewModel:(_Nullable id<VIOSMessageHeaderViewModelProtocol>)viewModel
{
self = [super initWithReuseIdentifier:reuseIdentifier];
if (self) {
[self configureWithViewModel:viewModel];
}
return self;
}
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
return [self initWithReuseIdentifier:reuseIdentifier andViewModel:nil];
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
return [self initWithCoder:aDecoder andViewModel:nil];
}
#pragma mark -
- (void)configureWithViewModel:(_Nullable id<VIOSMessageHeaderViewModelProtocol>)viewModel
{
self.viewModel = viewModel;
}
#pragma mark -
- (void)setViewModel:(id<VIOSMessageHeaderViewModelProtocol>)viewModel
{
if (_viewModel != viewModel) {
[self.KVOController unobserve:_viewModel];
_viewModel = viewModel;
__weak typeof(self) weakSelf = self;
weakSelf.nameLabel.text = _viewModel.name;
weakSelf.dateLabel.text = _viewModel.date;
[self.KVOController observe:_viewModel
keyPath:keypathStringForProtocol(VIOSMessageHeaderViewModelProtocol, name)
options:NSKeyValueObservingOptionNew
block:^(id observer, id object, NSDictionary *change)
{
weakSelf.nameLabel.text = _viewModel.name;
}];
[self.KVOController observe:_viewModel
keyPath:keypathStringForProtocol(VIOSMessageHeaderViewModelProtocol, date)
options:NSKeyValueObservingOptionNew
block:^(id observer, id object, NSDictionary *change)
{
weakSelf.dateLabel.text = _viewModel.date;
}];
}
}
#pragma mark - Dealloc -
- (void)dealloc
{
[self.KVOController unobserveAll];
}
@end
//
// VIOSMessageHeaderViewModel.h
// vipole
//
// Created by Aziz Latypov on 09/11/15.
// Copyright © 2015 vipole. All rights reserved.
//
#import "VIOSViewModel.h"
@protocol VIOSMessageHeaderViewModelProtocol <NSObject>
@property (readonly, nonatomic) NSString *name;
@property (readonly, nonatomic) NSString *date;
@end
@interface VIOSMessageHeaderViewModel : VIOSViewModel <VIOSMessageHeaderViewModelProtocol>
@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSString *date;
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment