Skip to content
This repository has been archived by the owner on Feb 15, 2023. It is now read-only.

Commit

Permalink
Better algorithms, more error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelomer committed Apr 9, 2019
1 parent 3fdb03e commit 05da12c
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 19 deletions.
3 changes: 2 additions & 1 deletion ModernDepictions/ModernHome/FeaturedBannersView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ - (instancetype)initWithPackages:(NSArray *)packages bannerLimit:(NSUInteger)ban
// V:|-16-[image(==148)]-16-|
// H:[previous]-16-[image(==263)]
// Radius: 10
for (NSUInteger i = 0; i < bannerLimit; i++) {
NSLog(@"Packages: %@", packages);
for (NSUInteger i = 0; i < min(packages.count, bannerLimit); i++) {
NSDictionary *packageInfo = packages[i];
FeaturedBannerView *banner = [FeaturedBannerView bannerWithPackageInfo:packageInfo];
[banner addTarget:self action:@selector(handleBannerTap:) forControlEvents:UIControlEventTouchUpInside];
Expand Down
7 changes: 7 additions & 0 deletions ModernDepictions/ModernHome/FeaturedDummyPackage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#import <Foundation/Foundation.h>

@interface FeaturedDummyPackage : NSObject
@property (nonatomic, readonly) NSString *identifier;
@property (nonatomic, readonly) NSString *name;
- (instancetype)initWithPackageName:(NSString *)name identifier:(NSString *)identifier;
@end
46 changes: 46 additions & 0 deletions ModernDepictions/ModernHome/FeaturedDummyPackage.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#import "FeaturedDummyPackage.h"

@implementation FeaturedDummyPackage

static UIImage *qmarkIcon;

+ (void)initialize {
if ([self class] == [FeaturedDummyPackage class]) {
qmarkIcon = [UIImage imageNamed:@"unknown"];
}
}

- (void)parse {}

- (NSString *)mode {
return nil;
}

- (UIImage *)icon {
return qmarkIcon;
}

- (NSString *)id {
return _identifier;
}

- (id)author {
return nil;
}

- (BOOL)uninstalled {
return YES;
}

- (NSString *)shortDescription {
return @"This package is not available in your sources.";
}

- (instancetype)initWithPackageName:(NSString *)name identifier:(NSString *)identifier {
self = [super init];
_name = name;
_identifier = identifier;
return self;
}

@end
1 change: 1 addition & 0 deletions ModernDepictions/ModernHome/ModernHomeController.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Cydia *_cydiaDelegate;
Database *database;
UIBarButtonItem *refreshButton;
NSMutableDictionary *dummies;
NSArray *cells; // Contains both actual cells and dictionaries
}
@property (nonatomic, assign, setter=setDelegate:, getter=delegate) Cydia *cydiaDelegate;
Expand Down
46 changes: 28 additions & 18 deletions ModernDepictions/ModernHome/ModernHomeController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
#import "FeaturedPackageCell.h"
#import "FeaturedBannersView.h"
#import "FeaturedHeaderView.h"
#import "FeaturedDummyPackage.h"
#import <Tweak/Tweak.h>

#define REFRESH_ALGORITHM_VERSION 1

@implementation ModernHomeController

- (instancetype)init {
Expand Down Expand Up @@ -33,9 +36,16 @@ - (void)viewDidLoad {
toDate:lastRefresh
options:0
];
NSNumber *lastRefreshedWith = [stddef objectForKey:@"ModernDepictionsLastRefreshVersion"];
shouldRefresh = (
![stddef objectForKey:@"ModernDepictionsCachedFeaturedPackages"] ||
([aWeekAfterRefresh compare:[NSDate date]] == NSOrderedAscending)
(
![stddef objectForKey:@"ModernDepictionsCachedFeaturedPackages"] ||
([aWeekAfterRefresh compare:[NSDate date]] == NSOrderedAscending)
) ||
(
![lastRefreshedWith isKindOfClass:[NSNumber class]] ||
[lastRefreshedWith unsignedLongValue] < REFRESH_ALGORITHM_VERSION
)
);
}
else {
Expand Down Expand Up @@ -74,10 +84,11 @@ - (void)loadFeaturedPackages {
}
NSLog(@"Featured packages: %@", featuredPackages);
NSMutableArray *chosenPackages = [NSMutableArray new];
for (int i = featuredPackages.count; i > max(0, featuredPackages.count - 9); i--) {
for (int i = featuredPackages.count; i > 0; i--) {
NSInteger index = arc4random_uniform(i);
[chosenPackages addObject:featuredPackages[index]];
[featuredPackages removeObjectAtIndex:index];
if ([chosenPackages count] >= 9) break;
}
NSLog(@"Chose packages: %@", chosenPackages);
[self createCellsFromPackages:chosenPackages];
Expand Down Expand Up @@ -112,7 +123,8 @@ - (void)refreshFeaturedPackages {
dispatch_sync(dispatch_get_main_queue(), ^{
self.title = [NSString stringWithFormat:@"%@...", source.name];
});
NSURL *featuredURL = [[NSURL URLWithString:source.rooturi] URLByAppendingPathComponent:@"sileo-featured.json"];
NSURL *rootURI = [NSURL URLWithString:source.rooturi];
NSURL *featuredURL = [rootURI URLByAppendingPathComponent:@"sileo-featured.json"];
if (!featuredURL) continue;
NSData *rawJSON = [NSData dataWithContentsOfURL:featuredURL];
NSLog(@"Featured URL: %@, Data: %@", featuredURL, rawJSON);
Expand All @@ -136,10 +148,11 @@ - (void)refreshFeaturedPackages {
if (data) banner[@"imageData"] = data;
}
}
finalDictionary[featuredURL.absoluteString] = [JSON copy];
finalDictionary[rootURI.absoluteString] = [JSON copy];
}
[stddef setObject:finalDictionary forKey:@"ModernDepictionsCachedFeaturedPackages"];
[stddef setObject:[NSDate date] forKey:@"ModernDepictionsLastRefreshDate"];
[stddef setObject:@(REFRESH_ALGORITHM_VERSION) forKey:@"ModernDepictionsLastRefreshVersion"];
dispatch_sync(dispatch_get_main_queue(), ^{
[self loadFeaturedPackages];
refreshButton.enabled = YES;
Expand Down Expand Up @@ -171,7 +184,16 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
else if ([object isKindOfClass:[NSDictionary class]]) {
cell = [tableView dequeueReusableCellWithIdentifier:@"Package"];
if (!cell) cell = [[FeaturedPackageCell alloc] initWithIconSize:65.0 centerText:YES reuseIdentifier:@"Package"];
((FeaturedPackageCell *)cell).package = [database packageWithName:cells[indexPath.row][@"package"]];
NSString *packageID = cells[indexPath.row][@"package"];
__kindof NSObject *package = [database packageWithName:packageID];
if (!package) {
if (!dummies) dummies = [NSMutableDictionary new];
if (!dummies[packageID]) {
dummies[packageID] = [[FeaturedDummyPackage alloc] initWithPackageName:cells[indexPath.row][@"title"] identifier:packageID];
}
package = dummies[packageID];
}
((FeaturedPackageCell *)cell).package = package;
}
else {
cell = (UITableViewCell *)object;
Expand All @@ -196,16 +218,4 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
}
}

- (void)didSelectPackage:(NSString *)packageID {
Package *package = [database packageWithName:packageID];
NSLog(@"Did select: %@", package);
[self.navigationController
pushViewController:[(Cydia *)[UIApplication sharedApplication]
pageForPackage:[package id]
withReferrer:ModernDepictionsGeneratePackageURL([package id])
]
animated:YES
];
}

@end
26 changes: 26 additions & 0 deletions Tweak/ModernHome.xm
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
#import <UIKit/UIKit.h>
#import <Headers/Headers.h>
#import <ModernDepictions/ModernHome/ModernHomeController.h>
#import "Tweak.h"

@interface HomeController : CyteViewController
@end

static void _logos_method$ModernHome$Shared$didSelectPackage$(UIViewController *self, SEL _cmd, NSString *packageID) {
Package *package = [[%c(Database) sharedInstance] packageWithName:packageID];
NSLog(@"Did select: %@", package);
[self.navigationController
pushViewController:[(Cydia *)[UIApplication sharedApplication]
pageForPackage:[package id]
withReferrer:ModernDepictionsGeneratePackageURL([package id])
]
animated:YES
];
}

%group ModernHome
%hook Cydia

Expand All @@ -17,6 +30,19 @@
%end
%end


void ModernDepictionsInitializeHome(void) {
%init(ModernHome);
class_addMethod(
objc_getClass("ModernHomeController"),
@selector(didSelectPackage:),
(IMP)_logos_method$ModernHome$Shared$didSelectPackage$,
"@:@"
);
class_addMethod(
objc_getClass("SectionController"),
@selector(didSelectPackage:),
(IMP)_logos_method$ModernHome$Shared$didSelectPackage$,
"@:@"
);
}

0 comments on commit 05da12c

Please sign in to comment.