Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/PojavLauncher_iOS
Path: blob/main/Natives/LauncherNewsViewController.m
589 views
1
#import <WebKit/WebKit.h>
2
#import "LauncherMenuViewController.h"
3
#import "LauncherNewsViewController.h"
4
#import "LauncherPreferences.h"
5
#import "utils.h"
6
7
@interface LauncherNewsViewController()<WKNavigationDelegate>
8
@end
9
10
@implementation LauncherNewsViewController
11
WKWebView *webView;
12
UIEdgeInsets insets;
13
14
- (id)init {
15
self = [super init];
16
self.title = localize(@"News", nil);
17
return self;
18
}
19
20
- (NSString *)imageName {
21
return @"MenuNews";
22
}
23
24
- (void)viewDidLoad
25
{
26
[super viewDidLoad];
27
28
CGSize size = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
29
insets = UIApplication.sharedApplication.windows.firstObject.safeAreaInsets;
30
31
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://pojavlauncherteam.github.io/changelogs/IOS.html"]];
32
33
WKWebViewConfiguration *webConfig = [[WKWebViewConfiguration alloc] init];
34
webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:webConfig];
35
webView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
36
webView.translatesAutoresizingMaskIntoConstraints = NO;
37
webView.navigationDelegate = self;
38
webView.opaque = NO;
39
[self adjustWebViewForSize:size];
40
webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
41
NSString *javascript = @"var meta = document.createElement('meta');meta.setAttribute('name', 'viewport');meta.setAttribute('content', 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no');document.getElementsByTagName('head')[0].appendChild(meta);";
42
WKUserScript *nozoom = [[WKUserScript alloc] initWithSource:javascript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
43
[webView.configuration.userContentController addUserScript:nozoom];
44
[webView.scrollView setShowsHorizontalScrollIndicator:NO];
45
[webView loadRequest:request];
46
[self.view addSubview:webView];
47
48
if(!isJailbroken && getPrefBool(@"warnings.limited_ram_warn") && (roundf(NSProcessInfo.processInfo.physicalMemory / 0x1000000) < 3900)) {
49
// "This device has a limited amount of memory available."
50
[self showWarningAlert:@"limited_ram" hasPreference:YES];
51
}
52
53
self.navigationItem.leftBarButtonItem = self.splitViewController.displayModeButtonItem;
54
self.navigationItem.rightBarButtonItem = [sidebarViewController drawAccountButton];
55
self.navigationItem.leftItemsSupplementBackButton = true;
56
}
57
58
-(void)showWarningAlert:(NSString *)key hasPreference:(BOOL)isPreferenced {
59
UIAlertController *warning = [UIAlertController
60
alertControllerWithTitle:localize([NSString stringWithFormat:@"login.warn.title.%@", key], nil)
61
message:localize([NSString stringWithFormat:@"login.warn.title.%@", key], nil)
62
preferredStyle:UIAlertControllerStyleAlert];
63
64
UIAlertAction *action;
65
if(isPreferenced) {
66
action = [UIAlertAction actionWithTitle:localize(@"OK", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction * action) {
67
setPrefBool([NSString stringWithFormat:@"warnings.%@_warn", key], NO);
68
}];
69
} else {
70
action = [UIAlertAction actionWithTitle:localize(@"OK", nil) style:UIAlertActionStyleCancel handler:nil];
71
}
72
warning.popoverPresentationController.sourceView = self.view;
73
warning.popoverPresentationController.sourceRect = self.view.bounds;
74
[warning addAction:action];
75
[self presentViewController:warning animated:YES completion:nil];
76
}
77
78
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
79
{
80
if (scrollView.contentOffset.x > 0)
81
scrollView.contentOffset = CGPointMake(0, scrollView.contentOffset.y);
82
}
83
84
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
85
{
86
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
87
[self adjustWebViewForSize:size];
88
}
89
90
- (void)adjustWebViewForSize:(CGSize)size {
91
BOOL isPortrait = size.height > size.width;
92
if (isPortrait) {
93
webView.scrollView.contentInset = UIEdgeInsetsMake(self.navigationController.navigationBar.frame.size.height + insets.top, 0, self.navigationController.navigationBar.frame.size.height + insets.bottom, 0);
94
} else {
95
webView.scrollView.contentInset = UIEdgeInsetsMake(self.navigationController.navigationBar.frame.size.height, 0, self.navigationController.navigationBar.frame.size.height, 0);
96
}
97
}
98
99
- (void)webView:(WKWebView *)webView
100
decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction
101
decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
102
if (navigationAction.navigationType == WKNavigationTypeLinkActivated) {
103
openLink(self, navigationAction.request.URL);
104
decisionHandler(WKNavigationActionPolicyCancel);
105
return;
106
}
107
decisionHandler(WKNavigationActionPolicyAllow);
108
}
109
110
@end
111
112