forked from lxm_flutter/FlutterUnit
✨ 逻辑优化
This commit is contained in:
@@ -22,7 +22,7 @@ class GlobalBloc extends Bloc<GlobalEvent, GlobalState> {
|
||||
|
||||
@override
|
||||
Stream<GlobalState> mapEventToState(GlobalEvent event) async* {
|
||||
// 程序初始化事件处理: 使用AppStorage进行初始化
|
||||
// 程序初始化事件处理: 使用 AppStorage 进行初始化
|
||||
if (event is EventInitApp) {
|
||||
yield await storage.initApp();
|
||||
}
|
||||
|
||||
@@ -21,7 +21,6 @@ abstract class CategoryRepository {
|
||||
|
||||
// 获取 所有收藏集 及 收藏集对应的组件 id 列表
|
||||
Future<List<CategoryTo>> loadCategoryData();
|
||||
Future<List<dynamic>> loadLikesData();
|
||||
|
||||
// 根据 Category 数据 同步 收藏集
|
||||
Future<bool> syncCategoryByData(String data,String likeData);
|
||||
|
||||
@@ -10,8 +10,6 @@ import 'package:flutter_unit/repositories/local_db.dart';
|
||||
import 'package:flutter_unit/repositories/rep/category_repository.dart';
|
||||
import 'package:sqflite/sqflite.dart';
|
||||
|
||||
import '../../app_start.dart';
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -99,32 +97,6 @@ class CategoryDbRepository implements CategoryRepository {
|
||||
return completer.future;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<dynamic>> loadLikesData() async {
|
||||
var likes = await db.rawQuery("SELECT id "
|
||||
"FROM widget WHERE collected = 1 ORDER BY family,lever DESC");
|
||||
var likesData = likes.map((e) => e['id']).toList();
|
||||
|
||||
return likesData;
|
||||
}
|
||||
|
||||
|
||||
Future<void> _setLikes(List<dynamic> ids) async {
|
||||
if(ids.isEmpty) return;
|
||||
String sql = 'UPDATE widget SET collected = 1 WHERE ';
|
||||
for(int i=0;i<ids.length;i++){
|
||||
if(i==0){
|
||||
sql += 'id = ${ids[i]} ';
|
||||
}else{
|
||||
sql += 'OR id = ${ids[i]} ';
|
||||
}
|
||||
}
|
||||
|
||||
await db.rawUpdate(sql, );
|
||||
List<Map<String, dynamic>> data = await db.rawQuery('SELECT id FROM widget WHERE collected = 1', []);
|
||||
print(data);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> syncCategoryByData(String data,String likeData) async {
|
||||
try {
|
||||
@@ -134,11 +106,14 @@ class CategoryDbRepository implements CategoryRepository {
|
||||
CategoryPo po = CategoryPo.fromNetJson(dataMap[i]["model"]);
|
||||
List<dynamic> widgetIds = dataMap[i]["widgetIds"];
|
||||
await addCategory(po);
|
||||
if(widgetIds.isNotEmpty){
|
||||
if (widgetIds.isNotEmpty) {
|
||||
await _categoryDao.addWidgets(po.id, widgetIds);
|
||||
}
|
||||
}
|
||||
await _setLikes(json.decode(likeData));
|
||||
List<int> likeWidgets = json.decode(likeData);
|
||||
for (int i = 0; i < likeWidgets.length; i++) {
|
||||
await LocalDb.instance.likeDao.like(likeWidgets[i]);
|
||||
}
|
||||
return true;
|
||||
} catch (e) {
|
||||
print(e);
|
||||
|
||||
@@ -40,22 +40,18 @@ class _BlocWrapperState extends State<BlocWrapper> {
|
||||
//使用MultiBlocProvider包裹
|
||||
providers: [
|
||||
//Bloc提供器
|
||||
BlocProvider<GlobalBloc>(
|
||||
create: (_) => GlobalBloc(storage)..add(const EventInitApp())),
|
||||
BlocProvider<GlobalBloc>(create: (_) => GlobalBloc(storage)..add(const EventInitApp())),
|
||||
|
||||
BlocProvider<WidgetsBloc>(
|
||||
create: (_) => WidgetsBloc(repository: repository)
|
||||
..add(EventTabTap(WidgetFamily.statelessWidget))),
|
||||
BlocProvider<WidgetsBloc>(create: (_) => WidgetsBloc(repository: repository)),
|
||||
|
||||
BlocProvider<DetailBloc>(
|
||||
create: (_) => DetailBloc(repository: repository)),
|
||||
|
||||
BlocProvider<CategoryBloc>(
|
||||
create: (_) => categoryBloc..add(EventLoadCategory())),
|
||||
create: (_) => categoryBloc),
|
||||
|
||||
BlocProvider<LikeWidgetBloc>(
|
||||
create: (_) => LikeWidgetBloc(repository: repository)
|
||||
..add(EventLoadLikeData())),
|
||||
create: (_) => LikeWidgetBloc(repository: repository)),
|
||||
|
||||
BlocProvider<RegisterBloc>(create: (_) => RegisterBloc()),
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ import 'package:flutter_unit/app/utils/http_utils/result_bean.dart';
|
||||
import 'package:flutter_unit/blocs/bloc_exp.dart';
|
||||
import 'package:flutter_unit/blocs/category/category_bloc.dart';
|
||||
import 'package:flutter_unit/model/category_model.dart';
|
||||
import 'package:flutter_unit/repositories/local_db.dart';
|
||||
import 'package:flutter_unit/repositories/rep/category_repository.dart';
|
||||
import 'package:flutter_unit/views/components/permanent/feedback_widget.dart';
|
||||
|
||||
@@ -75,8 +76,7 @@ class _SyncCategoryButtonState extends State<SyncCategoryButton> {
|
||||
// 说明请求成功
|
||||
if (result.data != null) {
|
||||
//说明有后台备份数据,进行同步操作
|
||||
CategoryRepository repository =
|
||||
BlocProvider.of<CategoryBloc>(context).repository;
|
||||
CategoryRepository repository = BlocProvider.of<CategoryBloc>(context).repository;
|
||||
await repository.syncCategoryByData(result.data.data,result.data.likeData);
|
||||
BlocProvider.of<CategoryBloc>(context).add(EventLoadCategory());
|
||||
BlocProvider.of<LikeWidgetBloc>(context).add(EventLoadLikeData());
|
||||
@@ -85,7 +85,7 @@ class _SyncCategoryButtonState extends State<SyncCategoryButton> {
|
||||
// 这里防止有傻孩子没点备份,就点同步,哥哥好心,给备份一下。
|
||||
CategoryRepository rep = BlocProvider.of<CategoryBloc>(context).repository;
|
||||
List<CategoryTo> loadCategories = await rep.loadCategoryData();
|
||||
List<dynamic> likeData = await rep.loadLikesData();
|
||||
List<int> likeData = await LocalDb.instance.likeDao.likeWidgetIds();
|
||||
|
||||
String json = jsonEncode(loadCategories);
|
||||
String likeJson = jsonEncode(likeData);
|
||||
|
||||
@@ -8,6 +8,7 @@ import 'package:flutter_unit/app/res/toly_icon.dart';
|
||||
import 'package:flutter_unit/app/utils/http_utils/result_bean.dart';
|
||||
import 'package:flutter_unit/blocs/category/category_bloc.dart';
|
||||
import 'package:flutter_unit/model/category_model.dart';
|
||||
import 'package:flutter_unit/repositories/local_db.dart';
|
||||
import 'package:flutter_unit/repositories/rep/category_repository.dart';
|
||||
import 'package:flutter_unit/views/components/permanent/feedback_widget.dart';
|
||||
|
||||
@@ -71,7 +72,8 @@ class _UploadCategoryButtonState extends State<UploadCategoryButton> {
|
||||
|
||||
CategoryRepository rep = BlocProvider.of<CategoryBloc>(context).repository;
|
||||
List<CategoryTo> loadCategories = await rep.loadCategoryData();
|
||||
List<dynamic> likeData = await rep.loadLikesData();
|
||||
List<int> likeData = await LocalDb.instance.likeDao.likeWidgetIds();
|
||||
|
||||
|
||||
String json = jsonEncode(loadCategories);
|
||||
String likeJson = jsonEncode(likeData);
|
||||
|
||||
@@ -10,10 +10,11 @@ import 'package:flutter_unit/app/utils/Toast.dart';
|
||||
import 'package:flutter_unit/app/utils/http_utils/result_bean.dart';
|
||||
import 'package:flutter_unit/blocs/bloc_exp.dart';
|
||||
import 'package:flutter_unit/model/category_model.dart';
|
||||
import 'package:flutter_unit/repositories/local_db.dart';
|
||||
import 'package:flutter_unit/repositories/rep/category_repository.dart';
|
||||
import 'package:flutter_unit/user_system/component/authentic_widget.dart';
|
||||
import 'package:sqflite/sqflite.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
import 'package:sqflite/sqflite.dart';
|
||||
|
||||
/// create by 张风捷特烈 on 2021/2/26
|
||||
/// contact me by email 1981462002@qq.com
|
||||
@@ -82,7 +83,8 @@ class DataManagePage extends StatelessWidget {
|
||||
void _doUploadCategoryData(BuildContext context) async {
|
||||
CategoryRepository rep = BlocProvider.of<CategoryBloc>(context).repository;
|
||||
List<CategoryTo> loadCategories = await rep.loadCategoryData();
|
||||
List<dynamic> likeData = await rep.loadLikesData();
|
||||
|
||||
List<int> likeData = await LocalDb.instance.likeDao.likeWidgetIds();
|
||||
|
||||
String json = jsonEncode(loadCategories);
|
||||
String likeJson = jsonEncode(likeData);
|
||||
@@ -116,7 +118,7 @@ class DataManagePage extends StatelessWidget {
|
||||
CategoryRepository rep =
|
||||
BlocProvider.of<CategoryBloc>(context).repository;
|
||||
List<CategoryTo> loadCategories = await rep.loadCategoryData();
|
||||
List<dynamic> likeData = await rep.loadLikesData();
|
||||
List<int> likeData = await LocalDb.instance.likeDao.likeWidgetIds();
|
||||
|
||||
String json = jsonEncode(loadCategories);
|
||||
String likeJson = jsonEncode(likeData);
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flutter_unit/app/res/str_unit.dart';
|
||||
import 'package:flutter_unit/app/router/unit_router.dart';
|
||||
import 'package:flutter_unit/blocs/bloc_exp.dart';
|
||||
import 'package:flutter_unit/model/enums.dart';
|
||||
import 'package:flutter_unit/views/pages/splash/splash_bottom.dart';
|
||||
|
||||
import 'unit_paint.dart';
|
||||
|
||||
/// create by 张风捷特烈 on 2020-03-07
|
||||
@@ -58,24 +62,25 @@ class _UnitSplashState extends State<UnitSplash> with TickerProviderStateMixin {
|
||||
final double winH = MediaQuery.of(context).size.height;
|
||||
final double winW = MediaQuery.of(context).size.width;
|
||||
|
||||
return Scaffold(
|
||||
body: Stack(
|
||||
alignment: Alignment.center,
|
||||
children: <Widget>[
|
||||
_buildFlutterLogo(),
|
||||
Container(
|
||||
width: winW,
|
||||
height: winH,
|
||||
child: CustomPaint(
|
||||
painter: UnitPainter(repaint: _controller),
|
||||
return BlocListener<GlobalBloc, GlobalState>(
|
||||
listener: _listenStart,
|
||||
child: Scaffold(
|
||||
body: Stack(
|
||||
alignment: Alignment.center,
|
||||
children: <Widget>[
|
||||
_buildFlutterLogo(),
|
||||
Container(
|
||||
width: winW,
|
||||
height: winH,
|
||||
child: CustomPaint(
|
||||
painter: UnitPainter(repaint: _controller),
|
||||
),
|
||||
),
|
||||
),
|
||||
_buildFlutterUnitText(winH, winW),
|
||||
_buildHead(),
|
||||
Positioned(
|
||||
bottom: 15,
|
||||
child: SplashBottom())
|
||||
],
|
||||
_buildFlutterUnitText(winH, winW),
|
||||
_buildHead(),
|
||||
Positioned(bottom: 15, child: SplashBottom())
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -138,4 +143,11 @@ class _UnitSplashState extends State<UnitSplash> with TickerProviderStateMixin {
|
||||
width: 45,
|
||||
child: Image.asset('assets/images/icon_head.webp'),
|
||||
));
|
||||
|
||||
// 监听资源加载完毕,启动,触发事件
|
||||
void _listenStart(BuildContext context, GlobalState state) {
|
||||
BlocProvider.of<WidgetsBloc>(context).add(EventTabTap(WidgetFamily.statelessWidget));
|
||||
BlocProvider.of<LikeWidgetBloc>(context).add(EventLoadLikeData());
|
||||
BlocProvider.of<CategoryBloc>(context).add(EventLoadCategory());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,19 +3,18 @@ import 'dart:math';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flutter_unit/app/utils/convert.dart';
|
||||
|
||||
import 'package:flutter_unit/app/router/unit_router.dart';
|
||||
import 'package:flutter_unit/app/utils/convert.dart';
|
||||
import 'package:flutter_unit/blocs/bloc_exp.dart';
|
||||
import 'package:flutter_unit/model/enums.dart';
|
||||
import 'package:flutter_unit/model/widget_model.dart';
|
||||
import 'package:flutter_unit/views/components/permanent/feedback_widget.dart';
|
||||
import 'package:flutter_unit/views/components/project/overlay_tool_wrapper.dart';
|
||||
import 'package:flutter_unit/views/components/project/default/empty_shower.dart';
|
||||
import 'package:flutter_unit/views/components/project/default/error_shower.dart';
|
||||
import 'package:flutter_unit/views/components/project/default/loading_shower.dart';
|
||||
import 'package:flutter_unit/views/components/project/no_more_widget.dart';
|
||||
|
||||
import 'package:flutter_unit/model/widget_model.dart';
|
||||
import 'package:flutter_unit/views/components/project/items/widget/home_item_support.dart';
|
||||
import 'package:flutter_unit/views/components/project/no_more_widget.dart';
|
||||
import 'package:flutter_unit/views/components/project/overlay_tool_wrapper.dart';
|
||||
import 'package:flutter_unit/views/pages/widget_home/toly_app_bar.dart';
|
||||
|
||||
import 'background.dart';
|
||||
@@ -30,8 +29,11 @@ class _HomePageState extends State<HomePage>
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
WidgetsBinding.instance.addPostFrameCallback(
|
||||
(_) => OverlayToolWrapper.of(context).showFloating());
|
||||
WidgetsBinding.instance.addPostFrameCallback(_onFrameCallBack);
|
||||
}
|
||||
|
||||
void _onFrameCallBack(Duration timeStamp) {
|
||||
OverlayToolWrapper.of(context).showFloating();
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
Reference in New Issue
Block a user