Created
August 21, 2017 14:52
-
-
Save userow/13de570a07409c60e6ff4153660ea317 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
// | |
// Counter.h | |
// | |
#import <Foundation/Foundation.h> | |
@interface Counter : NSObject <NSCoding> | |
/** | |
текущее значение счётчика | |
*/ | |
@property (assign) NSInteger currentValue; | |
/** | |
инкремент счётчика | |
*/ | |
@property (assign) NSInteger incrementValue; | |
/** | |
граничное значение - по достижении которого счётчик сбрасывается до 0 | |
*/ | |
@property (assign) NSInteger limitValue; | |
/** | |
initializer | |
@return Counter | |
*/ | |
- (instancetype)init; | |
/** | |
constructor | |
@param increment инкремент | |
@return Counter | |
*/ | |
+ (instancetype)counterWithIncrement:(NSInteger)increment; | |
/** | |
constructor | |
@param increment инкремент | |
@param limit граничное значение | |
@return Counter | |
*/ | |
+ (instancetype)counterWithIncrement:(NSInteger)increment | |
limit:(NSInteger)limit; | |
/** | |
constructor | |
@param increment инкремент | |
@param limit граничное значение | |
@param current текущее значение | |
@return Counter | |
*/ | |
+ (instancetype)counterWithIncrement:(NSInteger)increment | |
limit:(NSInteger)limit | |
current:(NSInteger)current; | |
/** | |
сброс значения счётчика | |
*/ | |
- (void)reset; | |
/** | |
инкрементирование счётчика | |
@return инерементированное значение счётчика | |
*/ | |
- (NSInteger)count; | |
//TODO: NSCoding | |
//TODO: Hash | |
@end | |
#import "Counter.h" | |
@interface Counter () | |
@property (nonatomic, strong, readonly) NSUserDefaults *userDefaults; | |
@end | |
@implementation Counter | |
#pragma mark - initializers and constructors | |
- (instancetype)init { | |
if (self = [super init]) { | |
_userDefaults = [NSUserDefaults standardUserDefaults]; | |
_incrementValue = 1; | |
_currentValue = 0; | |
_limitValue = 0; | |
} | |
return self; | |
} | |
+ (instancetype)counterWithIncrement:(NSInteger)increment | |
limit:(NSInteger)limit | |
current:(NSInteger)current | |
{ | |
Counter *cnt = [Counter new]; | |
cnt.incrementValue = increment; | |
cnt.limitValue = limit; | |
cnt.currentValue = current; | |
return cnt; | |
} | |
+ (instancetype)counterWithIncrement:(NSInteger)increment | |
limit:(NSInteger)limit | |
{ | |
Counter *cnt = [Counter counterWithIncrement:increment | |
limit:limit | |
current:0]; | |
return cnt; | |
} | |
+ (instancetype)counterWithIncrement:(NSInteger)increment | |
{ | |
Counter *cnt = [Counter counterWithIncrement:increment | |
limit:0 | |
current:0]; | |
return cnt; | |
} | |
#pragma mark - description | |
- (NSString *)description { | |
NSString *desc = [NSString stringWithFormat:@"current: %ld, increment: %ld, limit: %ld", | |
(long)_currentValue, | |
(long)_incrementValue, | |
(long)_limitValue]; | |
return desc; | |
} | |
#pragma mark - equality | |
- (BOOL)isEqual:(id)object | |
{ | |
BOOL equal = NO; | |
if ([object isKindOfClass:[Counter class]]) { | |
Counter *cn = (Counter *)object; | |
if (cn.currentValue == _currentValue && | |
cn.incrementValue == _incrementValue && | |
cn.limitValue == _limitValue) | |
{ | |
equal = YES; | |
} | |
} | |
return equal; | |
} | |
#pragma mark - operations | |
- (NSInteger)count { | |
self.currentValue += self.incrementValue; | |
if (self.limitValue && self.currentValue >= self.limitValue) { | |
self.currentValue = 0; | |
} | |
return self.currentValue; | |
} | |
- (void)reset; | |
{ | |
self.currentValue = 0; | |
} | |
#pragma mark NSCoding | |
- (void)encodeWithCoder:(NSCoder *)aCoder; | |
{ | |
[aCoder encodeObject:@(_currentValue) forKey:@"currentValue"]; | |
[aCoder encodeObject:@(_incrementValue) forKey:@"incrementValue"]; | |
[aCoder encodeObject:@(_limitValue) forKey:@"limitValue"]; | |
} | |
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder; | |
{ | |
if (self = [super init]) | |
{ | |
_userDefaults = [NSUserDefaults standardUserDefaults]; | |
_currentValue = [[aDecoder decodeObjectForKey:@"currentValue"] integerValue]; | |
_incrementValue = [[aDecoder decodeObjectForKey:@"incrementValue"] integerValue]; | |
_limitValue = [[aDecoder decodeObjectForKey:@"limitValue"] integerValue]; | |
} | |
return self; | |
} | |
/* | |
#pragma mark - saving to / loading form NSUserDefaults | |
- (void)save; | |
{ | |
NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:self]; | |
[[NSUserDefaults standardUserDefaults] setObject:encodedObject forKey:CURRENT_USER_KEY]; | |
[[NSUserDefaults standardUserDefaults] synchronize]; | |
} | |
+ (BUUser *)loadUser; | |
{ | |
DDLogInfo(@"Loading User..."); | |
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; | |
NSData *encodedObject = [defaults objectForKey:CURRENT_USER_KEY]; | |
BUUser *object = [NSKeyedUnarchiver unarchiveObjectWithData:encodedObject]; | |
return object; | |
} | |
*/ | |
@end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment