| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- //
- // ScenesViewController.m
- // kneet
- //
- // Created by Jason Lee on 4/1/15.
- // Copyright (c) 2015 ntels. All rights reserved.
- //
- #import "JDObject.h"
- #import "RequestHandler.h"
- #import "SceneModel.h"
- #import "UIImageView+WebCache.h"
- #import "CustomLabel.h"
- #import "CustomImageView.h"
- #import "JYRefreshController.h"
- #import "ScenesDetailViewController.h"
- #import "ScenesViewController.h"
- #import "CustomButton.h"
- #define kfScenesTableViewCellHeight 100.0f
- @interface ScenesTableViewCell () {
- }
- @end
- @implementation ScenesTableViewCell
- - (void)awakeFromNib {
- self.backgroundColor = [UIColor clearColor];
- self.selectionStyle = UITableViewCellSelectionStyleNone;
- }
- @end
- @implementation ScenesAddTableViewCell
- - (void)awakeFromNib {
- self.backgroundColor = [UIColor clearColor];
- self.selectionStyle = UITableViewCellSelectionStyleNone;
-
- _lblActionDesc.text = NSLocalizedString(@"버튼 하나로 여러 장치가 동작되도록\n지금 바로 만들어보세요", @"버튼 하나로 여러 장치가 동작되도록\n지금 바로 만들어보세요");
- }
- - (void)btnAddTouched:(id)sender {
- UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ScenesRegisterTypeViewController" storyboardName:@"Scenes"];
- [[JDFacade facade].currentViewController.navigationController pushViewController:vc animated:YES];
- }
- @end
- @interface ScenesViewController () <UITableViewDataSource, UITableViewDelegate> {
- NSString *_pagingType, *_pagingId;
- NSMutableArray<SceneModel> *_sceneList;
- UIImage *_bgCellImage1, *_bgCellImage2;
- UIImage *_runImageNormal, *_runImageDisable;
- }
- @property (strong, nonatomic) JYPullToRefreshController *refreshController;
- @property (strong, nonatomic) JYPullToLoadMoreController *loadMoreController;
- @end
- #pragma mark - Class Definition
- @implementation ScenesViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- _sceneList = (NSMutableArray<SceneModel> *)[[NSMutableArray alloc] init];
- [self initUI];
- [self prepareViewDidLoad];
- }
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
- self.title = NSLocalizedString(@"멀티 제어",nil);
- }
- - (void)initUI {
-
-
- [self generateOptionButton];
- //intialize tableview
- _tableView.dataSource = self;
- _tableView.delegate = self;
- _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
- _tableView.backgroundColor = [UIColor clearColor];
- _tableView.tableFooterView = [[UIView alloc] init]; //this call table events;
- //set refresh controls
- __weak typeof(self) weakSelf = self;
- self.refreshController = [[JYPullToRefreshController alloc] initWithScrollView:self.tableView];
- self.refreshController.pullToRefreshHandleAction = ^{
- [weakSelf requestRuleListRecently];
- };
-
- self.loadMoreController = [[JYPullToLoadMoreController alloc] initWithScrollView:self.tableView];
- self.loadMoreController.pullToLoadMoreHandleAction = ^{
- [weakSelf requestRuleListOlder];
- };
- UIEdgeInsets insets = UIEdgeInsetsMake(4, 4, 4, 4);
- _bgCellImage1 = [CommonUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"tp_01_img_list_bg_02"]];
- _bgCellImage2 = [CommonUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"tp_01_img_list_bg_01"]];
- _runImageNormal = [UIImage imageNamed:@"tp_01_img_scene_btn_play"];
- _runImageDisable = [UIImage imageNamed:@"tp_01_img_scene_btn_play_disable"];
-
- [self setThingsPopoverOptions];
- }
- - (void)setThingsPopoverOptions {
- //set Popover Contents
- __weak typeof(self) weakSelf = self;
- _popooverOptionArray = [[NSMutableArray alloc] init];
- [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"새로 고침", @"새로 고침"),
- @"iconName": @"tp_01_img_bg_morepopup_icon_refresh",
- @"target": weakSelf,
- @"selector": [NSValue valueWithPointer:@selector(refreshRuleList)]}];
- if ([JDFacade facade].loginHomeGroup.level > 10) {//파워유저 이상일 경우,
- [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"새 멀티 제어 추가", @"새 멀티 제어 추가"),
- @"iconName": @"tp_01_img_bg_morepopup_icon_group_deviceadd",
- @"target": weakSelf,
- @"selector": [NSValue valueWithPointer:@selector(addNewScene)]}];
- }
- }
- - (void)prepareViewDidLoad {
- [self performSelector:@selector(requestScenes) withObject:nil afterDelay:0.0f];
- }
- - (void)refreshRuleList {
- _pagingId = nil;
- _pagingType = nil;
- if (_sceneList && _sceneList.count) {
- [_sceneList removeAllObjects];
- }
- [self performSelector:@selector(requestScenes) withObject:nil afterDelay:0.0f];
- }
- - (void)addNewScene {
- UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ScenesRegisterTypeViewController" storyboardName:@"Scenes"];
- [[JDFacade facade].currentViewController.navigationController pushViewController:vc animated:YES];
- }
- #pragma mark - Main Logic
- - (void)requestRuleListRecently {
- SceneModel *firstScene = [_sceneList firstObject];
- _pagingType = ksListPagingTypeUpward;
- _pagingId = firstScene.homegrpSceneId;
- [self performSelector:@selector(requestScenes) withObject:nil afterDelay:0.0f];
- }
- - (void)requestRuleListOlder {
- SceneModel *lastScene = [_sceneList lastObject];
- _pagingType = ksListPagingTypeDownward;
- _pagingId = lastScene.homegrpSceneId;
- [self performSelector:@selector(requestScenes) withObject:nil afterDelay:0.0f];
- }
- - (void)requestScenes {
- NSDictionary *parameter = @{@"homegrp_scene_id": _pagingId ? _pagingId : ksEmptyString,
- @"paging_type": _pagingType ? _pagingType : ksEmptyString};
- NSString *path = [NSString stringWithFormat:API_GET_SCENE];
- [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:parameter modelClass:[SceneListModel class] completion:^(id responseObject) {
- if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
- return;
- }
- SceneListModel *fetchedRuleList = (SceneListModel *)responseObject;
- if (fetchedRuleList && fetchedRuleList.list && fetchedRuleList.list.count) {//API 성공,
- [_sceneList addObjectsFromArray:fetchedRuleList.list];
- } else {//실패 시,
- if (!_sceneList.count) {//이미 로드된 데이터가 있을 경우는 출력하지 않음.
- // NoContentView *noContentView = [NoContentView viewFromNib];
- // _tableView.tableFooterView = noContentView;
- }
- }
-
- [_tableView reloadData];
- } failure:^(id errorObject) {
- JDErrorModel *error = (JDErrorModel *)errorObject;
- [[JDFacade facade] alert:error.errorMessage];
- }];
- }
- - (void)requestRunHomeGroupScene:(SceneModel *)scene {
- NSString *path = [NSString stringWithFormat:API_POST_SCENE_EXECUTE, scene.homegrpSceneId];
- [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:nil modelClass:[JDJSONModel class] completion:^(id responseObject) {
- if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
- return;
- }
- JDJSONModel *result = (JDJSONModel *) responseObject;
- if (result) {//API 성공 ,
- if (result.msg && ![result.msg isEmptyString]) {
- [[JDFacade facade] alert:result.msg];
- } else {
- [[JDFacade facade] toast:NSLocalizedString(@"실행되었습니다", @"실행되었습니다")];
- }
- }
- } failure:^(id errorObject) {
- JDErrorModel *error = (JDErrorModel *)errorObject;
- [[JDFacade facade] alert:error.errorMessage];
- }];
- }
- - (void)updateScene:(SceneModel *)scene {
- for (SceneModel *tmpScene in _sceneList) {
- if ([scene.homegrpSceneId isEqualToString:tmpScene.homegrpSceneId]) {
- tmpScene.sceneName = scene.sceneName;
- tmpScene.deleteCnt = scene.deleteCnt;
- break;
- }
- }
- [_tableView reloadData];
- }
- #pragma mark - UITableView DataSource & Delegate
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- return 1;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- return _sceneList.count + ([JDFacade facade].loginHomeGroup.level > 10 ? 1 : 0);
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
- CGFloat height = kfScenesTableViewCellHeight;
- if (indexPath.row == _sceneList.count) {
- height = _sceneList.count ? 190.0f : IPHONE_HEIGHT - kfNavigationBarHeight;
- _tableView.scrollEnabled = _sceneList.count;
- }
- return height;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- UITableViewCell *cell = nil;
- if (indexPath.row < _sceneList.count) {
- ScenesTableViewCell *rcell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
- if (rcell == nil) {
- rcell = [[ScenesTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellIdentifier"];
- }
- SceneModel *scene = _sceneList[indexPath.row];
-
- //활성/비활성 이미지 적용
- UIImage *nimage = ![scene.deleteCnt boolValue] ? _runImageNormal : _runImageDisable;
- [rcell.btnRun setImage:nimage forState:UIControlStateNormal];
-
- rcell.btnRun.value = indexPath;
- rcell.lblSceneName.text = scene.sceneName;
- rcell.lblSceneName.textColor = [UIColor whiteColor];
- [rcell.btnRun addTarget:self action:@selector(btnRunTouchedOnCell:) forControlEvents:UIControlEventTouchUpInside];
- cell = rcell;
- //set background image
- if (indexPath.row % 2 == 1) {
- cell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage1];
- } else {
- cell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage2];
- }
- } else {//씬 추가 셀
- ScenesAddTableViewCell *acell = [tableView dequeueReusableCellWithIdentifier:@"AddCellIdentifier"];
- if (acell == nil) {
- acell = [[ScenesAddTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AddCellIdentifier"];
- }
- cell = acell;
- }
- return cell;
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
- [tableView deselectRowAtIndexPath:indexPath animated:YES];
- if (indexPath.row == _sceneList.count) {//add cell 일 경우, 리턴,
- return;
- }
- //goto scene details
- ScenesDetailViewController *vc = (ScenesDetailViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"ScenesDetailViewController" storyboardName:@"Scenes"];
- vc.scene = _sceneList[indexPath.row];
- [self.navigationController pushViewController:vc animated:YES];
- }
- - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
- // Remove seperator inset
- if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
- [cell setSeparatorInset:UIEdgeInsetsZero];
- }
- // Prevent the cell from inheriting the Table View's margin settings
- if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
- [cell setPreservesSuperviewLayoutMargins:NO];
- }
- // Explictly set your cell's layout margins
- if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
- [cell setLayoutMargins:UIEdgeInsetsZero];
- }
- }
- #pragma mark - UI Events
- - (IBAction)btnRunTouchedOnCell:(id)sender {
- CustomButton *btn = (CustomButton *)sender;
- NSIndexPath *indexPath = (NSIndexPath *)btn.value;
- SceneModel *scene = _sceneList[indexPath.row];
- if ([scene.deleteCnt boolValue]) {
- [[JDFacade facade] toast:NSLocalizedString(@"일부 장치가 삭제되어 비활성 되었습니다\n편집 후 다시 사용해주세요", @"일부 장치가 삭제되어 비활성 되었습니다\n편집 후 다시 사용해주세요")];
- return;
- }
-
- [self requestRunHomeGroupScene:scene];
- }
- #pragma mark - MemoryWarning
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- @end
|