CNLiveCommuntServiceViewController.m 6.41 KB
//
//  CNLiveCommuntServiceViewController.m
//  CNLiveCommunitModule
//
//  Created by open on 2022/2/1.
//

#import "CNLiveCommuntServiceViewController.h"
#import "CNLiveCommuntyColGridViewLayout.h"

#define kGNCOLGRIDVIEWID @"GNCOLGRIDVIEWID"

@interface CNLiveCommuntServiceViewController ()
@property (nonatomic, strong) CNLiveCommuntyHomeModel * homeModel;
@property (nonatomic, strong) CNLiveCommuntySubHomeModel * homeSubModel;
@property (nonatomic, assign) BOOL isRootVC;
@property (nonatomic, assign) BOOL isLoading;
@property (nonatomic, assign) BOOL isShowLocation;
@end

@implementation CNLiveCommuntServiceViewController

- (instancetype)initWithHomeModel:(CNLiveCommuntyHomeModel *)homeModel isRootVC:(BOOL)isRootVC isShowLocation:(BOOL)isShowLocation
{
    self = [super init];
    if (self) {
        _isShowLocation = isShowLocation;
        _isRootVC = isRootVC;
        _homeModel = homeModel;
        _isRootVC = isRootVC;
    }
    return self;
}
- (instancetype)initWithHomeSubModel:(CNLiveCommuntySubHomeModel *)homeSubModel isRootVC:(BOOL)isRootVC isShowLocation:(BOOL)isShowLocation
{
    self = [super init];
    if (self) {
        _isShowLocation = isShowLocation;
        _isRootVC = isRootVC;
        _homeSubModel = homeSubModel;
        _isRootVC = isRootVC;
    }
    return self;
}
- (void)dealloc
{
    if (self.childViewControllers.count > 0) {
        UIViewController *vc = self.childViewControllers[0];
        if (([self searchProperties:[vc class] string:@"collectionView"] || [self searchProperties:[vc.superclass class] string:@"collectionView"])&&[vc valueForKey:@"collectionView"]&&[[vc valueForKey:@"collectionView"] isKindOfClass:[UICollectionView class]]) {
            UICollectionView *collectionView = (UICollectionView *)[vc valueForKey:@"collectionView"];
            if ([collectionView valueForKey:@"mj_header"]) {
                id header = [collectionView valueForKey:@"mj_header"];
                    [header removeObserver:self forKeyPath:@"state"];
            }
        }
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];

    NSString * cmsPageId = self.homeModel?self.homeModel.cmsPageId:self.homeSubModel.cmsPageId;
    NSString * cmsColumnId = self.homeModel?self.homeModel.cmsColumnId:self.homeSubModel.cmsColumnId;
    NSString * cmsPageUrl = self.homeModel?self.homeModel.cmsPageUrl:self.homeSubModel.cmsPageUrl;
    
    UIViewController * subVC = [[CNLiveCommuntyModule shareInstance].protocol pushLiveViewWithMainProjectWithPageId:cmsPageId ChannelName:self.homeModel.cmsColumnName ChannelId:cmsColumnId WebUrl:cmsPageUrl];
    subVC.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:subVC.view];
    [self addChildViewController:subVC];
    [subVC didMoveToParentViewController:self];
    
    if (self.isRootVC) {
        [subVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(kNavigationBarHeight);
            make.left.bottom.right.mas_equalTo(self.view);
        }];
    }
    
    UIViewController *vc = self.childViewControllers[0];
    if ([self searchProperties:[vc class] string:@"collectionView"]&&[vc valueForKey:@"collectionView"] && [[vc valueForKey:@"collectionView"] isKindOfClass:[UICollectionView class]]){
        UICollectionView *collectionView = (UICollectionView *)[vc valueForKey:@"collectionView"];
        if (!self.isLoading) [collectionView.mj_header beginRefreshing];
        if ([collectionView valueForKey:@"mj_header"]) {
            id header = [collectionView valueForKey:@"mj_header"];
            [header addObserver:self forKeyPath:@"state" options:NSKeyValueObservingOptionNew context:nil];
        }
    }

    if (self.isRootVC) [self initNav];
}
- (BOOL)searchProperties:(Class)cls string:(NSString *)key
{
    unsigned int count;
    objc_property_t *properties = class_copyPropertyList(cls, &count);
    for (int i = 0; i < count; i++) {
        objc_property_t property = properties[i];
        const char *cName = property_getName(property);
        NSString *name = [NSString stringWithCString:cName encoding:NSUTF8StringEncoding];
        if ([name isEqualToString:key]) {
            return YES;
        }
    }
    return NO;
}

#pragma mark - KVO
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    NSLog(@"%@:%@",object,change);
    if ([keyPath isEqualToString:@"state"]) {
        NSString *state = change[NSKeyValueChangeNewKey];
        if (state.integerValue == 1) {
            [self performSelector:@selector(delayAction) afterDelay:1];
        }else{
            self.isLoading = YES;
        }
    }
}
- (void)delayAction{
    self.isLoading = NO;
}
-(void)initNav
{
    self.topNavView.backgroundColor = [UIColor whiteColor];
    self.topLine.backgroundColor = [UIColor whiteColor];

    DSNavButton * searchBtn = [[DSNavButton alloc] initWithImage:[CNLiveCommuntTools cCommunt_setImageWithName:@"c_communit_back"]];
    [searchBtn addTarget:self action:@selector(leftNavBtnAction) forControlEvents:UIControlEventTouchUpInside];
    self.leftNavBtns = @[searchBtn];
    
    NSString * nb_info =  (NSString *)[[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"%@%@",kCNLiveCommuntyNBhdInfoIdentifier,[CNUserInfoManager manager].userInfoModel.uid]];
    if (self.isShowLocation && [nb_info isNotBlank]) {
        CNLiveCommuntyHomeListModel * listModel = [CNLiveCommuntyHomeListModel mj_objectWithKeyValues:nb_info];
        DSNavButton * rightBtn = [[DSNavButton alloc] initWithImage:[CNLiveCommuntTools cCommunt_setImageWithName:@"c_community_service_location"] imageSize:CGSizeMake(15, 15) title:listModel.name font:[UIFont systemFontOfSize:10] titleColor:UIColorHex(#333333) imagePosition:DSImagePositionLeft];
        rightBtn.size = CGSizeMake(80, self.topNavViewHeight - kStatusHeight);
        [rightBtn addTarget:self action:@selector(rightNavBtnAction) forControlEvents:UIControlEventTouchUpInside];
        self.rightNavBtns = @[rightBtn];
    }
}
-(void)rightNavBtnAction
{
    CNLiveCommuntyHomeListViewController * listView = [[CNLiveCommuntyHomeListViewController alloc] init];
    [[self displayViewController] dsPushViewController:listView animated:YES];
}
-(void)leftNavBtnAction
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (UIView *)listView {
    return self.view;
}
@end