You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
163 lines
4.6 KiB
163 lines
4.6 KiB
4 years ago
|
//
|
||
|
// TFAdvertisementManager.m
|
||
|
// TFReader
|
||
|
//
|
||
|
// Created by 谢腾飞 on 2020/12/23.
|
||
|
// Copyright © 2020 xtfei_2011@126.com. All rights reserved.
|
||
|
//
|
||
|
|
||
|
#import "TFAdvertisementManager.h"
|
||
|
#import "TFAdvertisementWebView.h"
|
||
|
#import "AppDelegate.h"
|
||
|
|
||
|
#if TF_Enable_Third_Party_Ad
|
||
|
#import <BUAdSDK/BUBannerAdView.h>
|
||
|
#endif
|
||
|
|
||
|
#import "WXYZ_ADPangolinView.h"
|
||
|
|
||
|
@interface TFAdvertisementManager ()
|
||
|
|
||
|
@property (nonatomic ,weak) TFAdvertisementWebView *webView;
|
||
|
@property (nonatomic ,weak) WXYZ_ADPangolinView *adPangolinView;
|
||
|
|
||
|
@end
|
||
|
|
||
|
@implementation TFAdvertisementManager
|
||
|
|
||
|
+ (BOOL)whetherToLoadAdsWithAdvertisementType:(TFAdvertisementType)type advertisementPosition:(TFAdvertisementPosition)position
|
||
|
{
|
||
|
AppDelegate *appDelegate = (AppDelegate *)kRCodeSync([[UIApplication sharedApplication] delegate]);
|
||
|
|
||
|
switch (position) {
|
||
|
case TFAdvertisementPositionEnd: {
|
||
|
if (type == TFAdvertisementTypeNovel) {
|
||
|
return appDelegate.checkSettingModel.ad_status_setting.chapter_read_end;
|
||
|
} else if (type == TFAdvertisementTypeComic) {
|
||
|
return appDelegate.checkSettingModel.ad_status_setting.comic_read_end;
|
||
|
} else {
|
||
|
return NO;
|
||
|
}
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case TFAdvertisementPositionBottom: {
|
||
|
if (type == TFAdvertisementTypeNovel) {
|
||
|
return appDelegate.checkSettingModel.ad_status_setting.chapter_read_bottom;
|
||
|
} else {
|
||
|
return NO;
|
||
|
}
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
default:
|
||
|
return YES;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
- (instancetype)initWithFrame:(CGRect)frame advertisementType:(TFAdvertisementType)type advertisementPosition:(TFAdvertisementPosition)position
|
||
|
{
|
||
|
if (![self.class whetherToLoadAdsWithAdvertisementType:type advertisementPosition:position]) return nil;
|
||
|
|
||
|
if (self = [super initWithFrame:frame advertisementType:type advertisementPosition:position]) {
|
||
|
[self initialize];
|
||
|
[self netRequest];
|
||
|
}
|
||
|
return self;
|
||
|
}
|
||
|
|
||
|
- (void)setAdvertModel:(TFAdvertModel *)advertModel
|
||
|
{
|
||
|
[super setAdvertModel:advertModel];
|
||
|
|
||
|
if (advertModel.ad_type == 1) { // 内部广告
|
||
|
self.webView.advertModel = advertModel;
|
||
|
self.webView.hidden = NO;
|
||
|
self.adPangolinView.hidden = YES;
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (advertModel.ad_type == 2) { // 穿山甲广告
|
||
|
self.adPangolinView.advertModel = advertModel;
|
||
|
self.adPangolinView.hidden = NO;
|
||
|
self.webView.hidden = YES;
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
- (void)setCloseBlock:(void (^)(void))closeBlock
|
||
|
{
|
||
|
_closeBlock = closeBlock;
|
||
|
|
||
|
self.adPangolinView.closeBlock = closeBlock;
|
||
|
}
|
||
|
|
||
|
- (void)initialize
|
||
|
{
|
||
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveTimestamp) name:UIApplicationWillResignActiveNotification object:nil];
|
||
|
}
|
||
|
|
||
|
- (void)createSubviews
|
||
|
{
|
||
|
TFAdvertisementWebView *webView = [[TFAdvertisementWebView alloc] initWithFrame:self.bounds advertisementType:self.type advertisementPosition:self.position];
|
||
|
webView.hidden = YES;
|
||
|
self.webView = webView;
|
||
|
[self addSubview:webView];
|
||
|
|
||
|
[webView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
|
make.edges.equalTo(self);
|
||
|
}];
|
||
|
|
||
|
WXYZ_ADPangolinView *adPangolinView = [[WXYZ_ADPangolinView alloc] initWithFrame:self.bounds advertisementType:self.type advertisementPosition:self.position];
|
||
|
adPangolinView.hidden = YES;
|
||
|
self.adPangolinView = adPangolinView;
|
||
|
[self addSubview:adPangolinView];
|
||
|
[adPangolinView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
|
make.edges.equalTo(self);
|
||
|
}];
|
||
|
}
|
||
|
|
||
|
- (void)netRequest
|
||
|
{
|
||
|
if (self.position == 0 || self.type == 0) return;
|
||
|
|
||
|
WS(weakSelf)
|
||
|
[super requestAdvertisementWithType:self.type advertisementPostion:self.position complete:^(TFAdvertModel * _Nullable adModel, NSString * _Nonnull msg) {
|
||
|
|
||
|
if (adModel) {
|
||
|
_adTimestamp = [adModel.timestamp integerValue];
|
||
|
weakSelf.advertModel = adModel;
|
||
|
} else {
|
||
|
weakSelf.frame = CGRectZero;
|
||
|
}
|
||
|
}];
|
||
|
}
|
||
|
|
||
|
- (void)dealloc
|
||
|
{
|
||
|
[self saveTimestamp];
|
||
|
|
||
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
||
|
}
|
||
|
|
||
|
static NSInteger _adTimestamp;
|
||
|
+ (NSInteger)adTimestamp
|
||
|
{
|
||
|
if (!_adTimestamp) {
|
||
|
_adTimestamp = [[NSUserDefaults standardUserDefaults] integerForKey:TF_Reader_Ad_Timestamp];
|
||
|
}
|
||
|
return _adTimestamp;
|
||
|
}
|
||
|
|
||
|
+ (void)setAdTimestamp:(NSInteger)adTimestamp
|
||
|
{
|
||
|
_adTimestamp = adTimestamp;
|
||
|
}
|
||
|
|
||
|
- (void)saveTimestamp
|
||
|
{
|
||
|
[[NSUserDefaults standardUserDefaults] setInteger:_adTimestamp forKey:TF_Reader_Ad_Timestamp];
|
||
|
}
|
||
|
|
||
|
@end
|