Skip to content
This repository has been archived by the owner on Jan 4, 2024. It is now read-only.

Commit

Permalink
Support Theming the App (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
ExTBH authored Nov 10, 2022
1 parent 64d8a33 commit 6b1b456
Show file tree
Hide file tree
Showing 15 changed files with 257 additions and 72 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
Theos/packages/
Theos/.theos
.theos
.cache/
compile_commands.json
6 changes: 6 additions & 0 deletions Theos/Classes/Exts.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#include <Foundation/NSUserDefaults.h>
#import <UIKit/UIKit.h>

@interface UIImage (Scale)
+ (UIImage *)resizeImageFromImage:(UIImage *)image withSize:(CGSize)size;
@end

@interface NSUserDefaults (Colors)
- (void)setColor:(UIColor*)color forKey:(NSString*)key;
- (UIColor*)colorForKey:(NSString*)key;
@end
19 changes: 19 additions & 0 deletions Theos/Classes/Exts.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#import "Exts.h"
#include <UIKit/UIColor.h>
#include <Foundation/NSKeyedArchiver.h>

@implementation UIImage (Scale)
// Resize images by luki120 @ Theos discord
Expand All @@ -24,4 +26,21 @@ + (UIImage *)resizeImageFromImage:(UIImage *)image withSize:(CGSize)size {
return [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];

}
@end

@implementation NSUserDefaults (Colors)
- (void)setColor:(UIColor*)color forKey:(NSString*)key{
NSData *archivedColor = [NSKeyedArchiver archivedDataWithRootObject:color
requiringSecureCoding:NO
error:nil];
[self setObject:archivedColor forKey:key];
}
- (UIColor*)colorForKey:(NSString*)key{
NSData *archivedColor = [self objectForKey:key];
if (archivedColor){
UIColor *color = [NSKeyedUnarchiver unarchivedObjectOfClass:[UIColor class] fromData:archivedColor error:nil];
return color;
}
return nil;
}
@end
1 change: 0 additions & 1 deletion Theos/Classes/JDEButtons.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#import <UIKit/UIKit.h>

@interface JDEButtons : NSObject
- (UIButton*)boldButton;
- (UIButton*)buttonWithImageNamed:(NSString*)image;
@end
14 changes: 0 additions & 14 deletions Theos/Classes/JDEButtons.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,6 @@


@implementation JDEButtons

-(UIButton*)boldButton{
//init
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.translatesAutoresizingMaskIntoConstraints = NO;
//colors
[btn setTintColor:[UIColor whiteColor]];
//sizes

btn.titleLabel.font = [UIFont boldSystemFontOfSize:20];


return btn;
}
- (UIButton*)buttonWithImageNamed:(NSString*)image{
//init
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
Expand Down
4 changes: 2 additions & 2 deletions Theos/Classes/JDEMapView.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ - (void)viewDidLoad {

//Press detection
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
lpgr.minimumPressDuration = 1;
lpgr.minimumPressDuration = .5;
[_mapView addGestureRecognizer:lpgr];
//add pin at 0, 0
_pin = [MKPointAnnotation new];
Expand All @@ -46,7 +46,7 @@ - (void)viewDidLoad {
[_btn addTarget:self action:@selector(didTapSwitchButton:) forControlEvents:UIControlEventTouchUpInside];
_btn.enabled = NO;
_btn.translatesAutoresizingMaskIntoConstraints = NO;
_btn.backgroundColor = [UIColor colorWithRed: 0.98 green: 0.49 blue: 0.05 alpha: 1.00];
_btn.backgroundColor = [UIColor colorNamed:@"mainColor"];
[_btn setTitle:[[JDESettingsManager sharedInstance] localizedStringForKey:@"map_change_location_hint"] forState:UIControlStateNormal];
_btn.titleLabel.font = [UIFont boldSystemFontOfSize:15];
_btn.tintColor = UIColor.labelColor;
Expand Down
1 change: 1 addition & 0 deletions Theos/Classes/JDESettingsManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#import <objc/runtime.h>

@interface JDESettingsManager : NSObject
@property (strong, nonatomic) NSUserDefaults *tweakSettings;
@property (strong, nonatomic, readonly) NSString *logFile;
@property (nonatomic, readonly) BOOL logFileExists;

Expand Down
11 changes: 3 additions & 8 deletions Theos/Classes/JDESettingsManager.m
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#import "JDESettingsManager.h"
#include <Foundation/NSString.h>
#include <Foundation/NSObjCRuntime.h>
#include <UIKit/UIColor.h>

#define suiteName "dev.extbh.jodelemproved"

@interface JDESettingsManager()
@property (strong, nonatomic) NSUserDefaults *tweakSettings;
@property (strong, nonatomic) NSBundle *bundle;
@property (strong, nonatomic, readwrite) NSString *logFile;
@property (nonatomic, readwrite) BOOL logFileExists;
Expand Down Expand Up @@ -83,13 +85,6 @@ - (NSDictionary*)cellInfoForPath:(NSIndexPath*)indexPath{
@"desc": [self localizedStringForKey:@"screenshot_protection_desc"],
@"image": [UIImage systemImageNamed:@"bell.slash"]
}; break;
case 7:
info = @{
@"title": [self localizedStringForKey:@"tracking_protection"],
@"desc": [self localizedStringForKey:@"tracking_protection_desc"],
@"image": [UIImage systemImageNamed:@"person.crop.circle.badge.xmark"],
@"disabled": @1
}; break;
}
break;
case 1:
Expand Down
30 changes: 30 additions & 0 deletions Theos/Classes/ThemingViewController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#import <UIKit/UIKit.h>
#import "Exts.h"
@interface ThemingViewController : UIViewController
@end

@interface ColorCell : UITableViewCell
@property (nonatomic, strong) UIButton *colorButton;
@end


#define main "mainColor"
#define secondary "customLightGrayColor"
#define user "meColor"
#define userDot "channelNotification"
#define channel "channelColor"
#define channelDot "declineColor" // channel and notification Dots
#define notification "notificationColor"
#define pollCell "pollBgColor"


typedef NS_ENUM(NSUInteger, ThemeOption){
ThemeOptionMainColor,
ThemeOptionSecondaryColor,
ThemeOptionUserSectionColor,
ThemeOptionChannelsSectionColor,
ThemeOptionNotificationsSectionColor,
ThemeOptionPollCellsBackgroundColor

};

138 changes: 138 additions & 0 deletions Theos/Classes/ThemingViewController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
#import "ThemingViewController.h"
#include "Classes/JDESettingsManager.h"


#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending) // https://stackoverflow.com/a/5337804



static NSDictionary<NSNumber*, NSString*> *_ThemeOptionKeysDict(){
return @{
@(ThemeOptionMainColor): @"mainColor",
@(ThemeOptionSecondaryColor): @"customLightGrayColor",
@(ThemeOptionUserSectionColor): @"meColor",
@(ThemeOptionChannelsSectionColor): @"channelColor",
@(ThemeOptionNotificationsSectionColor): @"notificationColor",
@(ThemeOptionPollCellsBackgroundColor): @"pollBgColor"
};
}

UIColor *colorForKey(NSString *key){
UIColor *color = [[JDESettingsManager sharedInstance].tweakSettings colorForKey:key];
if (!color){
return [UIColor colorNamed:key];
}
return color;
}


@interface ColorCell()
@end

@implementation ColorCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if(self){
self.colorButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.accessoryView = self.colorButton;
self.colorButton.frame = CGRectMake(0, 0, 30, 30);
self.colorButton.layer.cornerRadius = .5 * self.colorButton.frame.size.width;
self.colorButton.clipsToBounds = YES;
self.colorButton.layer.borderWidth = 3;
self.colorButton.layer.borderColor = UIColor.systemGrayColor.CGColor;
}
return self;
}

@end

@interface ThemingViewController()<UITableViewDelegate, UITableViewDataSource, UIColorPickerViewControllerDelegate>
@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) NSIndexPath *indexPathForColorPicker;
@end

@implementation ThemingViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = UIColor.systemBackgroundColor;
self.title = @"Theming";

self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleInsetGrouped];
self.tableView.delegate = self;
self.tableView.dataSource = self;

[self.view addSubview:self.tableView];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 33;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
label.text = @"Unavailable for below iOS 13 :)";
label.textColor = UIColor.secondaryLabelColor;
return label;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _ThemeOptionKeysDict().allKeys.count +1;
}

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

if(indexPath.row == [tableView numberOfRowsInSection:0] -1){
UITableViewCell *resetCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

resetCell.textLabel.text = @"Reset";
resetCell.textLabel.textColor = UIColor.systemRedColor;
resetCell.textLabel.textAlignment = NSTextAlignmentCenter;

return resetCell;
}

ColorCell *cell = [tableView dequeueReusableCellWithIdentifier:@"colorCell"];
if (!cell){
cell = [[ColorCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"colorCell"];
}

cell.selectionStyle = UITableViewCellSelectionStyleNone;
NSString *key = _ThemeOptionKeysDict()[@(indexPath.row)];
cell.textLabel.text = key;
cell.colorButton.backgroundColor = colorForKey(key);
[cell.colorButton addTarget:self action:@selector(tappedColor:) forControlEvents:UIControlEventTouchUpInside];



return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row == [tableView numberOfRowsInSection:0] -1){
for(NSString *color in _ThemeOptionKeysDict().allValues){
[[JDESettingsManager sharedInstance].tweakSettings removeObjectForKey:color];
}
[tableView reloadData];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)tappedColor:(UIButton*)sender{
CGPoint buttonPos = [sender convertPoint:CGPointZero toView:self.tableView];
self.indexPathForColorPicker = [self.tableView indexPathForRowAtPoint:buttonPos];

if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"14")){
UIColorPickerViewController *colorPicker = [UIColorPickerViewController new];
colorPicker.delegate = self;
[self presentViewController:colorPicker animated:YES completion:nil];
}
}

- (void)colorPickerViewControllerDidSelectColor:(UIColorPickerViewController *)viewController{
ColorCell *cell = [self.tableView cellForRowAtIndexPath:self.indexPathForColorPicker];
cell.colorButton.backgroundColor = viewController.selectedColor;
[[JDESettingsManager sharedInstance].tweakSettings setColor:viewController.selectedColor
forKey:_ThemeOptionKeysDict()[@(self.indexPathForColorPicker.row)]];
}
@end

1 change: 1 addition & 0 deletions Theos/JDEViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#import "Classes/JDESettingsManager.h"
#import "Classes/JDEMapView.h"
#import "Classes/Exts.h"
#import "Classes/ThemingViewController.h"


@interface JDEViewController : UIViewController
Expand Down
Loading

1 comment on commit 6b1b456

@com20112233
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.