From 188d8e4ea870d1a6c8edd5a9b323742804badeba Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sun, 28 Aug 2016 21:03:22 +0300 Subject: project cleanup --- Juick/Model/Message.h | 29 ++++++++++++++++++++++++++++ Juick/Model/Message.m | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++ Juick/Model/User.h | 27 ++++++++++++++++++++++++++ Juick/Model/User.m | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 157 insertions(+) create mode 100644 Juick/Model/Message.h create mode 100644 Juick/Model/Message.m create mode 100644 Juick/Model/User.h create mode 100644 Juick/Model/User.m (limited to 'Juick/Model') diff --git a/Juick/Model/Message.h b/Juick/Model/Message.h new file mode 100644 index 0000000..b100e7d --- /dev/null +++ b/Juick/Model/Message.h @@ -0,0 +1,29 @@ +// +// Message.h +// Juick +// +// Created by Vitaly Takmazov on 29.10.13. +// Copyright (c) 2013 com.juick. All rights reserved. +// + +#import +#import "User.h" + +@interface Message : NSObject + +@property NSNumber *MID; +@property NSNumber *RID; +@property User *user; +@property NSString *text; +@property NSArray *tags; +@property NSString *timestamp; +@property NSString *attach; +@property NSString *repliesBy; +@property NSNumber *repliesCount; + ++(void) pullNextFromPath:(NSString *)path params:(NSDictionary *)params callback:(void(^)(NSArray *))callback; ++(NSString *) messagesUrl; ++(NSString *) threadUrl; ++(NSString *) feedUrl; + +@end diff --git a/Juick/Model/Message.m b/Juick/Model/Message.m new file mode 100644 index 0000000..de08e98 --- /dev/null +++ b/Juick/Model/Message.m @@ -0,0 +1,52 @@ +// +// Message.m +// Juick +// +// Created by Vitaly Takmazov on 29.10.13. +// Copyright (c) 2013 com.juick. All rights reserved. +// + +#import "Message.h" +#import "PDKeychainBindings.h" + +@implementation Message + ++ (NSDictionary *)modelCustomPropertyMapper { + return @{@"text" : @"body", + @"MID" : @"mid", + @"RID" : @"rid", + @"attach" : @"photo.small", + @"repliesCount": @"replies", + @"repliesBy": @"repliesby"}; +} + + ++(void) pullNextFromPath:(NSString *)path params:(NSDictionary *) params callback:(void(^)(NSArray *))callback { + AFHTTPSessionManager *manager = [APIClient sharedClient].manager; + if ([[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.username"] != nil) { + [manager.requestSerializer setAuthorizationHeaderFieldWithUsername:[[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.username"] password:[[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.password"]]; + } + [manager GET:path parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + NSMutableArray *messages = [NSMutableArray new]; + [((NSArray *)responseObject) enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { + [messages addObject:[Message yy_modelWithJSON:obj]]; + }]; + callback(messages); + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + NSLog(@"REST Error: %@", error); + }]; +} + ++(NSString *) messagesUrl { + return @"https://api.juick.com/messages"; +} + ++(NSString *) threadUrl { + return @"https://api.juick.com/thread"; +} + ++(NSString *) feedUrl { + return @"https://api.juick.com/home"; +} + +@end diff --git a/Juick/Model/User.h b/Juick/Model/User.h new file mode 100644 index 0000000..84300f3 --- /dev/null +++ b/Juick/Model/User.h @@ -0,0 +1,27 @@ +// +// User.h +// Juick +// +// Created by Vitaly on 01.02.14. +// Copyright (c) 2014 com.juick. All rights reserved. +// + +#import +#import "PDKeychainBindings.h" +#import "SIAlertView.h" +#import "APIClient.h" +#import "YYModel.h" + +@interface User : NSObject +@property (nonatomic, strong) NSString *uname; +@property (nonatomic, strong) NSString *uid; + ++ (void) get:(NSString *)name callback:(void(^)(User *))callback; + ++ (BOOL) isAuthenticated; + ++ (void) checkIsValid:(void(^)(BOOL))callback; + ++ (void) throwUnableToLogin; + +@end diff --git a/Juick/Model/User.m b/Juick/Model/User.m new file mode 100644 index 0000000..f4b1e00 --- /dev/null +++ b/Juick/Model/User.m @@ -0,0 +1,49 @@ +// +// User.m +// Juick +// +// Created by Vitaly on 01.02.14. +// Copyright (c) 2014 com.juick. All rights reserved. +// + +#import "User.h" + + +@implementation User + ++(BOOL) isAuthenticated { + return [[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.username"] != nil; +} + ++(void) throwUnableToLogin { + SIAlertView *alert = [[SIAlertView alloc] initWithTitle:@"Error" andMessage:@"Unable to login, check username/password, or network connectivity"]; + [alert addButtonWithTitle:@"OK" type:SIAlertViewButtonTypeCancel handler:^(SIAlertView *alertView) { + // <#code#> + }]; + [alert show]; +} + ++(void) checkIsValid:(void (^)(BOOL))callback { + AFHTTPSessionManager *manager = [APIClient sharedClient].manager; + [manager.requestSerializer setAuthorizationHeaderFieldWithUsername:[[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.username"] password:[[PDKeychainBindings sharedKeychainBindings] stringForKey:@"com.juick.password"]]; + [manager POST:@"/post" parameters:nil progress:nil success:nil failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + NSInteger statusCode = ((NSHTTPURLResponse *)task.response).statusCode; + if (statusCode == 400) { + callback(YES); + } else { + [manager.requestSerializer setAuthorizationHeaderFieldWithUsername:nil password:nil]; + callback(NO); + } + }];} + ++(void) get:(NSString *) name callback:(void(^)(User *))callback { + AFHTTPSessionManager *manager = [APIClient sharedClient].manager; + NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:name, @"uname", nil]; + [manager GET:@"/users" parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { + callback([User yy_modelWithJSON:[(NSArray *)responseObject firstObject]]); + } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { + callback(nil); + }]; +} + +@end -- cgit v1.2.3