Студопедия

Главная страница Случайная страница

Разделы сайта

АвтомобилиАстрономияБиологияГеографияДом и садДругие языкиДругоеИнформатикаИсторияКультураЛитератураЛогикаМатематикаМедицинаМеталлургияМеханикаОбразованиеОхрана трудаПедагогикаПолитикаПравоПсихологияРелигияРиторикаСоциологияСпортСтроительствоТехнологияТуризмФизикаФилософияФинансыХимияЧерчениеЭкологияЭкономикаЭлектроника






Текст програми головного модуля






 

#import < UIKit/UIKit.h>

#import " RDgetAccessToken.h"

#import " RDRootFolder.h"

 

@protocol RDWebViewDelegate < NSObject>

 

- (void)exitWebView;

 

@end

 

@interface RDWebView: UIWebView < UIWebViewDelegate, RDgetAccessToken>

@property (strong, nonatomic) id< RDWebViewDelegate> rdWebViewDelegate;

@property (strong, nonatomic) NSString *client;

 

@property (weak, nonatomic) IBOutlet UIWebView *webView;

@property (strong, nonatomic) RDgetAccessToken *getAccessToken;

@property (strong, nonatomic) NSString *authCode;

@property (nonatomic, retain) NSDate *previousTime;


 

- (void)authCodeWithId: (NSString *)client_id callback: (NSString *)callback scope: (NSString *)scope;

 

@end

#import " RDWebView.h"

 

static NSString *const googleUrlOauth = @" https://accounts.google.com/o/oauth2/auth";

static NSString *const dropBoxUrlOauth = @" https://www.dropbox.com/1/oauth2/authorize";

 

 

static NSString *const callback = @" https://localhost";

static NSString *const scope = @" https://www.googleapis.com/auth/drive";

static NSString *const secret = @" IRYNP8V3_unEk0QNqbPFGD6K";

 

@implementation RDWebView

 

- (instancetype)init

{

self = [super init];

if (self) {

self.getAccessToken = [[RDgetAccessToken alloc]init];

[self.getAccessToken setRdGetAccessToken: self];

}

return self;

}

 

- (void)authCodeWithId: (NSString *)client_id callback: (NSString *)callback scope: (NSString *)scope{

 

self.client = client_id;

if (scope) {

[self.webView loadRequest: [NSURLRequest requestWithURL: [NSURL URLWithString: [NSString stringWithFormat: @" %@? response_type=code& client_id=%@& redirect_uri=%@& scope=%@", googleUrlOauth, client_id, callback, scope]]]];

}else{

[self.webView loadRequest: [NSURLRequest requestWithURL: [NSURL URLWithString: [NSString stringWithFormat: @" %@? response_type=code& client_id=%@& redirect_uri=%@", dropBoxUrlOauth, client_id, callback]]]];

}

 

}

- (BOOL)webView: (UIWebView *)webView shouldStartLoadWithRequest: (NSURLRequest *)request navigationType: (UIWebViewNavigationType)navigationType{

NSString * urlString = request.URL.absoluteString;

 

if ([urlString hasPrefix: @" https://localhost" ]) {

 

[self.getAccessToken getAccessToken: [urlString componentsSeparatedByString: @" code=" ][1]idClient: self.client secret: secret callback: callback];

 

}

 

return YES;

 

}

- (NSMutableDictionary *)dropBoxToken: (NSString *)string{

NSMutableDictionary *queryStringDictionary = [[NSMutableDictionary alloc] init];

NSArray *urlComponents = [string componentsSeparatedByString: @" & " ];

for (NSString *keyValuePair in urlComponents)

{

NSArray *pairComponents = [keyValuePair componentsSeparatedByString: @" =" ];

NSString *key = [[pairComponents firstObject] stringByRemovingPercentEncoding];

NSString *value = [[pairComponents lastObject] stringByRemovingPercentEncoding];

 

[queryStringDictionary setObject: value forKey: key];

}

return queryStringDictionary;

}

 

- (void)refresh{

[self.rdWebViewDelegate exitWebView];

}

@end

 

#import < Foundation/Foundation.h>

 

#define RDAPIAccessToken [[NSUserDefaults standardUserDefaults] objectForKey: @" access_token" ]

#define RDAPIDropBoxToken [[NSUserDefaults standardUserDefaults] objectForKey: @" dropBox_token" ]

 

@interface RDDeleteRequst: NSObject

 

- (void)deleteItems: (NSString *)itemsId folder: (NSString *)folder google: (BOOL)goole;

 

@end

#import " RDDeleteRequst.h"

 

@implementation RDDeleteRequst

 

- (void)deleteItems: (NSString *)itemsId folder: (NSString *)folder google: (BOOL)goole{

 

NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];

if (goole) {

request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: [NSString stringWithFormat: @" https://www.googleapis.com/drive/v2/files/%@? access_token=%@", itemsId, RDAPIAccessToken]]];

[request setHTTPMethod: @" DELETE" ];

}else{

request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: [NSString stringWithFormat: @" https://api.dropbox.com/1/fileops/delete" ]]];

[request addValue: [NSString stringWithFormat: @" Bearer %@", RDAPIDropBoxToken] forHTTPHeaderField: @" Authorization" ];

[request setHTTPMethod: @" POST" ];

NSString *postParam = [NSString stringWithFormat: @" root=dropbox& path=%@", itemsId];

[request setHTTPBody: [postParam dataUsingEncoding: NSUTF8StringEncoding]];

}

[NSURLConnection sendAsynchronousRequest: request queue: [NSOperationQueue mainQueue] completionHandler: ^(NSURLResponse *response, NSData *data, NSError *connectionError){

if (! connectionError) {

 

}

}];

}

 

@end

 

#import < Foundation/Foundation.h>

 

#define RDAPIAccessToken [[NSUserDefaults standardUserDefaults] objectForKey: @" access_token" ]

 

@interface RDGetItemSender: NSObject

 

@property (readonly, strong, nonatomic) NSDictionary *items;

+ (void)getItemWithCompletionHandler: (void (^)(RDGetItemSender *itme))handler andId: (NSString *)itemId;

 

@end

 

#import " RDgetAccessToken.h"

 

static NSString *const accessToken = @" access_token";

static NSString *const expiresIn = @" expires_in";

static NSString *const startTime = @" start_time";

static NSString *const dclient_id = @" 0pfe07c1jhrymyf";

static NSString *const dsecret = @" gohn32xbmqym5ct";

 

@implementation RDgetAccessToken

 

- (void)getAccessToken: (NSString *)authCode idClient: (NSString *)client secret: (NSString *)secret callback: (NSString *)callback{

 

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @" https://accounts.google.com/o/oauth2/token" ]];

NSString *postParams = [NSString stringWithFormat: @" code=%@& client_id=%@& client_secret=%@& redirect_uri=%@& grant_type=authorization_code", authCode, client, secret, callback];

[request setHTTPMethod: @" POST" ];

[request setHTTPBody: [postParams dataUsingEncoding: NSUTF8StringEncoding]];

[request setValue: @" application/x-www-form-urlencoded" forHTTPHeaderField: @" Content-Type" ];

 

[NSURLConnection sendAsynchronousRequest: request queue: [NSOperationQueue mainQueue] completionHandler: ^(NSURLResponse *response, NSData *data, NSError *connectionError) {

 

if (! connectionError) {

NSError *jsonError;

if (! jsonError) {

NSDictionary *json = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: & jsonError];

 

NSUserDefaults *userDefault =[NSUserDefaults standardUserDefaults];

[userDefault setObject: [json objectForKey: accessToken] forKey: accessToken];

[userDefault setObject: [json objectForKey: expiresIn] forKey: expiresIn];

[userDefault setObject: [NSDate date] forKey: startTime];

[self.rdGetAccessToken refresh];

}

 

}else{

[self getDropBoxAccessToken: authCode callback: callback];

 

}

 

}];

 

}

 

- (void)getDropBoxAccessToken: (NSString *)authCode callback: (NSString *)callback{

 

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @" https://www.dropbox.com/1/oauth2/token" ]];

NSString *postParams = [NSString stringWithFormat: @" code=%@& client_id=%@& client_secret=%@& redirect_uri=%@& grant_type=authorization_code", authCode, dclient_id, dsecret, callback];

[request setHTTPMethod: @" POST" ];

[request setHTTPBody: [postParams dataUsingEncoding: NSUTF8StringEncoding]];

[request setValue: @" application/x-www-form-urlencoded" forHTTPHeaderField: @" Content-Type" ];

 

[NSURLConnection sendAsynchronousRequest: request queue: [NSOperationQueue mainQueue] completionHandler: ^(NSURLResponse *response, NSData *data, NSError *connectionError) {

 

if (! connectionError) {

NSError *jsonError;

NSDictionary *json = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: & jsonError];

if (! jsonError) {

NSUserDefaults *userDefault =[NSUserDefaults standardUserDefaults];

[userDefault setObject: [json objectForKey: accessToken] forKey: @" dropBox_token" ];

[userDefault setObject: [NSDate date] forKey: @" start_dropbox" ];

[self.rdGetAccessToken refresh];

}

}

 

}];

 

}

@end

 

#import < Foundation/Foundation.h>

#import < UIKit/UIKit.h>

#define RDAPIAccessToken [[NSUserDefaults standardUserDefaults] objectForKey: @" access_token" ]

#define RDAPIDropBoxToken [[NSUserDefaults standardUserDefaults] objectForKey: @" dropBox_token" ]

 

 

@interface RDInsertRequest: NSObject

 

- (void)uploadFileForImage: (UIImage *)image title: (NSString *)title google: (BOOL)google;

 

@end

 

#import " RDInsertRequest.h"

 

@implementation RDInsertRequest

 

- (void)uploadFileForImage: (UIImage *)image title: (NSString *)title google: (BOOL)google{

NSMutableData *dataFile = [[NSMutableData alloc]init];

if (google) {

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @" https://www.googleapis.com/upload/drive/v2/files? uploadType=multipart" ]];

[request setHTTPMethod: @" POST" ];

 

 

NSData *imageData = UIImageJPEGRepresentation(image, 1);

NSString *boundary = @" ---------------------------14737809831466499882746641449";

NSString *contentType = [NSString stringWithFormat: @" multipart/form-data; boundary=%@", boundary];

[request addValue: [NSString stringWithFormat: @" Bearer %@", RDAPIAccessToken] forHTTPHeaderField: @" Authorization" ];

[request addValue: contentType forHTTPHeaderField: @" Content-Type" ];

 

 

[dataFile appendData: [[NSString stringWithFormat: @" \r\n--%@\r\n", boundary] dataUsingEncoding: NSUTF8StringEncoding]];

[dataFile appendData: [[NSString stringWithFormat: @" Content-Type: application/json; charset=UTF-8\r\n\r\n" ] dataUsingEncoding: NSUTF8StringEncoding]];

[dataFile appendData: [[NSString stringWithFormat: @" { \" %@\": \" %@\" }\r\n\r\n ", @" title", title] dataUsingEncoding: NSUTF8StringEncoding]];

[dataFile appendData: [[NSString stringWithFormat: @" \r\n--%@\r\n", boundary] dataUsingEncoding: NSUTF8StringEncoding]];

[dataFile appendData: [[NSString stringWithFormat: @" Content-Type: image/jpeg\r\n\r\n" ] dataUsingEncoding: NSUTF8StringEncoding]];

[dataFile appendData: [NSData dataWithData: imageData]];

 

 

[dataFile appendData: [[NSString stringWithFormat: @" \r\n--%@--\r\n", boundary] dataUsingEncoding: NSUTF8StringEncoding]];

[request addValue: [NSString stringWithFormat: @" %lu", (unsigned long)dataFile.length] forHTTPHeaderField: @" Content-Length" ];

[request setHTTPBody: dataFile];

 

[NSURLConnection sendAsynchronousRequest: request queue: [NSOperationQueue mainQueue] completionHandler: ^(NSURLResponse *response, NSData *data, NSError *connectionError){

if (! connectionError) {

 

}

 

}];

 

}else{

NSData *imageData = UIImageJPEGRepresentation(image, 1);

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @" https://api-content.dropbox.com/2-beta/files/upload" ]];

[request setHTTPMethod: @" POST" ];

[request addValue: [NSString stringWithFormat: @" Bearer %@", RDAPIDropBoxToken] forHTTPHeaderField: @" Authorization" ];

[request addValue: [NSString stringWithFormat: @" %@", @" application/octet-stream" ] forHTTPHeaderField: @" Content-Type" ];

[request addValue: [NSString stringWithFormat: @" {\" path\": \" /%@\", \" mode\": \" overwrite\" }", title] forHTTPHeaderField: @" Dropbox-API-Arg" ];

[dataFile appendData: [NSData dataWithData: imageData]];

[request setHTTPBody: dataFile];

[NSURLConnection sendAsynchronousRequest: request queue: [NSOperationQueue mainQueue] completionHandler: ^(NSURLResponse *response, NSData *data, NSError *connectionError) {

if (! connectionError) {

 

 

}

}];

}

 

}

@end

 

#import < Foundation/Foundation.h>

#import < UIKit/UIKit.h>

#define RDAPIAccessToken [[NSUserDefaults standardUserDefaults] objectForKey: @" access_token" ]

#define RDAPIDropBoxToken [[NSUserDefaults standardUserDefaults] objectForKey: @" dropBox_token" ]

 

@interface RDSaveRequest: NSObject

 

- (void)saveItems: (NSURL *)dowloadUrl andTitle: (NSString *)title google: (BOOL)google;

 

@end

 

#import " RDSaveRequest.h"

 

@implementation RDSaveRequest

 

- (void)saveItems: (NSURL *)dowloadUrl andTitle: (NSString *)title google: (BOOL)google{

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

if (google) {

request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: [NSString stringWithFormat: @" %@? access_token=%@", dowloadUrl, RDAPIAccessToken]]];

}else{

request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: [NSString stringWithFormat: @" https://api-content.dropbox.com/1/files/dropbox/%@? param=rev", title]]];

 

[request addValue: [NSString stringWithFormat: @" Bearer %@", RDAPIDropBoxToken] forHTTPHeaderField: @" Authorization" ];

 

}

 

 

[NSURLConnection sendAsynchronousRequest: request queue: [NSOperationQueue mainQueue] completionHandler: ^(NSURLResponse *response, NSData *data, NSError *connectionError) {

 

if (! connectionError) {

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex: 0];

NSString *filePath = [NSString stringWithFormat: @" %@/%@", documentsDirectory, title];

[data writeToFile: filePath atomically: YES];

if ([title containsString: @".png" ] || [title containsString: @".PNG" ] || [title containsString: @".jpg" ] || [title containsString: @".JPG" ]) {

UIImage *img = [UIImage imageWithContentsOfFile: filePath];

 

UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);

 

}else if ([title containsString: @".mov" ] || [title containsString: @".mp4" ] || [title containsString: @".mp4v" ]){

UISaveVideoAtPathToSavedPhotosAlbum(filePath, nil, nil, nil);

}else {

UIAlertView *message = [[UIAlertView alloc] initWithTitle: title

message: [NSString stringWithFormat: @" File save in %@", filePath]

delegate: nil

cancelButtonTitle: @" OK"

otherButtonTitles: nil];

 

[message show];

 

}

}

 

}];

}

 

@end

 

#import < Foundation/Foundation.h>

#import " RDFolderModel.h"

#import " RDItemModel.h"

#import " RDGetItemSender.h"

 

#define RDAPIAccessToken [[NSUserDefaults standardUserDefaults] objectForKey: @" access_token" ]

#define RDAPIDropBoxToken [[NSUserDefaults standardUserDefaults] objectForKey: @" dropBox_token" ]

 

@interface RDRootFolder: NSObject

@property (strong, nonatomic) NSMutableArray *folderObjectsArray;

@property (strong, nonatomic) NSMutableArray *itemsObjectsArray;

 

 

+ (void)getChaildsWithCompletionHandler: (void (^)(RDRootFolder *chaild))handler andFolder: (NSString *)folder google: (BOOL)param;

 

@end

 

#import " RDRootFolder.h"

 

@implementation RDRootFolder

 

- (instancetype)initWithFolders: (NSArray *)folder andItems: (NSArray *)items

{

self = [super init];

if (self) {

self.folderObjectsArray = [[NSMutableArray alloc]initWithArray: folder];

self.itemsObjectsArray = [[NSMutableArray alloc]initWithArray: items];

 

}

return self;

}

 

 

+ (void)getChaildsWithCompletionHandler: (void (^)(RDRootFolder *chaild))handler andFolder: (NSString *)folder google: (BOOL)param{

 

NSMutableURLRequest *request = [[ NSMutableURLRequest alloc]init];

if (param) {

request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: [NSString stringWithFormat: @" https://www.googleapis.com/drive/v2/files? q='%@'%@", folder, @" %20in%20parents" ]]];

[request addValue: [NSString stringWithFormat: @" Bearer %@", RDAPIAccessToken] forHTTPHeaderField: @" Authorization" ];

}else{

request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: [NSString stringWithFormat: @" https://api.dropbox.com/1/metadata/dropbox%@? list=true", folder]]];

[request addValue: [NSString stringWithFormat: @" Bearer %@", RDAPIDropBoxToken] forHTTPHeaderField: @" Authorization" ];

}

 

 

[NSURLConnection sendAsynchronousRequest: request queue: [NSOperationQueue mainQueue] completionHandler: ^(NSURLResponse *response, NSData *data, NSError *connectionError) {

if (! connectionError) {

NSError *jsonError;

NSDictionary *json = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: & jsonError];

if (! jsonError) {

NSMutableArray *folderArr = [[NSMutableArray alloc]init];

NSMutableArray *itemArr = [[NSMutableArray alloc]init];

if (param) {

[RDGetItemSender getItemWithCompletionHandler: ^(RDGetItemSender *itme) {

RDFolderModel *folderModel = [[RDFolderModel alloc]initWithJson: itme.items];

[folderArr addObject: folderModel];

 

for (id key in json) {

if ([[json objectForKey: key] isKindOfClass: [NSArray class]]) {

for (NSDictionary *item in [json objectForKey: key]) {

 

RDItemModel *itemModel = [[RDItemModel alloc]initWithJson: item];

 

[itemArr addObject: itemModel];

 

}

}

}

RDRootFolder *rootAction = [[RDRootFolder alloc]initWithFolders: folderArr andItems: itemArr];

if (handler) {

handler(rootAction);

}

} andId: folder];

}else{

RDFolderModel *folderModel = [[RDFolderModel alloc]initWithJson: json];

[folderArr addObject: folderModel];

for (id key in json) {

if ([[json objectForKey: key] isKindOfClass: [NSArray class]]) {

for (NSDictionary *item in [json objectForKey: key]) {

 

RDItemModel *itemModel = [[RDItemModel alloc]initWithJson: item];

 

[itemArr addObject: itemModel];

 

}

}

}

RDRootFolder *rootAction = [[RDRootFolder alloc]initWithFolders: folderArr andItems: itemArr];

if (handler) {

handler(rootAction);

}

}

 

}

 

}

}];

}

@end

 

#import < Foundation/Foundation.h>

 

@interface RDFolderModel: NSObject

@property (strong, nonatomic) NSString *parent;

@property (strong, nonatomic) NSString *uuid;

@property (strong, nonatomic) NSString *title;

 

 

- (instancetype)initWithJson: (NSDictionary *)json;

 

@end

 

 


#import " RDFolderModel.h"

 

@implementation RDFolderModel

 

- (instancetype)initWithJson: (NSDictionary *)json {

self = [super init];

if (self) {

[self jsonConvert: json];

}

return self;

}

 

- (void)jsonConvert: (NSDictionary *)json{

for (id key in json) {

 

if ([key isEqualToString: @" id" ]) {

_uuid = [json objectForKey: @" id" ];

_title = [json objectForKey: @" title" ];

_parent = [json objectForKey: @" parent" ];

 

}else if ([key isEqualToString: @" path" ]){

 

_uuid = [json objectForKey: @" path" ];

_title = [json objectForKey: @" path" ];

if ([[json objectForKey: @" path" ] isEqualToString: @" /" ]) {

_parent = nil;

}else{

_parent = [json objectForKey: @" path" ];

}

 

}

}

}

 

@end

 

#import < Foundation/Foundation.h>

#import < UIKit/UIKit.h>

 

@interface RDItemModel: NSObject;

 

@property (strong, nonatomic) NSString *uuid;

@property (strong, nonatomic) NSString *title;

@property double size;

@property (strong, nonatomic) NSString *parentId;

@property (strong, nonatomic) UIImageView *icon;

@property (strong, nonatomic) NSString *isFolder;

@property (strong, nonatomic) NSString *downloadUrl;

 

- (instancetype)initWithJson: (NSDictionary *)json;

 

@end

#import " RDItemModel.h"

 

@implementation RDItemModel

 

- (instancetype)initWithJson: (NSDictionary *)json {

self = [super init];

if (self) {

_icon = [[UIImageView alloc]init];

[self jsonConvert: json];

}

return self;

}

 

- (NSString *)downloadLinkInJson: (NSDictionary *)json{

NSString *url = [[NSString alloc]init];

if ([json.allKeys containsObject: @" webContentLink" ]) {

url = [json objectForKey: @" webContentLink" ];

}else{

url = [[json objectForKey: @" exportLinks" ] objectForKey: @" application/pdf" ];

}

return url;

}

 

- (void)jsonConvert: (NSDictionary *)json{

for (id key in json) {

 

if ([key isEqualToString: @" id" ]) {

_uuid = [json objectForKey: @" id" ];

_title = [json objectForKey: @" title" ];

_size = [[json objectForKey: @" fileSize" ] doubleValue];

_parentId = [[json objectForKey: @" parents" ][0] objectForKey: @" id" ];

_icon.image = [UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString: [json objectForKey: @" iconLink" ]]]];

if ([[json objectForKey: @" mimeType" ] isEqualToString: @" application/vnd.google-apps.folder" ]) {

_isFolder = @" folder";

}

_downloadUrl = [self downloadLinkInJson: json];

}else if ([key isEqualToString: @" path" ]){

 

_uuid = [json objectForKey: @" path" ];

_title = [json objectForKey: @" path" ];

_size = [[json objectForKey: @" bytes" ] doubleValue];

_parentId = [json objectForKey: @" path" ];

if ([[json objectForKey: @" icon" ] isEqualToString: @" folder" ]) {

_isFolder = @" folder";

}

[self createIconWithType: [json objectForKey: @" icon" ]];

}

}

}

 

- (void)createIconWithType: (NSString *)type{

 

if ([type isEqualToString: @" page_white_film" ]) {

_icon.image = [UIImage imageNamed: @" video" ];

} else if([type isEqualToString: @" page_white_picture" ]){

_icon.image = [UIImage imageNamed: @" image" ];

}else if([type isEqualToString: @" page_white_sound" ]){

_icon.image = [UIImage imageNamed: @" audio" ];

}else if([type isEqualToString: @" page_white_compressed" ]){

_icon.image = [UIImage imageNamed: @" archive" ];

}else if([type isEqualToString: @" folder" ]){

_icon.image = [UIImage imageNamed: @" folder" ];

}else{

_icon.image = [UIImage imageNamed: @" docum" ];

}

}

@end

 

#import < UIKit/UIKit.h>

 

@interface RDTableViewCell: UITableViewCell

@property (weak, nonatomic) IBOutlet UIImageView *icon;

@property (weak, nonatomic) IBOutlet UILabel *title;

@property (weak, nonatomic) IBOutlet UILabel *size;

 

@end

#import " RDTableViewCell.h"

 

@implementation RDTableViewCell

 

- (void)awakeFromNib {

// Initialization code

}

 

- (void)setSelected: (BOOL)selected animated: (BOOL)animated {

[super setSelected: selected animated: animated];

 

// Configure the view for the selected state

}

 

@end

#import < UIKit/UIKit.h>

#import " RDTableViewCell.h"

#import " RDItemModel.h"

#import " RDFolderModel.h"

 

@protocol RDTableViewControllerDelegate < NSObject>

 

- (void)getSubFolder: (NSString *)foldert;

- (void)itemIdSendler: (NSString *)itemId andTitle: (NSString *)title andUrl: (NSURL *)url;

 

@end

 

@interface RDTableViewController: NSObject < UITableViewDataSource>

@property (strong, nonatomic) id< RDTableViewControllerDelegate> rdTableViewControllerDelegate;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic) NSArray *folders;

@property (strong, nonatomic) NSArray *items;

 

- (void)setup;

- (void)reloadWithFolder: (NSArray *)folder andItems: (NSArray *)items;

 

@end

 

#import " RDTableViewController.h"

 

static NSString *const identifier = @" Cell";

@interface RDTableViewController ()

 

@end

 

@implementation RDTableViewController

 

- (void)reloadWithFolder: (NSArray *)folder andItems: (NSArray *)items {

self.folders = [[NSArray alloc]initWithArray: folder];

self.items = [[NSArray alloc]initWithArray: items];

[self.tableView reloadData];

 

 

}

 

- (void)setup{

 

[self.tableView registerNib: [UINib nibWithNibName: @" RDTableViewCell" bundle: nil] forCellReuseIdentifier: identifier];

 

}

 

 

#pragma mark - Table view data source

 

- (NSInteger)numberOfSectionsInTableView: (UITableView *)tableView {

 

 

return self.folders.count;

 

}

 

- (NSInteger)tableView: (UITableView *)tableView numberOfRowsInSection: (NSInteger)section {

 

return self.items.count;

}

 

 

- (UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {

 

RDTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: identifier forIndexPath: indexPath];

 

cell = [self styleCell: cell];

 

RDItemModel *items = self.items[indexPath.row];

RDFolderModel *fold = self.folders[indexPath.section];

cell.title.text = [items.title stringByReplacingOccurrencesOfString: [NSString stringWithFormat: @" %@", fold.title] withString: @" " ];

cell.icon.image = items.icon.image;

if (items.size > 0) {

cell.size.text = [NSString stringWithFormat: @" %.2f k/byte", items.size / 1024];

}else{

cell.size.text =@" ";

}

 

 

return cell;

}

 

- (NSString *)tableView: (UITableView *)tableView titleForHeaderInSection: (NSInteger)section{

 

RDFolderModel *folder = self.folders[section];

 

return folder.title;

 

}

 

- (CGFloat)tableView: (UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *)indexPath{

return 36;

}

 

- (void)tableView: (UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath{

 

RDItemModel *item = self.items[indexPath.row];

if (item.isFolder.length > 0) {

[self.rdTableViewControllerDelegate getSubFolder: item.uuid];

}else{

[self.rdTableViewControllerDelegate itemIdSendler: item.uuid andTitle: item.title andUrl: [NSURL URLWithString: item.downloadUrl]];

}

 

}

 

- (RDTableViewCell *)styleCell: (RDTableViewCell *)cell{

CATransform3D rotation;

rotation = CATransform3DMakeRotation((90.0*M_PI)/180, 0.0, 0.7, 0.4);

rotation.m34 = 1.0/ -600;

cell.layer.shadowColor = [[UIColor blackColor]CGColor];

cell.layer.shadowOffset = CGSizeMake(10, 10);

cell.alpha = 0;

 

cell.layer.transform = rotation;

cell.layer.anchorPoint = CGPointMake(0, 0.5);

 

[UIView beginAnimations: @" rotation" context: NULL];

[UIView setAnimationDuration: 0.8];

cell.layer.transform = CATransform3DIdentity;

cell.alpha = 1;

cell.layer.shadowOffset = CGSizeMake(0, 0);

[UIView commitAnimations];

return cell;

}

 

@end

 

#import < UIKit/UIKit.h>

#import < AssetsLibrary/AssetsLibrary.h>

#import " RDTableViewController.h"

#import " RDWebView.h"

#import " RDDeleteRequst.h"

#import " RDSaveRequest.h"

#import " RDInsertRequest.h"

#import " RDItemModel.h"

#import " RDRootFolder.h"

 

#define RDAPIStartTime [[NSUserDefaults standardUserDefaults] objectForKey: @" start_time" ]

#define RDAPIDropBoxStart [[NSUserDefaults standardUserDefaults] objectForKey: @" start_dropbox" ]

 

@interface MainViewController: UIViewController < UIImagePickerControllerDelegate, UINavigationControllerDelegate, RDWebViewDelegate, RDTableViewControllerDelegate, UIActionSheetDelegate>

@property (weak, nonatomic) IBOutlet UIBarButtonItem *editButton;

@property (strong, nonatomic) IBOutlet RDWebView *webViewController;

@property (strong, nonatomic) IBOutlet RDTableViewController *tableViewController;

@property (weak, nonatomic) IBOutlet UIBarButtonItem *backButton;

@property (weak, nonatomic) IBOutlet UIView *prethentView;

@property (weak, nonatomic) IBOutlet UIButton *googleButton;

@property (weak, nonatomic) IBOutlet UIBarButtonItem *startViewButton;

@property (weak, nonatomic) IBOutlet UIBarButtonItem *refreshButton;

 

@property (weak, nonatomic) IBOutlet UIButton *logOut;

 

@property (weak, nonatomic) IBOutlet UIButton *dropBoxButton;

 

@property (strong, nonatomic) NSMutableArray *folder;

@property (strong, nonatomic) RDDeleteRequst *deleteRequest;

@property (strong, nonatomic) NSString *selectedItemsId;

@property (strong, nonatomic) NSString *selectedItemsTitle;

@property (strong, nonatomic) NSURL *selectedItemsDownload;

@property (strong, nonatomic) RDSaveRequest *saveRequst;

@property (strong, nonatomic) RDInsertRequest *insertRequest;

@property BOOL isGoogle;

@property int step;

 

- (IBAction)googleAction: (id)sender;

 

- (IBAction)back: (id)sender;

- (IBAction)dropBoxAction: (id)sender;

 

- (IBAction)editButton: (id)sender;

 

- (IBAction)toStartView: (id)sender;

- (IBAction)refreshAction: (id)sender;

- (IBAction)logOut: (id)sender;

 

@end

 

 

#import " MainViewController.h"

 

@interface MainViewController ()

 

@end

 

@implementation MainViewController

 

- (void)viewDidLoad {

[super viewDidLoad];

[self.tableViewController setup];

self.insertRequest = [[RDInsertRequest alloc]init];

self.deleteRequest = [[RDDeleteRequst alloc]init];

[self showPreView: YES];

[self.tableViewController setRdTableViewControllerDelegate: self];

[self.webViewController setRdWebViewDelegate: self];

[self filesActionsButtonsShow: NO];

[self backButtonShow];

[self logOutShow];

 

 

}

 

 

- (void)filesActionsButtonsShow: (BOOL)show{

if (show) {

self.editButton.enabled = YES;

self.startViewButton.enabled = YES;

self.refreshButton.enabled = YES;

}else{

self.editButton.enabled = NO;

self.startViewButton.enabled = NO;

self.refreshButton.enabled = NO;

}

}

 

- (void)editButtoonEnablet: (BOOL)enab{

if (enab) {

self.editButton.enabled = YES;

 

}else{

self.editButton.enabled = NO;

}

}

 

 

- (void)backButtonShow{

if (self.isGoogle & & self.step == 0) {

 

self.backButton.enabled = NO;

self.backButton.title = nil;

 

}else if(! self.isGoogle & & self.step == 0){

 

self.backButton.enabled = NO;

self.backButton.title = nil;

}else{

self.backButton.enabled = YES;

self.backButton.title = @" Back";

}

}

 

- (void)editButton: (id)sender{

UIActionSheet *editOption = [[UIActionSheet alloc]initWithTitle: @" Edit" delegate: self cancelButtonTitle: @" Cancel" destructiveButtonTitle: nil otherButtonTitles: @" Insert image", @" Save", @" Delete", nil];

[editOption showInView: self.view];

 

}

 

- (IBAction)toStartView: (id)sender {

[self showPreView: YES];

[self filesActionsButtonsShow: NO];

[self logOutShow];

}

 

- (IBAction)refreshAction: (id)sender {

[self getFiles];

}

 

- (IBAction)logOut: (id)sender {

NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];

for (NSHTTPCookie *cookie in [storage cookies]) {

[storage deleteCookie: cookie];

}

[[NSUserDefaults standardUserDefaults] setPersistentDomain: [NSDictionary dictionary] forName: [[NSBundle mainBundle] bundleIdentifier]];

[self logOutShow];

}

 

- (void)imagePickerController: (UIImagePickerController *)picker didFinishPickingMediaWithInfo: (NSDictionary *)info{

 

NSURL *resourceURL = [info objectForKey: UIImagePickerControllerReferenceURL];

ALAssetsLibrary *assetLibrary = [ALAssetsLibrary new];

[assetLibrary assetForURL: resourceURL

resultBlock: ^(ALAsset *asset) {

// get data

ALAssetRepresentation *assetRep = [asset defaultRepresentation];

CGImageRef cgImg = [assetRep fullResolutionImage];

NSString *filename = [assetRep filename];

UIImage *img = [UIImage imageWithCGImage: cgImg];

 

[self.insertRequest uploadFileForImage: img title: filename google: self.isGoogle];

 

}

failureBlock: ^(NSError *error) {

NSLog(@" %@", error);

}];

[picker dismissViewControllerAnimated: YES completion: NULL];

}

 

- (void)imagePickerControllerDidCancel: (UIImagePickerController *)picker {

 

[picker dismissViewControllerAnimated: YES completion: NULL];

 

}

 

 

- (IBAction)googleAction: (id)sender {

 

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath: @" transform.rotation" ];

animation.fromValue = [NSNumber numberWithFloat: 0];

animation.toValue = [NSNumber numberWithFloat: ((360*M_PI)/180)];

animation.duration = 2;

animation.repeatCount = 1;

[self.googleButton.layer addAnimation: animation forKey: @" 360" ];

[NSTimer scheduledTimerWithTimeInterval: 0.5

target: self

selector: @selector(googleGo)

userInfo: nil

repeats: NO];

 

 

}

 

- (void)googleGo{

self.isGoogle = YES;

if ([self timeOutGoogle]) {

[self webViewRequest];

}else{

[self setup];

}

}

 

- (IBAction)back: (id)sender {

[self.folder removeObjectAtIndex: self.step];

 

self.step--;

 

[self getFiles];

 

}

 

 

- (void)dropBoxGo{

self.isGoogle = NO;

if ([self timeOutDropBox]) {

[self webViewRequest];

}else{

[self setup];

}

 

 

}

- (IBAction)dropBoxAction: (id)sender {

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath: @" transform.rotation" ];

animation.fromValue = [NSNumber numberWithFloat: 0];

animation.toValue = [NSNumber numberWithFloat: ((360*M_PI)/180)];

animation.duration = 2;

animation.repeatCount = 1;

[self.dropBoxButton.layer addAnimation: animation forKey: @" 360" ];

[NSTimer scheduledTimerWithTimeInterval: 0.5

target: self

selector: @selector(dropBoxGo)

userInfo: nil

repeats: NO];

 

}

 

- (void)showTableView: (BOOL)param{

 

if (param) {

self.tableViewController.tableView.hidden=NO;

self.webViewController.webView.hidden=YES;

self.prethentView.hidden = YES;

 

}else{

self.tableViewController.tableView.hidden=YES;

self.webViewController.webView.hidden=NO;

self.prethentView.hidden = YES;

}

}

 

- (void)showPreView: (BOOL)param{

if (param) {

self.prethentView.hidden =NO;

}else{

self.prethentView.hidden =YES;

}

 

}

 

- (void)logOutShow{

if (RDAPIDropBoxToken || RDAPIAccessToken) {

self.logOut.hidden = NO;

}else{

self.logOut.hidden = YES;

}

}

- (void)setup{

 

self.folder = [[NSMutableArray alloc]init];

 

self.step = 0;

[self logOutShow];

if (self.isGoogle) {

if (RDAPIAccessToken) {

[self.folder addObject: @" root" ];

}else{

[self webViewRequest];

}

 

} else {

if (RDAPIDropBoxToken) {

[self.folder addObject: @" /" ];

}else{

[self webViewRequest];

}

 

 

}

[self getFiles];

}

- (void)exitWebView{

[self setup];

 

 

}

 

- (void)getFiles{

[self filesActionsButtonsShow: YES];

[self backButtonShow];

[RDRootFolder getChaildsWithCompletionHandler: ^(RDRootFolder *chaild) {

[self.tableViewController reloadWithFolder: chaild.folderObjectsArray andItems: chaild.itemsObjectsArray];

[self showTableView: YES];

 

} andFolder: self.folder[self.step] google: self.isGoogle];

}

 

- (void)getSubFolder: (NSString *)folder{

[self.folder addObject: folder];

self.step ++;

[self getFiles];

 

 

}

 

- (void)itemIdSendler: (NSString *)itemId andTitle: (NSString *)title andUrl: (NSURL *)url{

self.selectedItemsId = [itemId stringByReplacingOccurrencesOfString: [NSString stringWithFormat: @" %@", self.folder[self.step]] withString: @" " ];

self.selectedItemsTitle = [title stringByReplacingOccurrencesOfString: [NSString stringWithFormat: @" %@", self.folder[self.step]] withString: @" " ];

self.selectedItemsDownload = url;

}

 

- (void)actionSheet: (UIActionSheet *)actionSheet clickedButtonAtIndex: (NSInteger)buttonIndex{

switch (buttonIndex) {

case 0: {

self.insertRequest = [[RDInsertRequest alloc]init];

UIImagePickerController *picker = [[UIImagePickerController alloc] init];

picker.delegate = self;

 

picker.allowsEditing = YES;

 

picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;

[self presentViewController: picker animated: YES completion: nil];

}

 

break;

 

case 1: {

self.saveRequst = [[RDSaveRequest alloc]init];

[self.saveRequst saveItems: self.selectedItemsDownload andTitle: self.selectedItemsTitle google: self.isGoogle];

}

 

break;

case 2: {

if (self.selectedItemsId) {

self.deleteRequest = [[RDDeleteRequst

alloc]init];

[self.deleteRequest deleteItems: self.selectedItemsId folder: self.folder[self.step] google: self.isGoogle];

}else{

UIAlertView *message = [[UIAlertView alloc] initWithTitle: @" Message"

message: @" Select file to action"

delegate: nil

cancelButtonTitle: @" OK"

otherButtonTitles: nil];

 

[message show];

}

 

 

}

 

break;

default:

break;

}

}

 

- (BOOL)timeOutGoogle{

 

NSDate *endDate =[NSDate date];

NSDate *startDate = RDAPIStartTime;

NSTimeInterval distanceBetweenDates = [endDate timeIntervalSinceDate: startDate];

if (distanceBetweenDates < 3600) {

return NO;

}else{

return YES;

}

 

}

 

 

- (BOOL)timeOutDropBox{

 

NSDate *endDate =[NSDate date];

NSDate *startDate = RDAPIDropBoxStart;

NSTimeInterval distanceBetweenDates = [endDate timeIntervalSinceDate: startDate];

if (distanceBetweenDates < 3600) {

return NO;

}else{

return YES;

}

 

}

 

- (void)webViewRequest{

[self showTableView: NO];

if (self.isGoogle) {

 

[self.webViewController authCodeWithId: @" 555462043534-22adjdbcpjp7f32uq4si57tsoqtjimh0.apps.googleusercontent.com" callback: @" https://localhost" scope: @" https://www.googleapis.com/auth/drive" ];

}else{

[self.webViewController authCodeWithId: @" 0pfe07c1jhrymyf" callback: @" https://localhost" scope: nil];

 

}

}

 

 

@end

 






© 2023 :: MyLektsii.ru :: Мои Лекции
Все материалы представленные на сайте исключительно с целью ознакомления читателями и не преследуют коммерческих целей или нарушение авторских прав.
Копирование текстов разрешено только с указанием индексируемой ссылки на источник.