diff --git a/iOSStudy/Bean/FavouriteBean.h b/iOSStudy/Bean/FavouriteBean.h index 1bc484a..c2efd4c 100644 --- a/iOSStudy/Bean/FavouriteBean.h +++ b/iOSStudy/Bean/FavouriteBean.h @@ -8,12 +8,6 @@ #import -typedef NS_ENUM(NSInteger, FAVOURITE_TYPE) { - BLOG_TYPE = 1, - WEB_TYPE = 2, - VIDEO_TYPE = 3 -}; - @interface FavouriteBean : NSObject /** * 标题 @@ -34,5 +28,5 @@ typedef NS_ENUM(NSInteger, FAVOURITE_TYPE) { /** * 类型 1 博客 2 网站 3 视频 */ -@property(nonatomic,copy)NSString* type; +@property(nonatomic,assign)NSInteger *type; @end diff --git a/iOSStudy/Tools/NetWorkTools.m b/iOSStudy/Tools/NetWorkTools.m index 0b30257..ee11210 100644 --- a/iOSStudy/Tools/NetWorkTools.m +++ b/iOSStudy/Tools/NetWorkTools.m @@ -77,7 +77,7 @@ +(void)checkNetworking{ { NSLog(@"网络断开!"); - SharedApp.isNetworking = YES; + SharedApp.isNetworking = NO; dispatch_async(dispatch_get_main_queue(), ^{ diff --git a/iOSStudy/iOSStudy.xcodeproj/project.pbxproj b/iOSStudy/iOSStudy.xcodeproj/project.pbxproj index 77fbb5b..4e2c327 100644 --- a/iOSStudy/iOSStudy.xcodeproj/project.pbxproj +++ b/iOSStudy/iOSStudy.xcodeproj/project.pbxproj @@ -63,8 +63,6 @@ 18D908A71A8A205600B42A42 /* ThirdTableViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 18D908A61A8A205600B42A42 /* ThirdTableViewController.m */; }; 18DF6D3D1A80D3BC005751E8 /* Reachability.m in Sources */ = {isa = PBXBuildFile; fileRef = 18DF6D3C1A80D3BC005751E8 /* Reachability.m */; }; 18DF6D3F1A80D40D005751E8 /* SystemConfiguration.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 18DF6D3E1A80D40D005751E8 /* SystemConfiguration.framework */; }; - 18E016AA1ACAA898002A6E59 /* CoreData.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 18E016A91ACAA898002A6E59 /* CoreData.framework */; }; - 18E016E51ACAAB35002A6E59 /* iOSStudy.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 18E016E31ACAAB35002A6E59 /* iOSStudy.xcdatamodeld */; }; 54A0444849F1931415392FC1 /* libPods.a in Frameworks */ = {isa = PBXBuildFile; fileRef = E83ED1B8EA89BC57ACF91BA5 /* libPods.a */; }; /* End PBXBuildFile section */ @@ -164,8 +162,6 @@ 18DF6D3B1A80D3BC005751E8 /* Reachability.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Reachability.h; sourceTree = ""; }; 18DF6D3C1A80D3BC005751E8 /* Reachability.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Reachability.m; sourceTree = ""; }; 18DF6D3E1A80D40D005751E8 /* SystemConfiguration.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SystemConfiguration.framework; path = System/Library/Frameworks/SystemConfiguration.framework; sourceTree = SDKROOT; }; - 18E016A91ACAA898002A6E59 /* CoreData.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreData.framework; path = System/Library/Frameworks/CoreData.framework; sourceTree = SDKROOT; }; - 18E016E41ACAAB35002A6E59 /* iOSStudy.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = iOSStudy.xcdatamodel; sourceTree = ""; }; 2E2CC00A58FD0A745C574BAD /* Pods.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = Pods.release.xcconfig; path = "Pods/Target Support Files/Pods/Pods.release.xcconfig"; sourceTree = ""; }; E83ED1B8EA89BC57ACF91BA5 /* libPods.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libPods.a; sourceTree = BUILT_PRODUCTS_DIR; }; /* End PBXFileReference section */ @@ -175,7 +171,6 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( - 18E016AA1ACAA898002A6E59 /* CoreData.framework in Frameworks */, 18DF6D3F1A80D40D005751E8 /* SystemConfiguration.framework in Frameworks */, 54A0444849F1931415392FC1 /* libPods.a in Frameworks */, ); @@ -308,7 +303,6 @@ 18B23B321A7A14D100E80854 /* Images.xcassets */, 18B23B341A7A14D100E80854 /* LaunchScreen.xib */, 18B23B221A7A14D000E80854 /* Supporting Files */, - 18E016E31ACAAB35002A6E59 /* iOSStudy.xcdatamodeld */, ); path = iOSStudy; sourceTree = ""; @@ -445,7 +439,6 @@ 910A021243EF9C731476BCA9 /* Frameworks */ = { isa = PBXGroup; children = ( - 18E016A91ACAA898002A6E59 /* CoreData.framework */, 18DF6D3E1A80D40D005751E8 /* SystemConfiguration.framework */, E83ED1B8EA89BC57ACF91BA5 /* libPods.a */, ); @@ -625,7 +618,6 @@ buildActionMask = 2147483647; files = ( 18AB47981A99A51200E4C030 /* BaseTableViewCell.m in Sources */, - 18E016E51ACAAB35002A6E59 /* iOSStudy.xcdatamodeld in Sources */, 18AB479F1A9C5E3300E4C030 /* VideoCell.m in Sources */, 18B23B2E1A7A14D100E80854 /* SecondViewController.m in Sources */, 18B23B4D1A7A234800E80854 /* BaseViewController.m in Sources */, @@ -861,19 +853,6 @@ defaultConfigurationName = Release; }; /* End XCConfigurationList section */ - -/* Begin XCVersionGroup section */ - 18E016E31ACAAB35002A6E59 /* iOSStudy.xcdatamodeld */ = { - isa = XCVersionGroup; - children = ( - 18E016E41ACAAB35002A6E59 /* iOSStudy.xcdatamodel */, - ); - currentVersion = 18E016E41ACAAB35002A6E59 /* iOSStudy.xcdatamodel */; - path = iOSStudy.xcdatamodeld; - sourceTree = ""; - versionGroupType = wrapper.xcdatamodel; - }; -/* End XCVersionGroup section */ }; rootObject = 18B23B171A7A14D000E80854 /* Project object */; } diff --git a/iOSStudy/iOSStudy/AppDelegate.h b/iOSStudy/iOSStudy/AppDelegate.h index a86a54a..c6bee7f 100644 --- a/iOSStudy/iOSStudy/AppDelegate.h +++ b/iOSStudy/iOSStudy/AppDelegate.h @@ -7,19 +7,11 @@ // #import -#import + @interface AppDelegate : UIResponder @property (strong, nonatomic) UIWindow *window; @property(nonatomic,assign)__block BOOL isNetworking; @property(nonatomic,copy)NSString *str; - -#pragma mark --coreData -@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext; -@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel; -@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator; - -- (void)saveContext; -- (NSURL *)applicationDocumentsDirectory; @end diff --git a/iOSStudy/iOSStudy/AppDelegate.m b/iOSStudy/iOSStudy/AppDelegate.m index 5ae2306..a46c5a5 100644 --- a/iOSStudy/iOSStudy/AppDelegate.m +++ b/iOSStudy/iOSStudy/AppDelegate.m @@ -88,86 +88,4 @@ - (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } - - -#pragma mark - Core Data stack - -@synthesize managedObjectContext = _managedObjectContext; -@synthesize managedObjectModel = _managedObjectModel; -@synthesize persistentStoreCoordinator = _persistentStoreCoordinator; - -- (NSURL *)applicationDocumentsDirectory { - // The directory the application uses to store the Core Data store file. This code uses a directory named "---.test" in the application's documents directory. - return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; -} - -- (NSManagedObjectModel *)managedObjectModel { - // The managed object model for the application. It is a fatal error for the application not to be able to find and load its model. - if (_managedObjectModel != nil) { - return _managedObjectModel; - } - NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"iOSStudy" withExtension:@"momd"]; - _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL]; - return _managedObjectModel; -} - -- (NSPersistentStoreCoordinator *)persistentStoreCoordinator { - // The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it. - if (_persistentStoreCoordinator != nil) { - return _persistentStoreCoordinator; - } - - // Create the coordinator and store - - _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; - NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"iOSStudy.sqlite"]; - NSError *error = nil; - NSString *failureReason = @"There was an error creating or loading the application's saved data."; - if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) { - // Report any error we got. - NSMutableDictionary *dict = [NSMutableDictionary dictionary]; - dict[NSLocalizedDescriptionKey] = @"Failed to initialize the application's saved data"; - dict[NSLocalizedFailureReasonErrorKey] = failureReason; - dict[NSUnderlyingErrorKey] = error; - error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict]; - // Replace this with code to handle the error appropriately. - // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. - NSLog(@"Unresolved error %@, %@", error, [error userInfo]); - abort(); - } - - return _persistentStoreCoordinator; -} - - -- (NSManagedObjectContext *)managedObjectContext { - // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) - if (_managedObjectContext != nil) { - return _managedObjectContext; - } - - NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator]; - if (!coordinator) { - return nil; - } - _managedObjectContext = [[NSManagedObjectContext alloc] init]; - [_managedObjectContext setPersistentStoreCoordinator:coordinator]; - return _managedObjectContext; -} - -#pragma mark - Core Data Saving support - -- (void)saveContext { - NSManagedObjectContext *managedObjectContext = self.managedObjectContext; - if (managedObjectContext != nil) { - NSError *error = nil; - if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) { - // Replace this implementation with code to handle the error appropriately. - // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. - NSLog(@"Unresolved error %@, %@", error, [error userInfo]); - abort(); - } - } -} - @end diff --git a/iOSStudy/iOSStudy/Constants .h b/iOSStudy/iOSStudy/Constants .h index c7e120e..e7eae59 100644 --- a/iOSStudy/iOSStudy/Constants .h +++ b/iOSStudy/iOSStudy/Constants .h @@ -18,5 +18,5 @@ #define PATH_OF_DOCUMENT [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] -#define CD_FAVOURITE_BEAN @"FavouriteBean" + #endif diff --git a/iOSStudy/iOSStudy/FirstViewController.m b/iOSStudy/iOSStudy/FirstViewController.m index 619faa1..db38b67 100644 --- a/iOSStudy/iOSStudy/FirstViewController.m +++ b/iOSStudy/iOSStudy/FirstViewController.m @@ -167,16 +167,10 @@ - (void)swipeableTableViewCellDidEndScrolling:(SWTableViewCell *)cell{ - (void)swipeableTableViewCell:(SWTableViewCell *)cell didTriggerRightUtilityButtonWithIndex:(NSInteger)index{ switch (index) { case 0: - NSLog(@"不喜欢"); - { - NSIndexPath *indexPath = [_tableView indexPathForCell:cell]; - [_viewModel saveFavourite:indexPath]; - - } + NSLog(@"unlove"); break; case 1: - NSLog(@"收藏"); - + NSLog(@"love"); break; default: diff --git a/iOSStudy/iOSStudy/ViewModel/FirstViewControllerViewModel.h b/iOSStudy/iOSStudy/ViewModel/FirstViewControllerViewModel.h index cfbfc30..02a6ec6 100644 --- a/iOSStudy/iOSStudy/ViewModel/FirstViewControllerViewModel.h +++ b/iOSStudy/iOSStudy/ViewModel/FirstViewControllerViewModel.h @@ -41,11 +41,4 @@ typedef void (^modelNetWorking)(BOOL isNetWorking); * @return cell的数据 */ -(BlogBean*)getBlogBean:(NSIndexPath *)indexPath; - - - - --(void)saveFavourite:(NSIndexPath*)indexPath; - --(BOOL)isExistURL:(NSString*)url; @end diff --git a/iOSStudy/iOSStudy/ViewModel/FirstViewControllerViewModel.m b/iOSStudy/iOSStudy/ViewModel/FirstViewControllerViewModel.m index 2426222..91ba54e 100644 --- a/iOSStudy/iOSStudy/ViewModel/FirstViewControllerViewModel.m +++ b/iOSStudy/iOSStudy/ViewModel/FirstViewControllerViewModel.m @@ -8,7 +8,6 @@ #import "FirstViewControllerViewModel.h" #import "FavouriteBean.h" -#import "Constants .h" @implementation FirstViewControllerViewModel - (instancetype)init { @@ -35,13 +34,6 @@ -(BlogBean*)getBlogBean:(NSIndexPath *)indexPath{ -(void)getDate:(modelSuccess)modelDataSuccess modelDataErrors:(modelError)modelDataErrors modelDataIsNetworking:(modelNetWorking)modelDataIsNetWoring{ - _array = [[self getPersistenceData]copy]; - - modelDataSuccess(); - - - - // [NetWorkTools postHttp:Address_blogs success:^(AFHTTPRequestOperation *operation, id responseObject) { @@ -51,6 +43,7 @@ -(void)getDate:(modelSuccess)modelDataSuccess modelDataErrors:(modelError)modelD //将JSON数据和Model的属性进行绑定 + NSArray *arr = [MTLJSONAdapter modelsOfClass:[BlogBean class] fromJSONArray:dic error:nil]; for (BlogBean *bean in arr) { @@ -61,8 +54,6 @@ -(void)getDate:(modelSuccess)modelDataSuccess modelDataErrors:(modelError)modelD modelDataSuccess(); - - [self persistenceData]; } error:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); @@ -80,135 +71,7 @@ -(void)getDate:(modelSuccess)modelDataSuccess modelDataErrors:(modelError)modelD } -#pragma mark -- 得到持久化的数据 --(NSArray*)getPersistenceData{ - NSError *error; - NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; - NSEntityDescription *entity = [NSEntityDescription - entityForName:CD_FAVOURITE_BEAN inManagedObjectContext:SharedApp.managedObjectContext]; - [fetchRequest setEntity:entity]; - - NSArray *fetchedObjects = [SharedApp.managedObjectContext executeFetchRequest:fetchRequest error:&error]; - - NSMutableArray *blogArr = [NSMutableArray arrayWithCapacity:10]; - - - for (NSManagedObject *info in fetchedObjects) { - - BlogBean *blogBean = [[BlogBean alloc]init]; - blogBean.title = [info valueForKey:@"title"]; - blogBean.subTitle=[info valueForKey:@"subtitle"]; - blogBean.image = [info valueForKey:@"image_name"]; - blogBean.url = [info valueForKey:@"url"]; - [blogArr addObject:blogBean]; - - } - - return blogArr; - -} - -//持久化所有数据 --(void)persistenceData{ - - for (BlogBean *bean in _array) { - - if ([self isExistURL:bean.url]) { - NSLog(@"已经有这条数据了"); - }else{ - FavouriteBean *favouriteBean = (FavouriteBean*)[NSEntityDescription insertNewObjectForEntityForName:CD_FAVOURITE_BEAN inManagedObjectContext:SharedApp.managedObjectContext]; - - - favouriteBean.title = bean.title; - favouriteBean.subtitle = bean.subTitle; - favouriteBean.image_name = bean.image; - favouriteBean.url= bean.url; - favouriteBean.type =@"0"; - - - NSError *error; - BOOL isSaveSuccess =[SharedApp.managedObjectContext save:&error]; - if (!isSaveSuccess) { - NSLog(@"Error: %@,%@",error,[error userInfo]); - }else { - NSLog(@"Save successful favourite!"); - } - - - } - - } - -} - - --(void)saveFavourite:(NSIndexPath*)indexPath{ - - BlogBean *loveBean = _array[indexPath.row]; - - - if ([self isExistURL:loveBean.url]) { - NSLog(@"已经有这条数据了"); - }else{ - FavouriteBean *favouriteBean = (FavouriteBean*)[NSEntityDescription insertNewObjectForEntityForName:CD_FAVOURITE_BEAN inManagedObjectContext:SharedApp.managedObjectContext]; - - - favouriteBean.title = loveBean.title; - favouriteBean.subtitle = loveBean.subTitle; - favouriteBean.image_name = loveBean.image; - favouriteBean.url= loveBean.url; - favouriteBean.type =@"1"; - - - - NSError *error; - BOOL isSaveSuccess =[SharedApp.managedObjectContext save:&error]; - if (!isSaveSuccess) { - NSLog(@"Error: %@,%@",error,[error userInfo]); - }else { - NSLog(@"Save successful favourite!"); - } - - - } - - -} - -//检查这个URL在数据库是否存在 --(BOOL)isExistURL:(NSString*)url{ - NSError *error; - NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; - NSEntityDescription *entity = [NSEntityDescription - entityForName:CD_FAVOURITE_BEAN inManagedObjectContext:SharedApp.managedObjectContext]; - [fetchRequest setEntity:entity]; - - NSArray *fetchedObjects = [SharedApp.managedObjectContext executeFetchRequest:fetchRequest error:&error]; - - - for (NSManagedObject *info in fetchedObjects) { - NSLog(@"Name: %@", [info valueForKey:@"title"]); - /* - NSManagedObject *details = [info valueForKey:@"details"]; - NSLog(@"Zip: %@", [details valueForKey:@"zip"]); - */ - } - - - NSPredicate *titlePredicate = [NSPredicate predicateWithFormat:@"url = %@", url]; - - - NSArray *resultArr = [fetchedObjects filteredArrayUsingPredicate:titlePredicate]; - if (resultArr.count!=0) { - NSLog(@"111%lu",resultArr.count); - return YES; - }else{ - NSLog(@"222%lu",resultArr.count); - return NO; - } - - -} +//-(void)saveFavourite @end diff --git a/iOSStudy/iOSStudy/iOSStudy.xcdatamodeld/iOSStudy.xcdatamodel/contents b/iOSStudy/iOSStudy/iOSStudy.xcdatamodeld/iOSStudy.xcdatamodel/contents deleted file mode 100644 index 6944e80..0000000 --- a/iOSStudy/iOSStudy/iOSStudy.xcdatamodeld/iOSStudy.xcdatamodel/contents +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - - - - - \ No newline at end of file diff --git a/iOSStudyJsonData/versions.json b/iOSStudyJsonData/versions.json deleted file mode 100644 index 925ed7b..0000000 --- a/iOSStudyJsonData/versions.json +++ /dev/null @@ -1,2 +0,0 @@ -[{"url":"https://raw.githubusercontent.com/chenguandong/iOSStudy/master/iOSStudyJsonData/blogs.json","urlVersion":"1"},{"url":"https://raw.githubusercontent.com/chenguandong/iOSStudy/master/iOSStudyJsonData/webs.json","urlVersion":"1"},{"url":"https://raw.githubusercontent.com/chenguandong/iOSStudy/master/iOSStudyJsonData/videos.json","urlVersion":"1"}] -