106 lines
3.7 KiB
Dart
106 lines
3.7 KiB
Dart
import 'dart:io';
|
||
import 'package:flutter/material.dart';
|
||
import 'package:get/get.dart';
|
||
import 'package:image_gallery_saver/image_gallery_saver.dart';
|
||
import 'package:permission_handler/permission_handler.dart';
|
||
import 'package:wallpaper/common/components/easy_loading.dart';
|
||
import 'package:wallpaper/common/components/wallpaper_picker.dart';
|
||
import 'package:wallpaper/common/models/wallpaper_model.dart';
|
||
import 'package:wallpaper/common/storage/favorite_data.dart';
|
||
import 'package:wallpaper/common/utils/device_info_util.dart';
|
||
import 'package:wallpaper/common/utils/download_util.dart';
|
||
import 'package:wallpaper/common/utils/obj_util.dart';
|
||
import 'package:wallpaper/common/utils/permission_util.dart';
|
||
import 'package:wallpaper/modules/favorite/favorite_controller.dart';
|
||
|
||
class WallpaperDetController extends GetxController {
|
||
late int position;
|
||
late final List<WallpaperData> wallpaperList;
|
||
late final PageController pageController;
|
||
final options = [OptionItem('Download', Icons.download)];
|
||
var filePath = '';
|
||
|
||
@override
|
||
void onInit() {
|
||
super.onInit();
|
||
bool isFavorite = Get.arguments['isFavorite'] ?? false;
|
||
position = Get.arguments['position'] ?? 0;
|
||
wallpaperList = Get.arguments['wallpaperList'] ?? <WallpaperData>[];
|
||
pageController = PageController(initialPage: position);
|
||
if (!isFavorite) {
|
||
options.add(OptionItem('Favorite', Icons.favorite));
|
||
}
|
||
if (Platform.isAndroid) {
|
||
options.add(OptionItem('Set Wallpaper', Icons.wallpaper));
|
||
}
|
||
}
|
||
|
||
/// 图片切换
|
||
void onPageChanged(int index) {
|
||
position = index;
|
||
filePath = '';
|
||
}
|
||
|
||
/// 选项事件
|
||
void optionOnTap(int index) {
|
||
if (index == 0) {
|
||
// 下载
|
||
if (Get.isSnackbarOpen) {
|
||
return;
|
||
}
|
||
DownloadUtil.downloadWallpaper('${wallpaperList[position].original}', (savePath) async {
|
||
filePath = savePath;
|
||
bool canSave = true;
|
||
if (Platform.isAndroid) {
|
||
// 在Android 10及以上版本,由于引入了分区存储(Scoped Storage)机制,如果应用只是需要访问媒体文件,而不是整个外部存储
|
||
// ImageGallerySaver使用MediaStore API来添加图片到相册,无需请求存储权限
|
||
// Android 10以下版本,需要申请存储权限
|
||
int sdkVersion = await DeviceInfoUtil.getSDKVersion();
|
||
if (sdkVersion < 29) {
|
||
bool status = await PermissionUtil.checkPermission([Permission.storage]);
|
||
canSave = status;
|
||
}
|
||
}
|
||
if (canSave) {
|
||
final result = await ImageGallerySaver.saveFile(savePath);
|
||
if (result['isSuccess']) {
|
||
toast('Saved to album');
|
||
} else {
|
||
toast('Unable to save to album');
|
||
}
|
||
}
|
||
});
|
||
} else if (index == 1) {
|
||
// 收藏
|
||
WallpaperData wallpaperData = wallpaperList[position];
|
||
if (FavoriteData().getWallpaperData().firstWhereOrNull((element) => element.original == wallpaperData.original) != null) {
|
||
toast('Wallpaper has been collected');
|
||
return;
|
||
}
|
||
FavoriteData().setWallpaperData(wallpaperData);
|
||
toast('Wallpaper has been collected');
|
||
// 刷新收藏列表
|
||
if (Get.isRegistered<FavoriteController>()) {
|
||
FavoriteController.to.refreshList();
|
||
}
|
||
} else {
|
||
// 设置壁纸
|
||
if (ObjUtil.isNotEmptyStr(filePath)) {
|
||
WallpaperPicker.picker(filePath);
|
||
} else {
|
||
DownloadUtil.downloadWallpaper('${wallpaperList[position].original}', (savePath) async {
|
||
filePath = savePath;
|
||
WallpaperPicker.picker(savePath);
|
||
});
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
class OptionItem {
|
||
late final String option;
|
||
late final IconData icons;
|
||
|
||
OptionItem(this.option, this.icons);
|
||
}
|