This repository has been archived by the owner on Dec 5, 2019. It is now read-only.
forked from bitswift/Proton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PROViewModel.h
170 lines (149 loc) · 4.82 KB
/
PROViewModel.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//
// PROViewModel.h
// Proton
//
// Created by Justin Spahr-Summers on 01.04.12.
// Copyright (c) 2012 Bitswift. All rights reserved.
//
#import <Foundation/Foundation.h>
@class PROBinding;
/**
* Defines how a <PROViewModel> property is encoded into an archive.
*/
typedef enum {
/**
* The property should not be encoded.
*/
PROViewModelEncodingBehaviorNone = 0,
/**
* The property should be encoded unconditionally.
*/
PROViewModelEncodingBehaviorUnconditional,
/**
* The property should be encoded only if unconditionally encoded elsewhere.
*/
PROViewModelEncodingBehaviorConditional
} PROViewModelEncodingBehavior;
/**
* A base class for presentation model objects.
*
* To create a subclass:
*
* 1. Declare and synthesize any properties desired.
* 2. Override `setModel:` to tear down and set up bindings from the <model>.
*
* This class automatically implements `<NSCoding>` for itself and its
* subclasses, using the behavior of <encodingBehaviorForKey:> to determine
* which properties to archive.
*
* The implementations of `-hash` and `-isEqual:` are based on the `readwrite`
* properties of the class, including the <model> property.
*
* Upon deallocation, instances of this class will automatically invoke
* <[NSObject removeAllOwnedObservers]> and <[PROBinding
* removeAllBindingsFromOwner:>, and automatically unregister from any
* notifications from the default `NSNotificationCenter`.
*
* This class is not thread-safe.
*/
@interface PROViewModel : NSObject <NSCoding>
/**
* @name Initialization
*/
/**
* Initializes the receiver with the <defaultValuesForKeys> of the receiver's
* class.
*
* This is the designated initializer for this class.
*/
- (id)init;
/**
* Initializes the receiver, setting its <model> to the given object.
*
* @param model An object to use as the receiver's <model>.
*/
- (id)initWithModel:(id)model;
/**
* Whether the receiver is currently being initialized from an archive.
*
* This can be used to conditionalize the behavior of properties based on
* whether they're being set as part of decoding, or explicitly afterward.
*/
@property (nonatomic, getter = isInitializingFromArchive, readonly) BOOL initializingFromArchive;
/**
* @name Model
*/
/**
* The model object presented by the receiver.
*
* Setting this property to a new object will automatically invoke <[PROBinding
* removeAllBindingsFromOwner:>, thereby removing all of the receiver's bindings
* before changing model objects.
*
* @note This object is not archived with the receiver.
*/
@property (nonatomic, strong) id model;
/**
* Parent View Models
*/
/**
* The immediate parent view model of the receiver. Returns `nil` if the
* receiver is the <rootViewModel> for its hierarchy.
*
* This property is `nil` by default.
*/
@property (nonatomic, weak) PROViewModel *parentViewModel;
/**
* The root view model of the receiver.
*
* Returns the <[PROViewModel rootViewModel]> of the receiver's <parentViewModel>
* or the receiver if no <parentViewModel> is `nil`.
*/
@property (nonatomic, weak, readonly) PROViewModel *rootViewModel;
/**
* @name Declared Properties
*/
/**
* Returns default values for the receiver's properties. Any keys not present in
* the dictionary will not be set to a default value.
*
* Default values should be provided using this method so that <init> can set
* them appropriately as part of initialization.
*
* The default implementation of this method returns an empty dictionary.
*/
+ (NSDictionary *)defaultValuesForKeys;
/**
* Returns whether or how the given property key should be encoded into an archive.
*
* This is invoked from this class' implementation of `encodeWithCoder:`.
*
* The default implementation of this method returns:
*
* - `PROViewModelEncodingBehaviorUnconditional` for any writable properties
* (even if only privately writable) that are primitive values or strongly retained.
* - `PROViewModelEncodingBehaviorConditional` for any writable properties that
* are unretained (i.e., `unsafe_unretained` or `weak`).
* - `PROViewModelEncodingBehaviorNone` for all other properties.
*
* @param key The key to determine encoding behavior for.
*/
+ (PROViewModelEncodingBehavior)encodingBehaviorForKey:(NSString *)key;
/**
* @name Validating Actions
*/
/**
* Returns whether the given action selector can be validly invoked, given the
* current state of the receiver.
*
* The default implementation of this method looks for a method named
* `-validate<Action>` on the receiver, invoking it and using its result if
* present. Subclasses that override this method to perform custom logic should
* invoke `super` at some point in their implementation.
*
* If the receiver does not respond to `action`, `NO` is returned.
*
* @param action The selector to validate.
*/
- (BOOL)validateAction:(SEL)action;
@end