| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- //
- // ThingsViewController.m
- // kneet
- //
- // Created by Jason Lee on 3/10/15.
- // Copyright (c) 2015 ntels. All rights reserved.
- //
- #import "JDObject.h"
- #import "RequestHandler.h"
- #import "JDJSONModel.h"
- #import "DeviceModel.h"
- #import "UIImageView+WebCache.h"
- #import "CustomLabel.h"
- #import "CustomImageView.h"
- #import "CustomTextField.h"
- #import "CustomButton.h"
- #import "JYRefreshController.h"
- #import "WYPopoverController.h"
- #import "ImageUtil.h"
- #import "CommandClassControlView.h"
- #import "ThingsDetailViewController.h"
- #import "ThingsViewController.h"
- #import "UIButton+WebCache.h"
- #define kfThingsTableViewCellHeight 100.0f
- @interface ThingsCollectionViewCell () {
-
- }
- @property (weak, nonatomic) NSIndexPath *indexPath;
- @end
- @implementation ThingsCollectionViewCell
- - (void)awakeFromNib {
- }
- @end
- @implementation ThingsAddCollectionViewCell
- @end
- @implementation ThingsCollectionFooterView
- @end
- @interface ThingsViewController () <UICollectionViewDataSource, UICollectionViewDelegate> {
- NSMutableArray<DeviceModel> *_deviceList;
- NSString *_pagingId, *_pagingType;
- BOOL _isNotFirstLoading, _isDeleteMode;
- }
- @property (strong, nonatomic) JYPullToRefreshController *refreshController;
- @property (strong, nonatomic) JYPullToLoadMoreController *loadMoreController;
- @end
- #pragma mark - Class Definition
- @implementation ThingsViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- [self initUI];
- }
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
- [self prepareViewDidLoad];
- }
- - (void)initUI {
-
- //set tableview option
- _collectionView.delegate = self;
- _collectionView.dataSource = self;
- _collectionView.backgroundColor = kUIBgColor01;
-
- if ([JDFacade facade].loginUser.homehubDeviceId && ![[JDFacade facade].loginUser.homehubDeviceId isEmptyString]) {
- [self generateOptionButton];
- [self setThingsPopoverOptions];
- }
-
- //set refresh controls
- // __weak typeof(self) weakSelf = self;
- // self.refreshController = [[JYPullToRefreshController alloc] initWithScrollView:self.tableView];
- // self.refreshController.pullToRefreshHandleAction = ^{
- // [weakSelf requestPredefinedRulesRecently];
- // };
- //
- // self.loadMoreController = [[JYPullToLoadMoreController alloc] initWithScrollView:self.tableView];
- // self.loadMoreController.pullToLoadMoreHandleAction = ^{
- // [weakSelf requestPredefinedRulesOlder];
- // };
- }
- - (void)setThingsPopoverOptions {
-
- //set Popover Contents
- __weak typeof(self) weakSelf = self;
- _popooverOptionArray = [[NSMutableArray alloc] init];
- [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"새로 고침",nil),
- @"iconName": @"tp_01_img_bg_morepopup_icon_refresh",
- @"target": weakSelf,
- @"selector": [NSValue valueWithPointer:@selector(refreshDeviceList)]}];
- if ([JDFacade facade].loginUser.level > 10) {//권한
- [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"추가", @"추가"),
- @"iconName": @"tp_01_img_bg_morepopup_icon_group_deviceadd",
- @"target": weakSelf,
- @"selector": [NSValue valueWithPointer:@selector(addNewDevice)]}];
-
- [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"삭제", @"삭제"),
- @"iconName": @"tp_01_img_bg_morepopup_icon_group_deviceadd",
- @"target": weakSelf,
- @"selector": [NSValue valueWithPointer:@selector(deleteDevice)]}];
- }
- }
- - (void)prepareViewDidLoad {
- //fetch devices from server
-
- if (![JDFacade facade].loginUser.homehubDeviceId || [[JDFacade facade].loginUser.homehubDeviceId isEmptyString]) {
- [_mainView bringSubviewToFront:_addHubContainerView];
- _collectionView.hidden = YES;
-
- } else {
-
- [_mainView bringSubviewToFront:_collectionView];
- _addHubContainerView.hidden = YES;
- _collectionView.hidden = NO;
-
- [self performSelector:@selector(requestDeviceList) withObject:nil afterDelay:0.0f];
- }
- }
- - (void)updateDevice:(DeviceModel *)device {
- // if ([device.deviceId isEqualToString:ldevice.deviceId]) {
- // ldevice.cmdclsValue = device.cmdclsValue;
- // ldevice.contentValue = device.contentValue;
- // break;
- // }
- }
- - (void)addNewDevice {
- UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddViewController" storyboardName:@"Things"];
- [self presentViewController:vc animated:YES completion:nil];
- }
- - (void)deleteDevice {
-
- }
- - (void)refreshDeviceList {
- [self performSelector:@selector(requestDeviceList) withObject:nil afterDelay:0.0f];
- }
- #pragma mark - Main Logic
- - (void)requestDeviceListRecently {
- DeviceModel *firstDevice = [_deviceList firstObject];
- _pagingType = ksListPagingTypeUpward;
- _pagingId = firstDevice.createDatetime;
-
- [self performSelector:@selector(requestDeviceList) withObject:nil afterDelay:0.0f];
- }
- - (void)requestDeviceListOlder {
- DeviceModel *lastDevice = [_deviceList lastObject];
- _pagingType = ksListPagingTypeDownward;
- _pagingId = lastDevice.createDatetime;
-
- [self performSelector:@selector(requestDeviceList) withObject:nil afterDelay:0.0f];
- }
- - (void)requestDeviceList {
- //parameters
- NSDictionary *parameter = @{@"paging_datetime": _pagingId ? _pagingId : ksEmptyString,
- @"paging_type": _pagingType ? _pagingType : ksEmptyString};
- NSString *path = [NSString stringWithFormat:API_GET_DEVICE_LIST];
- [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:parameter modelClass:[DeviceListModel class] completion:^(id responseObject) {
- if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
- return;
- }
- DeviceListModel *deviceList = (DeviceListModel *)responseObject;
- if (deviceList && deviceList.list && deviceList.list.count) {
- _deviceList = deviceList.list;
- _lblTitle.text = [NSString stringWithFormat:@"장치 전체 %zd", _deviceList.count];
- [_lblTitle setColor:kUITextColor02 text:[NSString stringWithFormat:@"%zd", _deviceList.count]];
-
- } else {
- if (!_deviceList.count) {//이미 로드된 데이터가 있을 경우는 출력하지 않음.
- // NoContentView *noContentView = [NoContentView viewFromNib];
- // _tableView.tableFooterView = noContentView;
- }
- }
-
- [_collectionView reloadData];
- } failure:^(id errorObject) {
- JDErrorModel *error = (JDErrorModel *)errorObject;
- [[JDFacade facade] alert:error.errorMessage];
- }];
- }
- #pragma mark - UICollectionView Delegate
- - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
-
- NSInteger auth = [JDFacade facade].loginUser.level == 90 && !_isDeleteMode; //마스터 권한일 경우,
- NSInteger count = _deviceList.count % 2 == 0 ? _deviceList.count : _deviceList.count + auth; //홀수일 경우, 멤버 초대 버튼을 추가해줌.
- return count;
- }
- - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
-
- UICollectionViewCell *rcell = nil;
-
- if (indexPath.row < _deviceList.count) {
- ThingsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ThingsCellIdentifier" forIndexPath:indexPath];
-
- DeviceModel *device =_deviceList[indexPath.row];
- cell.indexPath = indexPath;
-
- [cell.btnDevice sd_setImageWithURL:[NSURL URLWithString:device.imageFileName] forState:UIControlStateNormal
- placeholderImage:nil
- options:SDWebImageRefreshCached
- completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
- [cell layoutIfNeeded];
- }];
-
- cell.lblDeviceName.text = device.deviceName;
- cell.lblDeviceStatus.text = device.contentValue;
-
- rcell = cell;
- } else {
- ThingsAddCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AddCollectionCellIdentifier" forIndexPath:indexPath];
-
- if (![cell.btnAdd actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
- [cell.btnAdd addTarget:self action:@selector(addNewDevice) forControlEvents:UIControlEventTouchUpInside];
- }
- rcell = cell;
- }
-
- return rcell;
- }
- - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
- viewForSupplementaryElementOfKind:(NSString *)kind
- atIndexPath:(NSIndexPath *)indexPath
- {
- ThingsCollectionFooterView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter
- withReuseIdentifier:@"FooterIdentifier" forIndexPath:indexPath];
- if (![footerView.btnAdd actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
- [footerView.btnAdd addTarget:self action:@selector(addNewDevice) forControlEvents:UIControlEventTouchUpInside];
- }
- return footerView;
- }
- - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
-
- //FIXME : 권한 추가
- // if (_memberList.count % 2 == 1 || [JDFacade facade].loginUser.level < 90 || _isDeleteMode) {//마스터 권한이 아니거나, 짝수가 아닐 경우
- if (_deviceList.count % 2 == 1 || _isDeleteMode) {//마스터 권한이 아니거나, 짝수가 아닐 경우
- return CGSizeZero;
- }
-
- return CGSizeMake(IPHONE_WIDTH, 160.0f);
- }
- - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
- if (IPHONE_WIDTH == 414.0f) {//아이폰 6일 경우,
- return CGSizeMake(212.0f, 160.0f);
- } else if (IPHONE_WIDTH == 375.0f) {//아이폰 6+일경우
- return CGSizeMake(187.5, 160.0f);
- }
- return CGSizeMake(160.0f, 160.0f);
- }
- - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
- [collectionView deselectItemAtIndexPath:indexPath animated:YES];
-
- if (indexPath.row < _deviceList.count) {
- ThingsCollectionViewCell *cell = (ThingsCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"ThingsCellIdentifier" forIndexPath:indexPath];
- DeviceModel *device = _deviceList[indexPath.row];
- }
- }
- #pragma mark - UI Events
- - (void)btnAddHubTouched:(id)sender {
- [[JDFacade facade] gotoHomeHubRegistration];
- }
- #pragma mark - MemoryWarning
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- @end
|