!27 优化图库代码

Merge pull request !27 from sunjunxiong/master
This commit is contained in:
openharmony_ci 2021-10-08 02:38:40 +00:00 committed by Gitee
commit cec466c538
6 changed files with 248 additions and 88 deletions

View File

@ -26,7 +26,7 @@ let media = mediaLibrary.getMediaLibraryHelper();
const PROGRESS = 100;
// 调用operations延时
const OPERATION_TIME = 50;
const OPERATION_TIME = 200;
// 照片类型
const PHOTO_TYPE = 3;
@ -86,7 +86,9 @@ export default {
let self = this;
setTimeout(() => {
self.$element('progress_dialog').show();
this.operations();
setTimeout(() => {
self.operations();
}, OPERATION_TIME);
}, OPERATION_TIME);
},

View File

@ -54,6 +54,9 @@ const PAGE_SIZE = 10;
// 视频类型
const VIDEO_TYPE = 4;
// promise执行延时
const PROMISE_TIME = 100;
export default {
data: {
headTitle: '',
@ -152,6 +155,8 @@ export default {
onShow() {
this.utils.logDebug('main => onShow');
if (this.$app.$def.dataManage.getRefreshed()) {
this.showEmptyDiv = true;
this.showEmptyDiv = false;
this.loadData();
}
},
@ -225,24 +230,7 @@ export default {
self.headSource = allRes;
}
});
self.getAlbums().then(albumRes => {
if (albumRes.grid || albumRes.list) {
self.gridData = albumRes.grid;
self.cacheOtherList = albumRes.list;
if (albumRes.list && albumRes.list.length > PAGE_SIZE) {
self.listData = albumRes.list.slice(0, PAGE_SIZE);
} else {
self.listData = albumRes.list;
}
self.getVideoAlbum().then(videoRes => {
if (videoRes.src) {
self.cacheAlbums.push(videoRes);
self.gridData.push(videoRes);
}
});
self.utils.logDebug('main albumRes' + self.listData.length);
}
});
self.getAlbums();
self.$app.$def.dataManage.isRefreshed(false);
},
@ -278,32 +266,105 @@ export default {
},
/**
* 获取所有相册数据
* 获取所有视频数据
*
* @return {Promise} - 返回获取所有相册promise
* @return {Promise} - 返回获取视频promise
*/
getAlbums() {
this.utils.logDebug('main => getAlbums => startTime');
getAllVideoAlbum() {
let self = this;
self.utils.logDebug('main => getAllVideoAlbum => startTime');
let args = {
selections: '',
selectionArgs: ['imagealbum'],
selectionArgs: ['imagealbum', 'videoalbum'],
};
return new Promise(function (resolve, reject) {
media.getImageAlbums(args, (error, albums) => {
self.utils.logDebug('main => getAlbums => endTime');
return new Promise((resolve, reject) => {
media.getVideoAlbums(args, (error, albums) => {
self.utils.logDebug('main => getAllVideoAlbum => endTime');
if (albums) {
self.cacheAlbums = albums;
self.dealImageAlbums(albums).then(res => {
resolve(res);
});
resolve(albums);
} else {
reject(new Error('get data error'));
resolve([]);
}
});
});
},
/**
* 获取所有照片数据
*
* @return {Promise} - 返回获取照片promise
*/
getAllImageAlbum() {
let self = this;
self.utils.logDebug('main => getAllImageAlbum => startTime');
let args = {
selections: '',
selectionArgs: ['imagealbum', 'videoalbum'],
};
return new Promise((resolve, reject) => {
media.getImageAlbums(args, (error, albums) => {
self.utils.logDebug('main => getAllImageAlbum => endTime');
if (albums) {
resolve(albums);
} else {
resolve([]);
}
});
});
},
/**
* 获取所有相册数据
*/
getAlbums() {
let self = this;
self.utils.logDebug('main => getAlbums => startTime');
let videoPromise = self.getAllVideoAlbum();
let imagePromise = self.getAllImageAlbum();
Promise.all([videoPromise, imagePromise]).then(result => {
let allAlbums = result[0].concat(result[1]) || [];
let array = [];
let obj = {};
allAlbums.forEach(item => {
if (!obj[item.albumName]) {
array.push(item);
obj[item.albumName] = 1;
} else {
obj[item.albumName]++;
}
});
allAlbums = array;
self.cacheAlbums = allAlbums;
self.dealImageAlbums(allAlbums).then(albumRes => {
if (albumRes.grid || albumRes.list) {
self.gridData = albumRes.grid;
self.cacheOtherList = albumRes.list;
if (albumRes.list && albumRes.list.length > PAGE_SIZE) {
self.listData = albumRes.list.slice(0, PAGE_SIZE);
} else {
self.listData = albumRes.list;
}
self.dealGetVideoAlbum();
}
});
});
},
/**
* 获取视频数据
*/
dealGetVideoAlbum() {
let self = this;
self.getVideoAlbum().then(videoRes => {
self.utils.logDebug('main => getAlbums => endTime');
if (videoRes.src && self.gridData.length < 2) {
self.cacheAlbums.push(videoRes);
self.gridData.push(videoRes);
}
});
},
/**
* 处理图片相册数据
*
@ -311,6 +372,7 @@ export default {
*/
dealImageAlbums(albums) {
let self = this;
self.utils.logDebug('main => dealImageAlbums => startTime');
return new Promise(function (resolve, reject) {
let list = [];
let grid = [];
@ -328,6 +390,7 @@ export default {
list: []
};
self.getAlbumImage(album).then((res) => {
self.utils.logDebug('main => dealImageAlbums => endTime');
if (res && res.length > 0) {
if (res[0].mediaType === VIDEO_TYPE) {
gridObj.src = '/common/image/icon/video_poster.png';
@ -361,13 +424,15 @@ export default {
* @return {Promise} - 返回获取相册图片promise
*/
getAlbumImage(album) {
this.utils.logDebug('main => getAlbumImage => startTime');
let self = this;
self.utils.logDebug('main => getAlbumImage => startTime');
let args = {
selections: album.albumName,
selectionArgs: ['imagealbum', 'videoalbum'],
};
return new Promise(function (resolve, reject) {
media.getMediaAssets(args, (error, images) => {
self.utils.logDebug('main => getAlbumImage => endTime');
if (images !== null || images !== undefined) {
resolve(images);
} else {
@ -430,8 +495,8 @@ export default {
* @param {string} albumName - 新建相册名
*/
createAlbum(albumName) {
this.utils.logDebug('main => createAlbum => startTime');
let self = this;
self.utils.logDebug('main => createAlbum => startTime');
self.$element('create_dialog').close();
media.createAlbum((err, album) => {
this.utils.logDebug('main => createAlbum => err' + err);
@ -499,21 +564,31 @@ export default {
* @param {string} albumName - 相册名
*/
modifyAlbumName(albumName) {
this.utils.logDebug('main => modifyAlbumName => startTime');
let self = this;
self.utils.logDebug('main => modifyAlbumName => startTime');
self.$element('rename_dialog').close();
let args = {
selections: '',
selectionArgs: ['imagealbum'],
};
media.getImageAlbums(args, (error, albums) => {
if (albums) {
for (let j = 0; j < albums.length; j++) {
let item = albums[j];
let videoPromise = self.getAllVideoAlbum();
let imagePromise = self.getAllImageAlbum();
setTimeout(() => {
Promise.all([videoPromise, imagePromise]).then(result => {
let allAlbums = result[0].concat(result[1]) || [];
let array = [];
let obj = {};
allAlbums.forEach(item => {
if (!obj[item.albumName]) {
array.push(item);
obj[item.albumName] = 1;
} else {
obj[item.albumName]++;
}
});
allAlbums = array;
for (let j = 0; j < allAlbums.length; j++) {
let item = allAlbums[j];
self.dealCommitModifyAlbum(item, albumName);
}
}
});
});
}, PROMISE_TIME);
},
/**

View File

@ -261,7 +261,7 @@ export default {
self.utils.logDebug('photoDetail => getVideoAsset => endTime');
self.cacheList = videos || [];
if (videos && videos.length > 0) {
let list = self.dealListData(videos || []);
let list = self.utils.deepCopy(self.dealListData(videos || []));
if (self.refreshType === DELETE_REFRESH_TYPE) {
if (self.shareIndex > 0) {
self.shareIndex--;
@ -330,7 +330,7 @@ export default {
media.getMediaAssets(args, (error, images) => {
self.utils.logDebug('photoDetail => getAlbumAsset => endTime');
self.cacheList = images || [];
let list = self.dealListData(images || []);
let list = self.utils.deepCopy(self.dealListData(images || []));
if (self.refreshType === DELETE_REFRESH_TYPE) {
if (self.shareIndex > 0) {
self.shareIndex--;
@ -384,7 +384,6 @@ export default {
if (self.list.length > 0) {
let item = self.list[self.shareIndex];
self.currentItem = item;
self.utils.logDebug(' === > ' + JSON.stringify(self.currentItem) + ' ');
self.topBarSource.title = self.currentItem.name;
if (self.currentItem.mediaType === VIDEO_TYPE) {
self.showScale = false;
@ -438,7 +437,6 @@ export default {
* 滑动回调
*
* @param {Object} item - 滑动后当前显示项
* @return {boolean} Verify result
*/
swiperChange(item) {
this.utils.logDebug('photoDetail => swiperChange');
@ -461,6 +459,15 @@ export default {
} else {
this.topBarSource.title = this.currentItem.name;
}
this.swiperBottom();
},
/**
* 滑动到最后
*
* @return {boolean} Verify result
*/
swiperBottom() {
let cacheLen = this.cacheSource.length;
let listLen = this.list.length;
if (this.shareIndex === listLen - 1) {
@ -886,7 +893,6 @@ export default {
* @param {Object} item - 列表点击当前项
*/
videoPause(item) {
item.isPause = true;
this.utils.logDebug('videoPause');
},

View File

@ -31,6 +31,9 @@ const PAGE_SIZE = 10;
// 视频类型
const VIDEO_TYPE = 4;
// promise执行延时
const PROMISE_TIME = 100;
export default {
data: {
listData: [],
@ -85,51 +88,126 @@ export default {
},
/**
* 获取相册
* 获取所有视频数据
*
* @return {Promise} - 返回获取视频promise
*/
getAlbums() {
getAllVideoAlbum() {
let self = this;
self.utils.logDebug('selectAlbum => getAllVideoAlbum => startTime');
let args = {
selections: '',
selectionArgs: ['imagealbum'],
selectionArgs: ['imagealbum', 'videoalbum'],
};
self.utils.logDebug('selectAlbum => getImageAlbums => startTime');
media.getImageAlbums(args, (error, albums) => {
self.utils.logDebug('selectAlbum => getImageAlbums => endTime');
if (albums) {
let list = [];
let others = [];
for (let i = 0; i < albums.length; i++) {
let album = albums[i];
let gridObj = {
name: album.albumName,
id: album.albumId,
icon: '/common/image/svg/arrow-right.svg',
checked: false,
showNumber: true,
src: self.$app.$def.utils.getIcon('default'),
list: []
};
setTimeout(() => {
self.getMediaAssets(gridObj, album, list, others);
}, (i + 1) * LOAD_DATA_TIME);
return new Promise((resolve, reject) => {
media.getVideoAlbums(args, (error, albums) => {
self.utils.logDebug('selectAlbum => getAllVideoAlbum => endTime');
if (albums) {
resolve(albums);
} else {
resolve([]);
}
setTimeout(() => {
if (others.length > PAGE_SIZE) {
self.otherList = others.slice(0, PAGE_SIZE);
} else {
self.otherList = others;
}
self.cacheList = others;
self.listData = list;
}, (albums.length + 1) * LOAD_DATA_TIME);
}
});
});
},
/**
* 获取所有照片数据
*
* @return {Promise} - 返回获取照片promise
*/
getAllImageAlbum() {
let self = this;
self.utils.logDebug('selectAlbum => getAllImageAlbum => startTime');
let args = {
selections: '',
selectionArgs: ['imagealbum', 'videoalbum'],
};
return new Promise((resolve, reject) => {
media.getImageAlbums(args, (error, albums) => {
self.utils.logDebug('selectAlbum => getAllImageAlbum => endTime');
if (albums) {
resolve(albums);
} else {
resolve([]);
}
});
});
},
/**
* 获取相册
*/
getAlbums() {
let self = this;
self.utils.logDebug('selectAlbum => getAlbums => startTime');
let videoPromise = self.getAllVideoAlbum();
let imagePromise = self.getAllImageAlbum();
setTimeout(() => {
Promise.all([videoPromise, imagePromise]).then(result => {
let allAlbums = result[0].concat(result[1]) || [];
let array = [];
let obj = {};
allAlbums.forEach(item => {
if (!obj[item.albumName]) {
array.push(item);
obj[item.albumName] = 1;
} else {
obj[item.albumName]++;
}
});
allAlbums = array;
self.setAlbumList(allAlbums);
self.utils.logDebug('selectAlbum => getAlbums => endTime');
});
}, PROMISE_TIME);
},
/**
* 获取相册
*
* @param {Array} allAlbums - 相册数据
*/
setAlbumList(allAlbums) {
let self = this;
if (allAlbums.length > 0) {
let list = [];
let others = [];
for (let i = 0; i < allAlbums.length; i++) {
let album = allAlbums[i];
let gridObj = {
name: album.albumName,
id: album.albumId,
icon: '/common/image/svg/arrow-right.svg',
checked: false,
showNumber: true,
src: self.$app.$def.utils.getIcon('default'),
list: []
};
setTimeout(() => {
self.getMediaAssets(gridObj, album, list, others);
}, (i + 1) * LOAD_DATA_TIME);
}
setTimeout(() => {
if (others.length > PAGE_SIZE) {
self.otherList = others.slice(0, PAGE_SIZE);
} else {
self.otherList = others;
}
self.cacheList = others;
self.listData = list;
}, (allAlbums.length + 1) * LOAD_DATA_TIME);
}
},
/**
* 获取相册
*
* @param {Object} gridObj - 数据对象
* @param {Object} album - 被操作对象
* @param {Array} list - 相册数据
* @param {Array} others - 其他相册数据
*/
getMediaAssets(gridObj, album, list, others) {
let self = this;
let args = {
@ -141,7 +219,8 @@ export default {
self.utils.logDebug('selectAlbum => getMediaAssets => endTime =>');
if (images && images.length > 0) {
gridObj.src = images[0].mediaType === VIDEO_TYPE
? '/common/image/icon/video_poster.png' : 'file://' + images[0].URI;
? '/common/image/icon/video_poster.png'
: 'file://' + images[0].URI;
gridObj.list = images;
}
// 判断是新建选相册 还是操作选相册

View File

@ -42,9 +42,6 @@
</image>
<image if="{{ selectMode }}" class="select" src="{{ item.icon }}">
</image>
<text else class="video-duration" onclick="videoClick({{ item }},{{ index }})">
{{ item.duration }}
</text>
</stack>
</div>
</list-item>

View File

@ -541,6 +541,7 @@ export default {
if (commitFlag) {
self.$app.$def.dataManage.isRefreshed(true);
self.selectMode = false;
self.$app.$def.dataManage.setPhotoList([]);
setTimeout(() => {
if (choose.length === self.listData.length) {
self.listData = [];