UIPrintInteractionController+APPPrinter category

Removes the need for special property to save the user setting
This commit is contained in:
Sebastián Katzer 2019-01-21 16:53:31 +01:00
parent 4046ab3473
commit 60444a8534
4 changed files with 107 additions and 36 deletions

View File

@ -75,6 +75,9 @@
<header-file src="src/ios/APPPrinterPaper.h" />
<source-file src="src/ios/APPPrinterPaper.m" />
<header-file src="src/ios/UIPrintInteractionController+APPPrinter.h" />
<source-file src="src/ios/UIPrintInteractionController+APPPrinter.m" />
</platform>
<!-- android -->

View File

@ -21,13 +21,12 @@
#import "APPPrinter.h"
#import "APPPrinterItem.h"
#import "APPPrinterInfo.h"
#import "APPPrinterPaper.h"
#import "APPPrinterLayout.h"
#import "UIPrintInteractionController+APPPrinter.h"
@interface APPPrinter ()
@property (retain) NSDictionary* settings;
@property (nonatomic) UIPrinter *previousPrinter;
@end
@ -82,8 +81,6 @@
NSMutableDictionary* settings = command.arguments[1];
settings[@"callbackId"] = command.callbackId;
self.settings = settings;
[self printContent:content withSettings:settings];
}];
}
@ -100,7 +97,7 @@
choosePaper:(NSArray *)paperList
{
APPPrinterPaper* paperSpec = [[APPPrinterPaper alloc]
initWithDictionary:_settings[@"paper"]];
initWithDictionary:ctrl.settings[@"paper"]];
return [paperSpec bestPaperFromArray:paperList];
}
@ -114,7 +111,7 @@
cutLengthForPaper:(UIPrintPaper *)paper
{
APPPrinterPaper* paperSpec = [[APPPrinterPaper alloc]
initWithDictionary:_settings[@"paper"]];
initWithDictionary:ctrl.settings[@"paper"]];
return paperSpec.length || paper.paperSize.height;
}
@ -157,34 +154,6 @@
});
}
/**
* Adjusts the settings for the print controller.
*
* @param view The view which content to print.
* @param settings The print job specs.
*
* @return A ready to use print controller instance.
*/
- (UIPrintInteractionController *) prepareControllerWithSettings:(NSDictionary *)settings
{
UIPrintInfo* printInfo = [APPPrinterInfo printInfoWithDictionary:settings];
NSDictionary* ui = settings[@"ui"];
UIPrintInteractionController* ctrl =
[UIPrintInteractionController sharedPrintController];
ctrl.printInfo = printInfo;
ctrl.delegate = self;
if (ui)
{
ctrl.showsNumberOfCopies = ![ui[@"hideNumberOfCopies"] boolValue];
ctrl.showsPaperSelectionForLoadedPapers = ![ui[@"hidePaperFormat"] boolValue];
}
return ctrl;
}
/**
* Loads the content into the print controller.
*
@ -199,7 +168,9 @@
__block id item;
UIPrintInteractionController* ctrl =
[self prepareControllerWithSettings:settings];
[UIPrintInteractionController sharedPrintControllerWithSettings:settings];
ctrl.delegate = self;
if ([self strIsNullOrEmpty:content])
{
@ -444,4 +415,3 @@
}
@end

View File

@ -0,0 +1,28 @@
/*
Copyright 2013 appPlant GmbH
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
@interface UIPrintInteractionController (APPPrinter)
+ (instancetype) sharedPrintControllerWithSettings:(NSDictionary *)settings;
@property (nonatomic, retain) NSDictionary *settings;
@end

View File

@ -0,0 +1,70 @@
/*
Copyright 2013 appPlant GmbH
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
#include "APPPrinterInfo.h"
#include "UIPrintInteractionController+APPPrinter.h"
#import <objc/runtime.h>
@implementation UIPrintInteractionController (APPPrinter)
static char UIP_SETTINGS_KEY;
@dynamic settings;
#pragma mark -
#pragma mark Init
+ (instancetype) sharedPrintControllerWithSettings:(NSDictionary *)settings
{
NSDictionary* ui = settings[@"ui"];
UIPrintInteractionController* ctrl =
UIPrintInteractionController.sharedPrintController;
ctrl.printInfo = [APPPrinterInfo
printInfoWithDictionary:settings];
ctrl.settings = settings;
if (ui)
{
ctrl.showsNumberOfCopies = ![ui[@"hideNumberOfCopies"] boolValue];
ctrl.showsPaperSelectionForLoadedPapers = ![ui[@"hidePaperFormat"] boolValue];
}
return ctrl;
}
#pragma mark -
#pragma mark Private
- (void) setSettings:(NSDictionary *)settings
{
objc_setAssociatedObject(self, &UIP_SETTINGS_KEY, settings, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSDictionary *)settings
{
return objc_getAssociatedObject(self, &UIP_SETTINGS_KEY);
}
@end