萤石云 iOSSDK,移植跨平台相关工程
Davin
2023-07-10 0de4cf306d8e13fd57b3e04bfb81362a10f12a4d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//
//  HDLEZDeviceMsgListViewController.m
//  EZSDK
//
//  Created by 陈启扬 on 2023/3/16.
//
 
#import "HDLEZDeviceMsgListViewController.h"
#import "HDLEZDeviceMsgListView.h"
#import "MJRefreshNormalHeader.h"
#import "MJRefreshAutoNormalFooter.h"
#import "HDLEZDevMsgListModel.h"
#import "MWPhotoBrowser.h"
#import "SDWebImageManager.h"
#import <Photos/Photos.h>
@interface HDLEZDeviceMsgListViewController ()<MWPhotoBrowserDelegate>
@property (nonatomic, strong) HDLEZDeviceMsgListView *msgListV;//消息列表view
 
@property (nonatomic, strong) MJRefreshNormalHeader *header;//刷新header
 
@property (nonatomic, strong) MJRefreshAutoNormalFooter *footer;//加载footer
 
@property (nonatomic, strong) HDLEZDevMsgListModel *msgListModel;//消息列表model
 
@property (nonatomic, strong) NSMutableArray<HDLEZDeviceMsgInfoModel*> *msgList;//消息列表(分页获取到的所有消息)
 
@property (nonatomic, assign) NSInteger currentPageNo;//当前页码
 
@property (nonatomic, assign) NSInteger pageSize;//分页容量
 
@property (nonatomic, strong) NSMutableArray <MWPhoto *>*photos;    // 预览图片信息
@property (nonatomic, strong) NSString *currentPreviewPhoto;        // 当前预览的图片
    
@end
 
@implementation HDLEZDeviceMsgListViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTopBarViewWithTitle:HDLEZLocallizedString(@"device_history")];
    [self.topBarView.bottomLine setHidden:YES];
    // Do any additional setup after loading the view.
}
 
-(void)addSubViews{
    
    //
    _msgListV=[[HDLEZDeviceMsgListView alloc] init];
    [self.view addSubview:_msgListV];
    [_msgListV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(HDLEZ_APP_TOP_BAR_HEIGHT);
        make.left.bottom.right.equalTo(self.view);
    }];
    __weak __typeof(self)weakSelf = self;
    [_msgListV previewPicture:^(NSString * _Nonnull pictureUrl) {
        [weakSelf previewPictureWithUrl:pictureUrl];
    }];
    
    //下拉刷新
    // 设置回调(一旦进入刷新状态,就调用target的action,也就是调用self的loadNewData方法)
    _header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshData)];
    // 设置自动切换透明度(在导航栏下面自动隐藏)
    _header.automaticallyChangeAlpha = YES;
    // 隐藏时间
    _header.lastUpdatedTimeLabel.hidden = YES;
    // 隐藏刷新状态文本
    _header.stateLabel.hidden = YES;
    _msgListV.mj_header=_header;
    
    //上拉加载更多
    _footer=[MJRefreshAutoNormalFooter  footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
    _footer.refreshingTitleHidden=YES;
    [_footer setTitle:@"" forState:MJRefreshStateIdle];
    [_footer setTitle:HDLEZLocallizedString(@"device_his_no_more_data") forState:MJRefreshStateNoMoreData];
    _footer.stateLabel.textColor=HDLEZ_COLOR_TEXT_LIGHT_GRAY;
    _msgListV.mj_footer=_footer;
 
    //开始加载数据
    [_header beginRefreshing];
    
    
}
 
/*刷新数据
 */
-(void)refreshData{
    self.msgList=[[NSMutableArray alloc] init];
    _currentPageNo=1;
    _pageSize=20;
    
    [self getMsgList];
}
 
/*加载更多
 */
-(void)loadMore{
//    _currentPageNo+=1;
    
    [self getMsgList];
 
}
 
/*获取历史记录
 */
-(void)getMsgList{
    __weak __typeof(self)weakSelf = self;
    [[EZHttpUtil sharedManager] getDeviceMessageByHDL:self.deviceId pageSize:_pageSize pageNo:_currentPageNo completion:^(ResponseData * _Nonnull responseData) {
        [weakSelf.header endRefreshing];
        [weakSelf.footer endRefreshing];
        weakSelf.msgListModel=[HDLEZDevMsgListModel yy_modelWithDictionary:responseData.data];
        if (responseData.success) {
            weakSelf.msgListModel=[HDLEZDevMsgListModel yy_modelWithDictionary:responseData.data];
            if (weakSelf.currentPageNo == 1) {
                [weakSelf.msgList removeAllObjects];
            }
            [weakSelf.msgList addObjectsFromArray:weakSelf.msgListModel.list];
            weakSelf.msgListV.msgList=weakSelf.msgList;
            if (weakSelf.msgListModel.list.count == weakSelf.pageSize) {
                weakSelf.currentPageNo+=1;
            }
            [weakSelf.footer endRefreshingWithNoMoreData];
        }else{
            [weakSelf.view makeToast:responseData.message
                            duration:1.5
                            position:@"center"];
        }
    }];
}
 
#pragma mark MWPhotoBrowserDelegate
- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
    return self.photos.count;
}
 
- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
    if (index < self.photos.count) {
        return [self.photos objectAtIndex:index];
    }
    return nil;
}
 
- (void)didClickCurrentPhotoBrowserBlank:(MWPhotoBrowser *)photoBrowser {
    [self.navigationController popToViewController:self animated:NO];
}
 
- (void)didClickDownLoad:(MWPhotoBrowser *)photoBrowser {
    
    __weak __typeof(self)weakSelf = self;
    [self authPhotoLibraries:^(BOOL isAblity) {
        if (isAblity) {
            [weakSelf downloadTheImage];
        } else {
            [weakSelf guideUserOpenAuth];
        }
    }];
}
 
- (void)downloadTheImage {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        if (!self.currentPreviewPhoto || self.currentPreviewPhoto.length == 0) {
            [UIView dd_showMessage:NSLocalizedString(@"device_save_gallery_failure", @"下载失败")];
            return;
        }
        NSURL *url = [NSURL URLWithString:self.currentPreviewPhoto];
        SDWebImageManager *manager = [SDWebImageManager sharedManager];
        UIImage *img;
        if ([manager diskImageExistsForURL:url]) {
            img =  [[manager imageCache] imageFromDiskCacheForKey:url.absoluteString];
        } else {
            //从网络下载图片
            NSData *data = [NSData dataWithContentsOfURL:url];
            img = [UIImage imageWithData:data];
        }
        // 保存图片到相册中
        dispatch_async(dispatch_get_main_queue(), ^{
            UIImageWriteToSavedPhotosAlbum(img,self, @selector(image:didFinishSavingWithError
                                                               :contextInfo:),nil);
        });
    });
}
 
//保存图片完成之后的回调
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error
  contextInfo:(void *)contextInfo {
    // Was there an error?
    NSString *message = nil;
    if (!error) {
        message = NSLocalizedString(@"device_save_gallery", @"已保存至手机相册");
    }
    else
    {
        message = NSLocalizedString(@"device_save_gallery_failure", @"下载失败");
        NSLog(@"#######图片保存失败:%@", [error description]);
    }
    [UIView dd_showMessage:message];
}
 
/*访问相册权限
 */
-(void)authPhotoLibraries:(void(^)(BOOL isAblity))result {
    PHAuthorizationStatus author =[PHPhotoLibrary authorizationStatus];
    if (author == PHAuthorizationStatusRestricted || author ==PHAuthorizationStatusDenied){
        if (result) {
            result(NO);
        }
    } else if (author == PHAuthorizationStatusNotDetermined) {  // 首次授权
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_async(queue, ^{
            [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
                if (status != PHAuthorizationStatusAuthorized) {
                    if (result) {
                        result(NO);
                    }
                } else {
                    if (result) {
                        result(YES);
                    }
                }
            }];
        });
    } else {
        if (result) {
            result(YES);
        }
    }
}
 
/*引导用户授权获取图片
 */
- (void)guideUserOpenAuth{
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"hdl_ez_download_tip", @"温馨提示") message:NSLocalizedString(@"hdl_ez_download_auth", @"请打开访问权限") preferredStyle:(UIAlertControllerStyleAlert)];
    UIAlertAction *alertA = [UIAlertAction actionWithTitle:NSLocalizedString(@"hdl_ez_download_cancle", @"取消") style:(UIAlertActionStyleDefault) handler:nil];
    UIAlertAction *act = [UIAlertAction actionWithTitle:NSLocalizedString(@"hdl_ez_download_toSet", @"去设置") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // 引导用户设置
        NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        
        if ([[UIApplication sharedApplication] canOpenURL:url]) {
            
            [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
        }
    }];
    [alertC addAction:alertA];
    [alertC addAction:act];
    [self presentViewController:alertC animated:YES completion:nil];
}
 
#pragma mark PrivateMethod
- (void)previewPictureWithUrl:(NSString *)picUrl {
    self.currentPreviewPhoto = picUrl;
    self.photos = [NSMutableArray array];
    [self.photos addObject:[MWPhoto photoWithURL:[NSURL URLWithString:picUrl]]];
    MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
    browser.zoomPhotosToFill = NO;
    [browser showDownloadBtn];
 
    [self.navigationController pushViewController:browser animated:NO];
}
 
 
@end