75 lines
1.8 KiB
Dart
75 lines
1.8 KiB
Dart
import 'package:get/get.dart';
|
|
import 'package:wallpaper/common/components/dialog/hint_dialog.dart';
|
|
import 'package:wallpaper/common/components/view_state_widget.dart';
|
|
import 'package:wallpaper/common/models/wallpaper_model.dart';
|
|
import 'package:wallpaper/common/storage/favorite_data.dart';
|
|
import 'package:wallpaper/routes/app_pages.dart';
|
|
|
|
class FavoriteController extends GetxController {
|
|
static FavoriteController get to => Get.find<FavoriteController>();
|
|
var viewState = ViewState.loading;
|
|
late List<WallpaperData> wallpaperList;
|
|
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
_getData();
|
|
}
|
|
|
|
/// 获取数据
|
|
void _getData() {
|
|
wallpaperList = FavoriteData().getWallpaperData().reversed.toList();
|
|
_changeViewState();
|
|
}
|
|
|
|
/// 改变页面状态
|
|
void _changeViewState() {
|
|
viewState = wallpaperList.isNotEmpty ? ViewState.normal : ViewState.empty;
|
|
}
|
|
|
|
/// 点击壁纸
|
|
void itemOnTap(int index) async {
|
|
Get.toNamed(AppPages.wallpaperDet, arguments: {
|
|
'isFavorite': true,
|
|
'position': index,
|
|
'wallpaperList': wallpaperList
|
|
});
|
|
}
|
|
|
|
/// 长按壁纸
|
|
void onLongPress(int index) {
|
|
Get.dialog(
|
|
barrierDismissible: false,
|
|
HintDialog(
|
|
content: 'Are you sure you want to delete the wallpaper?',
|
|
confirmOnTap: () {
|
|
FavoriteData().delete(index);
|
|
refreshList();
|
|
},
|
|
),
|
|
);
|
|
}
|
|
|
|
/// 删除所有壁纸
|
|
void deleteAll() {
|
|
Get.dialog(
|
|
barrierDismissible: false,
|
|
HintDialog(
|
|
content: 'Are you sure to delete all wallpapers?',
|
|
confirmOnTap: () {
|
|
FavoriteData().clear();
|
|
wallpaperList.clear();
|
|
_changeViewState();
|
|
refresh();
|
|
},
|
|
),
|
|
);
|
|
}
|
|
|
|
/// 刷新
|
|
void refreshList() {
|
|
_getData();
|
|
refresh();
|
|
}
|
|
}
|