Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(643)

Side by Side Diff: ios/chrome/content_widget_extension/content_widget_view_controller.mm

Issue 2927803002: Add the new content widget including localised title. (Closed)
Patch Set: Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #import "ios/chrome/content_widget_extension/content_widget_view_controller.h"
6
7 #import <NotificationCenter/NotificationCenter.h>
8
9 #include "base/ios/ios_util.h"
10 #include "base/mac/foundation_util.h"
11 #include "base/strings/sys_string_conversions.h"
12 #include "ios/chrome/common/app_group/app_group_constants.h"
13 #include "ios/chrome/content_widget_extension/content_widget_view.h"
14
15 #if !defined(__has_feature) || !__has_feature(objc_arc)
16 #error "This file requires ARC support."
17 #endif
18
19 namespace {
20 // Using GURL in the extension is not wanted as it includes ICU which makes the
21 // extension binary much larger; therefore, ios/chrome/common/x_callback_url.h
22 // cannot be used. This class makes a very basic use of x-callback-url, so no
23 // full implementation is required.
24 NSString* const kXCallbackURLHost = @"x-callback-url";
25 } // namespace
26
27 @interface ContentWidgetViewController ()
28 @property(nonatomic, weak) ContentWidgetView* widgetView;
29
30 // Updates the widget with latest data. Returns whether any visual updates
31 // occured.
32 - (BOOL)updateWidget;
33 // Opens the main application with the given |URL|.
34 - (void)openAppWithURL:(NSString*)URL;
35 @end
36
37 @implementation ContentWidgetViewController
38
39 @synthesize widgetView = _widgetView;
40
41 #pragma mark - UIViewController
42
43 - (void)viewDidLoad {
44 [super viewDidLoad];
45
46 // A local variable is necessary here as the property is declared weak and the
47 // object would be deallocated before being retained by the addSubview call.
48 ContentWidgetView* widgetView = [[ContentWidgetView alloc] init];
49 self.widgetView = widgetView;
50 [self.view addSubview:self.widgetView];
51
52 if (base::ios::IsRunningOnIOS10OrLater()) {
53 self.extensionContext.widgetLargestAvailableDisplayMode =
54 NCWidgetDisplayModeExpanded;
55 }
56
57 self.widgetView.translatesAutoresizingMaskIntoConstraints = NO;
58 [NSLayoutConstraint activateConstraints:@[
59 [self.view.leadingAnchor
60 constraintEqualToAnchor:self.widgetView.leadingAnchor],
61 [self.view.trailingAnchor
62 constraintEqualToAnchor:self.widgetView.trailingAnchor],
63 [self.view.topAnchor constraintEqualToAnchor:self.widgetView.topAnchor],
64 [self.view.bottomAnchor
65 constraintEqualToAnchor:self.widgetView.bottomAnchor]
66 ]];
67 }
68
69 - (void)viewWillAppear:(BOOL)animated {
70 [super viewWillAppear:animated];
71 [self updateWidget];
72 }
73
74 - (void)widgetPerformUpdateWithCompletionHandler:
75 (void (^)(NCUpdateResult))completionHandler {
76 completionHandler([self updateWidget] ? NCUpdateResultNewData
77 : NCUpdateResultNoData);
78 }
79
80 - (BOOL)updateWidget {
marq (ping after 24h) 2017/06/08 08:49:56 This belongs in the 'internal' section, below.
lody 2017/06/08 09:19:57 Done.
81 return NO;
marq (ping after 24h) 2017/06/08 08:49:57 Future CLs will actually perform an update?
lody 2017/06/08 09:19:57 Yes
82 }
83
84 #pragma mark - NCWidgetProviding
85
86 - (void)widgetActiveDisplayModeDidChange:(NCWidgetDisplayMode)activeDisplayMode
87 withMaximumSize:(CGSize)maxSize {
88 BOOL isVariableHeight = (activeDisplayMode == NCWidgetDisplayModeExpanded);
89
90 // If the widget's height is not variable, the preferredContentSize is the
91 // maxSize. Widgets cannot be shrunk, and this ensures the view will lay
92 // itself out according to the actual screen size. (This is only likely to
93 // happen if the accessibility option for larger font is used.) If the widget
94 // is not a fixed size, if the fitting size for the widget's contents is
95 // larger than the maximum size for the current widget display mode, this
96 // maximum size is used for the widget. Otherwise, the preferredContentSize is
97 // set to the fitting size so that the widget gets the correct height.
98 if (isVariableHeight) {
99 CGSize fittingSize = [self.widgetView
100 systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
101 if (fittingSize.height < maxSize.height) {
102 self.preferredContentSize = fittingSize;
103 return;
104 }
105 }
106 self.preferredContentSize = maxSize;
107 }
108
109 // Implementing this method removes the leading edge inset for iOS version < 10.
110 // TODO(crbug.com/720490): Remove implementation when dropping ios9 support.
111 #if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_10_0
112 - (UIEdgeInsets)widgetMarginInsetsForProposedMarginInsets:
113 (UIEdgeInsets)defaultMa‌​rginInsets {
114 return UIEdgeInsetsZero;
115 }
116 #endif
117
118 #pragma mark - internal
119
120 - (void)openAppWithURL:(NSString*)URL {
121 NSUserDefaults* sharedDefaults =
122 [[NSUserDefaults alloc] initWithSuiteName:app_group::ApplicationGroup()];
123 NSString* defaultsKey =
124 base::SysUTF8ToNSString(app_group::kChromeAppGroupCommandPreference);
125
126 NSString* timePrefKey =
127 base::SysUTF8ToNSString(app_group::kChromeAppGroupCommandTimePreference);
128 NSString* appPrefKey =
129 base::SysUTF8ToNSString(app_group::kChromeAppGroupCommandAppPreference);
130 NSString* commandPrefKey = base::SysUTF8ToNSString(
131 app_group::kChromeAppGroupCommandCommandPreference);
132 NSString* paramPrefKey = base::SysUTF8ToNSString(
133 app_group::kChromeAppGroupCommandParameterPreference);
134
135 [sharedDefaults setObject:@{
marq (ping after 24h) 2017/06/08 08:49:57 Please extract the dictionary value out to a local
lody 2017/06/08 09:19:57 Done.
136 timePrefKey : [NSDate date],
137 appPrefKey : @"TodayExtension",
138 commandPrefKey :
139 base::SysUTF8ToNSString(app_group::kChromeAppGroupOpenURLCommand),
140 paramPrefKey : URL,
141 }
142 forKey:defaultsKey];
143 [sharedDefaults synchronize];
144
145 NSString* scheme = base::mac::ObjCCast<NSString>([[NSBundle mainBundle]
146 objectForInfoDictionaryKey:@"KSChannelChromeScheme"]);
147 if (!scheme)
148 return;
149
150 NSURLComponents* urlComponents = [NSURLComponents new];
151 urlComponents.scheme = scheme;
152 urlComponents.host = kXCallbackURLHost;
153 urlComponents.path = [NSString
marq (ping after 24h) 2017/06/08 08:49:56 Optional nit: to just concatenate two strings, use
lody 2017/06/08 09:19:57 Done.
lody 2017/06/08 09:19:57 Done.
154 stringWithFormat:@"/%@", base::SysUTF8ToNSString(
155 app_group::kChromeAppGroupXCallbackCommand)];
156
157 NSURL* openURL = [urlComponents URL];
158 [self.extensionContext openURL:openURL completionHandler:nil];
159 }
160
161 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698