| Flutter iOS Embedder |

FlutterRestorationPlugin.mm
Go to the documentation of this file.
1 // Copyright 2013 The Flutter 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 "flutter/shell/platform/darwin/ios/framework/Source/FlutterRestorationPlugin.h"
6
7 #import <Foundation/Foundation.h>
8 #import <UIKit/UIKit.h>
9
10 #include "flutter/fml/logging.h"
11
12 FLUTTER_ASSERT_ARC
13
14 @interface FlutterRestorationPlugin ()
15 @property(nonatomic, copy) FlutterResult pendingRequest;
16 @end
17
18 @implementation FlutterRestorationPlugin {
19 BOOL _waitForData;
20 BOOL _restorationEnabled;
21 }
22
23 - (instancetype)initWithChannel:(FlutterMethodChannel*)channel
24 restorationEnabled:(BOOL)restorationEnabled {
25 FML_DCHECK(channel) << "channel must be set";
26 self = [super init];
27 if (self) {
28 [channel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
29 [self handleMethodCall:call result:result];
30 }];
31 _restorationEnabled = restorationEnabled;
32 _waitForData = restorationEnabled;
33 }
34 return self;
35 }
36
37 - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
38 if ([[call method] isEqualToString:@"put"]) {
39 NSAssert(self.pendingRequest == nil, @"Cannot put data while a get request is pending.");
40 FlutterStandardTypedData* data = [call arguments];
41 self.restorationData = [data data];
42 result(nil);
43 } else if ([[call method] isEqualToString:@"get"]) {
44 if (!_restorationEnabled || !_waitForData) {
45 result([self dataForFramework]);
46 return;
47 }
48 NSAssert(self.pendingRequest == nil, @"There can only be one pending request.");
49 self.pendingRequest = result;
50 } else {
51 result(FlutterMethodNotImplemented);
52 }
53 }
54
55 - (void)setRestorationData:(NSData*)data {
56 if (data != _restorationData) {
57 _restorationData = [data copy];
58 }
59 _waitForData = NO;
60 if (self.pendingRequest != nil) {
61 self.pendingRequest([self dataForFramework]);
62 self.pendingRequest = nil;
63 }
64 }
65
66 - (void)markRestorationComplete {
67 _waitForData = NO;
68 if (self.pendingRequest != nil) {
69 NSAssert(_restorationEnabled, @"No request can be pending when restoration is disabled.");
70 self.pendingRequest([self dataForFramework]);
71 self.pendingRequest = nil;
72 }
73 }
74
75 - (void)reset {
76 self.pendingRequest = nil;
77 self.restorationData = nil;
78 }
79
80 - (NSDictionary*)dataForFramework {
81 if (!_restorationEnabled) {
82 return @{@"enabled" : @NO};
83 }
84 if (self.restorationData == nil) {
85 return @{@"enabled" : @YES};
86 }
87 return @{
88 @"enabled" : @YES,
89 @"data" : [FlutterStandardTypedData typedDataWithBytes:self.restorationData]
90 };
91 }
92
93 @end
_restorationEnabled
BOOL _restorationEnabled
Definition: FlutterRestorationPlugin.mm:18
FlutterMethodChannel
Definition: FlutterChannels.h:220
FlutterMethodNotImplemented
FLUTTER_DARWIN_EXPORT NSObject const * FlutterMethodNotImplemented
FlutterRestorationPlugin.h
FlutterRestorationPlugin
Definition: FlutterRestorationPlugin.h:12
+[FlutterStandardTypedData typedDataWithBytes:]
instancetype typedDataWithBytes:(NSData *data)
Definition: FlutterStandardCodec.mm:146
-[FlutterMethodChannel setMethodCallHandler:]
void setMethodCallHandler:(FlutterMethodCallHandler _Nullable handler)
FlutterMethodCall
Definition: FlutterCodecs.h:220
FlutterResult
void(^ FlutterResult)(id _Nullable result)
Definition: FlutterChannels.h:194
FlutterStandardTypedData
Definition: FlutterCodecs.h:300
FlutterStandardTypedData::data
NSData * data
Definition: FlutterCodecs.h:344
FLUTTER_ASSERT_ARC
Definition: FlutterChannelKeyResponder.mm:13
FlutterMethodCall::arguments
id arguments
Definition: FlutterCodecs.h:238