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

Commit

Permalink
Refine URLs and numbers detection (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
ExTBH authored Dec 4, 2022
1 parent 4ac3570 commit f3f6dfd
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 9 deletions.
4 changes: 4 additions & 0 deletions Theos/Classes/Exts.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@

@interface UIView (FindUIViewController)
- (UIViewController *) firstAvailableUIViewController;
@end

@interface NSDataDetector (Shared)
+ (NSDataDetector*)sharedInstance;
@end
16 changes: 14 additions & 2 deletions Theos/Classes/Exts.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#import "Exts.h"
#include <UIKit/UIColor.h>
#include <Foundation/NSKeyedArchiver.h>

@implementation UIImage (Scale)
// Resize images by luki120 @ Theos discord
+ (UIImage *)resizeImageFromImage:(UIImage *)image withSize:(CGSize)size {
Expand Down Expand Up @@ -57,4 +55,18 @@ - (UIViewController *) firstAvailableUIViewController {
return nil;
}

@end

@implementation NSDataDetector (Shared)
+ (NSDataDetector *)sharedInstance {
static NSDataDetector *shared = nil;
static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{
shared = [self dataDetectorWithTypes:NSTextCheckingTypeLink|NSTextCheckingTypePhoneNumber error:nil];
});
return shared;

}

@end
36 changes: 29 additions & 7 deletions Theos/Tweak.x
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,8 @@
-(void)setAttributedText:(NSAttributedString *)arg1{
NSMutableAttributedString *newAttributedString = [[NSMutableAttributedString alloc] initWithAttributedString:arg1];
if([[JDESettingsManager sharedInstance] featureStateForTag:7]){
NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
[detector enumerateMatchesInString:arg1.string options:0 range:NSMakeRange(0, arg1.string.length)

[[NSDataDetector sharedInstance] enumerateMatchesInString:arg1.string options:0 range:NSMakeRange(0, arg1.string.length)
usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
attributes[ZSWTappableLabelTappableRegionAttributeName] = @YES;
Expand All @@ -355,11 +355,33 @@
- (void)tappableLabel:(ZSWTappableLabel *)tappableLabel tappedAtIndex:(NSInteger)idx withAttributes:(NSDictionary<NSAttributedStringKey, id> *)attributes{
if([[JDESettingsManager sharedInstance] featureStateForTag:7]){
NSTextCheckingResult *result = attributes[@"NSTextCheckingResult"];
if(result.resultType == NSTextCheckingTypeLink){
UIViewController *presentationController = [tappableLabel firstAvailableUIViewController];
if (presentationController != nil){
SFSafariViewController *inApp = [[SFSafariViewController alloc] initWithURL:result.URL];
[presentationController presentViewController:inApp animated:YES completion:nil];
UIViewController *presentationController = [tappableLabel firstAvailableUIViewController];
if (result){
switch(result.resultType){
case NSTextCheckingTypeLink:{
SFSafariViewController *inApp = [[SFSafariViewController alloc] initWithURL:result.URL];
[presentationController presentViewController:inApp animated:YES completion:nil];
}
break;
case NSTextCheckingTypePhoneNumber:{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Open with"
message:nil preferredStyle:UIAlertControllerStyleActionSheet];

UIAlertAction *cancelAlert = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];

UIAlertAction *whatsappAlert = [UIAlertAction actionWithTitle:[NSString stringWithFormat:@"Whatsapp: %@", result.phoneNumber]
style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
NSURL *whatsappURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://wa.me/%@", result.phoneNumber]];
[UIApplication.sharedApplication openURL:whatsappURL options:@{} completionHandler:nil];
}];

[alert addAction:cancelAlert];
[alert addAction:whatsappAlert];
[presentationController presentViewController:alert animated:YES completion:nil];
}
break;
default:
break;
}
}
}
Expand Down

0 comments on commit f3f6dfd

Please sign in to comment.