Path: blob/main/Natives/LauncherNewsViewController.m
589 views
#import <WebKit/WebKit.h>1#import "LauncherMenuViewController.h"2#import "LauncherNewsViewController.h"3#import "LauncherPreferences.h"4#import "utils.h"56@interface LauncherNewsViewController()<WKNavigationDelegate>7@end89@implementation LauncherNewsViewController10WKWebView *webView;11UIEdgeInsets insets;1213- (id)init {14self = [super init];15self.title = localize(@"News", nil);16return self;17}1819- (NSString *)imageName {20return @"MenuNews";21}2223- (void)viewDidLoad24{25[super viewDidLoad];2627CGSize size = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);28insets = UIApplication.sharedApplication.windows.firstObject.safeAreaInsets;2930NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://pojavlauncherteam.github.io/changelogs/IOS.html"]];3132WKWebViewConfiguration *webConfig = [[WKWebViewConfiguration alloc] init];33webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:webConfig];34webView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;35webView.translatesAutoresizingMaskIntoConstraints = NO;36webView.navigationDelegate = self;37webView.opaque = NO;38[self adjustWebViewForSize:size];39webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;40NSString *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);";41WKUserScript *nozoom = [[WKUserScript alloc] initWithSource:javascript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];42[webView.configuration.userContentController addUserScript:nozoom];43[webView.scrollView setShowsHorizontalScrollIndicator:NO];44[webView loadRequest:request];45[self.view addSubview:webView];4647if(!isJailbroken && getPrefBool(@"warnings.limited_ram_warn") && (roundf(NSProcessInfo.processInfo.physicalMemory / 0x1000000) < 3900)) {48// "This device has a limited amount of memory available."49[self showWarningAlert:@"limited_ram" hasPreference:YES];50}5152self.navigationItem.leftBarButtonItem = self.splitViewController.displayModeButtonItem;53self.navigationItem.rightBarButtonItem = [sidebarViewController drawAccountButton];54self.navigationItem.leftItemsSupplementBackButton = true;55}5657-(void)showWarningAlert:(NSString *)key hasPreference:(BOOL)isPreferenced {58UIAlertController *warning = [UIAlertController59alertControllerWithTitle:localize([NSString stringWithFormat:@"login.warn.title.%@", key], nil)60message:localize([NSString stringWithFormat:@"login.warn.title.%@", key], nil)61preferredStyle:UIAlertControllerStyleAlert];6263UIAlertAction *action;64if(isPreferenced) {65action = [UIAlertAction actionWithTitle:localize(@"OK", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction * action) {66setPrefBool([NSString stringWithFormat:@"warnings.%@_warn", key], NO);67}];68} else {69action = [UIAlertAction actionWithTitle:localize(@"OK", nil) style:UIAlertActionStyleCancel handler:nil];70}71warning.popoverPresentationController.sourceView = self.view;72warning.popoverPresentationController.sourceRect = self.view.bounds;73[warning addAction:action];74[self presentViewController:warning animated:YES completion:nil];75}7677-(void)scrollViewDidScroll:(UIScrollView *)scrollView78{79if (scrollView.contentOffset.x > 0)80scrollView.contentOffset = CGPointMake(0, scrollView.contentOffset.y);81}8283- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator84{85[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];86[self adjustWebViewForSize:size];87}8889- (void)adjustWebViewForSize:(CGSize)size {90BOOL isPortrait = size.height > size.width;91if (isPortrait) {92webView.scrollView.contentInset = UIEdgeInsetsMake(self.navigationController.navigationBar.frame.size.height + insets.top, 0, self.navigationController.navigationBar.frame.size.height + insets.bottom, 0);93} else {94webView.scrollView.contentInset = UIEdgeInsetsMake(self.navigationController.navigationBar.frame.size.height, 0, self.navigationController.navigationBar.frame.size.height, 0);95}96}9798- (void)webView:(WKWebView *)webView99decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction100decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {101if (navigationAction.navigationType == WKNavigationTypeLinkActivated) {102openLink(self, navigationAction.request.URL);103decisionHandler(WKNavigationActionPolicyCancel);104return;105}106decisionHandler(WKNavigationActionPolicyAllow);107}108109@end110111112