From 742f351b29f2c9181dbd3d3ff180a3338b70b3e4 Mon Sep 17 00:00:00 2001 From: toly <1981462002@qq.com> Date: Wed, 24 Feb 2021 20:15:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0key.properties?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- android/.gitignore | 1 - android/app/build.gradle | 8 +- lib/app/api/category_api.dart | 20 ++- lib/app/res/path_unit.dart | 7 +- lib/app/utils/http_utils/result_bean.dart | 8 ++ lib/model/category_model.dart | 12 +- .../impl/catagory_db_repository.dart | 25 +++- lib/repositories/itf/category_repository.dart | 4 + lib/storage/dao/category_dao.dart | 50 ++++++-- lib/storage/po/category_po.dart | 25 ++++ lib/user_system/pages/login/login_form.dart | 2 +- lib/views/pages/category/collect_page.dart | 120 ++---------------- .../pages/category/sync/async_button.dart | 106 ++++++++++++++++ .../pages/category/sync/upload_button.dart | 104 +++++++++++++++ 14 files changed, 348 insertions(+), 144 deletions(-) create mode 100644 lib/views/pages/category/sync/async_button.dart create mode 100644 lib/views/pages/category/sync/upload_button.dart diff --git a/android/.gitignore b/android/.gitignore index 82ed62c..bc2100d 100644 --- a/android/.gitignore +++ b/android/.gitignore @@ -4,5 +4,4 @@ gradle-wrapper.jar /gradlew /gradlew.bat /local.properties -/key.properties GeneratedPluginRegistrant.java diff --git a/android/app/build.gradle b/android/app/build.gradle index e135c38..3994ae8 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -54,10 +54,10 @@ android { signingConfigs { release { - keyAlias keystoreProperties['keyAlias'] - keyPassword keystoreProperties['keyPassword'] - storeFile file(keystoreProperties['storeFile']) - storePassword keystoreProperties['storePassword'] +// keyAlias keystoreProperties['keyAlias'] +// keyPassword keystoreProperties['keyPassword'] +// storeFile file(keystoreProperties['storeFile']) +// storePassword keystoreProperties['storePassword'] } } buildTypes { diff --git a/lib/app/api/category_api.dart b/lib/app/api/category_api.dart index ad1508d..4decf13 100644 --- a/lib/app/api/category_api.dart +++ b/lib/app/api/category_api.dart @@ -7,9 +7,9 @@ import 'package:flutter_unit/app/utils/http_utils/result_bean.dart'; /// 说明: class CategoryApi { - static Future> uploadCategoryData(String data) async { - HttpUtil.getInstance().client.get(PathUnit.register); + + static Future> uploadCategoryData(String data) async { var result = await HttpUtil.getInstance() .client .post(PathUnit.categoryDataSync, data: data) @@ -23,4 +23,20 @@ class CategoryApi { return ResultBean.error('请求错误'); } + static Future> getCategoryData() async { + var result = await HttpUtil.getInstance() + .client + .get(PathUnit.categoryData) + .catchError((err) { + return ResultBean.error('请求错误: ${err.toString()}'); + }); + + if (result.data != null && result.data['data']!=null) { + var dataStr = result.data['data']['data']; + return ResultBean.ok(dataStr); + } + + return ResultBean.error('请求错误'); + } + } diff --git a/lib/app/res/path_unit.dart b/lib/app/res/path_unit.dart index 6f3180d..1e3620c 100644 --- a/lib/app/res/path_unit.dart +++ b/lib/app/res/path_unit.dart @@ -4,12 +4,15 @@ class PathUnit{ - static const baseUrl='http://119.45.173.197:8080/api/v1'; - // static const baseUrl='http://192.168.0.104:8080/api/v1'; + // static const baseUrl='http://119.45.173.197:8080/api/v1'; + static const baseUrl='http://192.168.0.104:8080/api/v1'; static const sendEmail = '/sendEmail/'; static const register = '/register'; + static const categoryDataSync = '/categoryData/sync'; + static const categoryData = '/categoryData'; + static const login = '/login'; } \ No newline at end of file diff --git a/lib/app/utils/http_utils/result_bean.dart b/lib/app/utils/http_utils/result_bean.dart index e93e692..545bfa9 100644 --- a/lib/app/utils/http_utils/result_bean.dart +++ b/lib/app/utils/http_utils/result_bean.dart @@ -25,4 +25,12 @@ class ResultBean { status: false, ); } + + static ResultBean ok(T data) { + return ResultBean( + msg: '请求成功', + data: data, + status: true, + ); + } } diff --git a/lib/model/category_model.dart b/lib/model/category_model.dart index 53d0eb9..84abe70 100644 --- a/lib/model/category_model.dart +++ b/lib/model/category_model.dart @@ -55,22 +55,12 @@ class CategoryModel extends Equatable { String toString() { return 'CategoryModel{id: $id, name: $name, info: $info, createDate: $createDate, imageCover: $imageCover, count: $count, color: $color}'; } - - Map toJson() => { - "id": this.id, - "name": this.name, - "info": this.info, - "createDate": this.createDate, - "imageCover": this.imageCover, - "count": this.count, - "color": this.color.value, - }; } // 收藏集的 To 对象,用与上传/同步 收藏集 class CategoryTo{ - final CategoryModel model; + final CategoryPo model; final List widgetIds; CategoryTo({this.model, this.widgetIds}); diff --git a/lib/repositories/impl/catagory_db_repository.dart b/lib/repositories/impl/catagory_db_repository.dart index 7479b2e..ee62eba 100644 --- a/lib/repositories/impl/catagory_db_repository.dart +++ b/lib/repositories/impl/catagory_db_repository.dart @@ -1,4 +1,5 @@ import 'dart:async'; +import 'dart:convert'; import 'package:flutter_unit/model/category_model.dart'; import 'package:flutter_unit/model/widget_model.dart'; @@ -69,7 +70,6 @@ class CategoryDbRepository implements CategoryRepository { return success != -1; } - @override Future> loadCategoryData() async { List> data = await _categoryDao.queryAll(); @@ -82,7 +82,8 @@ class CategoryDbRepository implements CategoryRepository { for (int i = 0; i < data.length; i++) { List ids = await _categoryDao.loadCollectWidgetIds(data[i]['id']); - collects.add(CategoryTo(widgetIds: ids, model: CategoryModel.fromPo(CategoryPo.fromJson(data[i])))); + collects + .add(CategoryTo(widgetIds: ids, model: CategoryPo.fromJson(data[i]))); if (i == data.length - 1) { completer.complete(collects); @@ -92,4 +93,24 @@ class CategoryDbRepository implements CategoryRepository { return completer.future; } + @override + Future syncCategoryByData(String data) async { + try { + await _categoryDao.clear(); + List dataMap = json.decode(data); + for (int i = 0; i < dataMap.length; i++) { + CategoryPo po = CategoryPo.fromNetJson(dataMap[i]["model"]); + List widgetIds = dataMap[i]["widgetIds"]; + await addCategory(po); + await _categoryDao.addWidgets(po.id, widgetIds); + // for (int j = 0; j < widgetIds.length; j++) { + // await _categoryDao.addWidget(po.id, widgetIds[j]); + // } + } + return true; + } catch (e) { + print(e); + return false; + } + } } diff --git a/lib/repositories/itf/category_repository.dart b/lib/repositories/itf/category_repository.dart index b74b2fa..69cb265 100644 --- a/lib/repositories/itf/category_repository.dart +++ b/lib/repositories/itf/category_repository.dart @@ -22,6 +22,10 @@ abstract class CategoryRepository { // 获取 所有收藏集 及 收藏集对应的组件 id 列表 Future> loadCategoryData(); + // 根据 Category 数据 同步 收藏集 + Future syncCategoryByData(String data); + + //添加收藏集 Future addCategory(CategoryPo categoryPo); diff --git a/lib/storage/dao/category_dao.dart b/lib/storage/dao/category_dao.dart index cbd54d5..8ce9542 100644 --- a/lib/storage/dao/category_dao.dart +++ b/lib/storage/dao/category_dao.dart @@ -26,21 +26,22 @@ class CategoryDao { Future get _db async =>await storage.db; - Future insert(CategoryPo widget) async { + Future insert(CategoryPo category) async { //插入方法 final db = await _db; String addSql = //插入数据 "INSERT INTO " - "category(name,color,info,priority,image,created,updated) " - "VALUES (?,?,?,?,?,?,?);"; + "category(id,name,color,info,priority,image,created,updated) " + "VALUES (?,?,?,?,?,?,?,?);"; return await db.transaction((tran) async => await tran.rawInsert(addSql, [ - widget.name, - widget.color, - widget.info, - widget.priority, - widget.image, - widget.created.toIso8601String(), - widget.updated.toIso8601String(), + category.id, + category.name, + category.color, + category.info, + category.priority, + category.image, + category.created.toIso8601String(), + category.updated.toIso8601String(), ])); } @@ -77,6 +78,25 @@ class CategoryDao { ])); } + Future addWidgets(int categoryId,List widgetIds) async { + final db = await _db; + String addSql = //插入数据 + "INSERT INTO " + "category_widget(widgetId,categoryId) VALUES "; + + String args = ''; + + for(int i=0;i< widgetIds.length;i++){ + args+= "(${widgetIds[i]},$categoryId)"; + if(i==widgetIds.length-1){ + args+=";"; + }else{ + args+=","; + } + } + addSql += args; + return await db.transaction((tran) async => await tran.rawInsert(addSql)); + } Future existByName(String name) async { final db = await _db; @@ -125,6 +145,16 @@ class CategoryDao { [id]); } + Future clear() async { + final db = await _db; + await db.execute( + "DELETE FROM category_widget " + "WHERE categoryId >0"); + return await db.execute( + "DELETE FROM category " + "WHERE id > 0"); + } + Future removeWidget(int categoryId, int widgetId) async { //插入方法 final db = await _db; diff --git a/lib/storage/po/category_po.dart b/lib/storage/po/category_po.dart index 09890d1..ce98621 100644 --- a/lib/storage/po/category_po.dart +++ b/lib/storage/po/category_po.dart @@ -1,4 +1,5 @@ import 'package:equatable/equatable.dart'; +import 'package:flutter_unit/model/category_model.dart'; /// create by 张风捷特烈 on 2020-04-17 /// contact me by email 1981462002@qq.com @@ -51,6 +52,30 @@ class CategoryPo extends Equatable { info: map["info"]); } + factory CategoryPo.fromNetJson(Map map) { + return CategoryPo( + id: map['id'], + name: map['name'], + color: map["color"], + created: DateTime.fromMillisecondsSinceEpoch(map["created"]), + image: map["image"], + priority: map["priority"], + count: map["count"], + updated: DateTime.fromMillisecondsSinceEpoch(map["updated"]), + info: map["info"]); + } + + Map toJson() => { + "id": this.id, + "name": this.name, + "info": this.info, + "created": this.created.millisecondsSinceEpoch, + "updated": this.updated.millisecondsSinceEpoch, + "image": this.image, + "count": this.count, + "color": this.color, + "priority": this.priority, + }; @override String toString() { diff --git a/lib/user_system/pages/login/login_form.dart b/lib/user_system/pages/login/login_form.dart index eb80526..10249af 100644 --- a/lib/user_system/pages/login/login_form.dart +++ b/lib/user_system/pages/login/login_form.dart @@ -16,7 +16,7 @@ class LoginFrom extends StatefulWidget { class _LoginFromState extends State { final _usernameController = TextEditingController(text: '张风捷特烈'); - final _passwordController = TextEditingController(text: ''); + final _passwordController = TextEditingController(text: '111111'); bool _showPwd = false; diff --git a/lib/views/pages/category/collect_page.dart b/lib/views/pages/category/collect_page.dart index d81a30a..89b24ee 100644 --- a/lib/views/pages/category/collect_page.dart +++ b/lib/views/pages/category/collect_page.dart @@ -9,13 +9,12 @@ import 'package:flutter_unit/app/res/toly_icon.dart'; import 'package:flutter_unit/app/router/unit_router.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/itf/category_repository.dart'; import 'package:flutter_unit/user_system/component/authentic_widget.dart'; import 'package:flutter_unit/views/components/permanent/circle_image.dart'; import 'package:flutter_unit/views/components/permanent/feedback_widget.dart'; -import 'package:path_provider/path_provider.dart'; +import 'package:flutter_unit/views/pages/category/sync/upload_button.dart'; +import 'sync/async_button.dart'; import 'category_page.dart'; import 'like_widget_page.dart'; @@ -75,8 +74,13 @@ class _CollectPageState extends State )), backgroundColor: BlocProvider.of(context).state.color, actions: [ - AuthenticWidget.just(SyncCategoryButton()), - AuthenticWidget.just(_buildLoadAction(context)), + SizedBox( + width: 32, + child: AuthenticWidget.just(UploadCategoryButton())), + // SizedBox(width: 5,), + SizedBox( + width: 32, + child: AuthenticWidget.just(SyncCategoryButton())), _buildAddAction(context) ], title: Text( @@ -128,117 +132,11 @@ class _CollectPageState extends State ), onPressed: () => Scaffold.of(context).openEndDrawer()); - Widget _buildLoadAction(BuildContext context) { - return Padding( - padding: const EdgeInsets.only(left: 8.0), - child: FeedbackWidget( - child: Icon( - TolyIcon.download, - size: 28, - ), - onPressed: () async { - final dir = await getExternalStorageDirectory(); - CategoryRepository rep = - BlocProvider.of(context).repository; - List loadCategories = await rep.loadCategoryData(); - String json1 = jsonEncode(loadCategories); - loadCategories.forEach((element) { - print("${element.model.name} - ${element.widgetIds} "); - }); - print(json1); - File backFile = File(dir.path + "/back.txt"); - backFile.writeAsString(json1); - }), - ); - } @override bool get wantKeepAlive => true; } -class SyncCategoryButton extends StatefulWidget { - @override - _SyncCategoryButtonState createState() => _SyncCategoryButtonState(); -} -enum AsyncType { loading, error, none, success } -class _SyncCategoryButtonState extends State { - AsyncType state = AsyncType.none; - @override - Widget build(BuildContext context) { - Widget result; - switch (state) { - case AsyncType.loading: - result = _buildLoading(); - break; - case AsyncType.error: - result = _buildError(); - break; - case AsyncType.none: - result = _buildDefault(); - break; - case AsyncType.success: - result = _buildSuccess(); - break; - } - return result; - } - - Widget _buildLoading() { - return const CupertinoActivityIndicator(); - } - - Widget _buildError() { - return const Icon( - TolyIcon.error, - size: 28, - color: Colors.green, - ); - } - - Widget _buildDefault() { - return FeedbackWidget( - child: const Icon( - TolyIcon.upload, - size: 28, - ), - onPressed: () async { - CategoryRepository rep = - BlocProvider.of(context).repository; - List loadCategories = await rep.loadCategoryData(); - String json = jsonEncode(loadCategories); - setState(() { - state = AsyncType.loading; - }); - ResultBean result = await CategoryApi.uploadCategoryData(json); - - if (result.status) { - setState(() { - state = AsyncType.success; - }); - _toDefault(); - } else { - setState(() { - state = AsyncType.error; - }); - } - }); - } - - Widget _buildSuccess() { - return const Icon( - TolyIcon.upload_success, - size: 25, - color: Colors.green, - ); - } - - void _toDefault() async { - await Future.delayed(const Duration(milliseconds: 800)); - setState(() { - state = AsyncType.none; - }); - } -} diff --git a/lib/views/pages/category/sync/async_button.dart b/lib/views/pages/category/sync/async_button.dart new file mode 100644 index 0000000..e220928 --- /dev/null +++ b/lib/views/pages/category/sync/async_button.dart @@ -0,0 +1,106 @@ +import 'dart:convert'; + +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:flutter_unit/app/api/category_api.dart'; +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/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/itf/category_repository.dart'; +import 'package:flutter_unit/views/components/permanent/feedback_widget.dart'; + +/// create by 张风捷特烈 on 2021/2/24 +/// contact me by email 1981462002@qq.com +/// 说明: 同步数据按钮,点击时请求服务器,获取备份数据。 + +class SyncCategoryButton extends StatefulWidget { + @override + _SyncCategoryButtonState createState() => _SyncCategoryButtonState(); +} + +enum AsyncType { loading, error, none, success } + +class _SyncCategoryButtonState extends State { + AsyncType state = AsyncType.none; + + @override + Widget build(BuildContext context) { + Widget result; + switch (state) { + case AsyncType.loading: + result = _buildLoading(); + break; + case AsyncType.error: + result = _buildError(); + break; + case AsyncType.none: + result = _buildDefault(); + break; + case AsyncType.success: + result = _buildSuccess(); + break; + } + return result; + } + + Widget _buildLoading() { + return const CupertinoActivityIndicator(); + } + + Widget _buildError() { + return const Icon( + TolyIcon.error, + size: 25, + color: Colors.red, + ); + } + + Widget _buildDefault() { + return FeedbackWidget( + child: const Icon( + TolyIcon.download, + size: 28, + ), + onPressed: () async { + setState(() { + state = AsyncType.loading; + }); + + ResultBean result = await CategoryApi.getCategoryData(); + + CategoryRepository repository = BlocProvider.of(context).repository; + + await repository.syncCategoryByData(result.data); + + BlocProvider.of(context).add(EventLoadCategory()); + + if (result.status) { + setState(() { + state = AsyncType.success; + }); + _toDefault(); + } else { + setState(() { + state = AsyncType.error; + }); + _toDefault(); + } + }); + } + + Widget _buildSuccess() => const Icon( + TolyIcon.upload_success, + size: 25, + color: Colors.green, + ); + + void _toDefault() async { + await Future.delayed(const Duration(milliseconds: 800)); + setState(() { + state = AsyncType.none; + }); + } +} diff --git a/lib/views/pages/category/sync/upload_button.dart b/lib/views/pages/category/sync/upload_button.dart new file mode 100644 index 0000000..3196015 --- /dev/null +++ b/lib/views/pages/category/sync/upload_button.dart @@ -0,0 +1,104 @@ +import 'dart:convert'; + +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:flutter_unit/app/api/category_api.dart'; +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/itf/category_repository.dart'; +import 'package:flutter_unit/views/components/permanent/feedback_widget.dart'; + +/// create by 张风捷特烈 on 2021/2/24 +/// contact me by email 1981462002@qq.com +/// 说明: + +class UploadCategoryButton extends StatefulWidget { + @override + _UploadCategoryButtonState createState() => _UploadCategoryButtonState(); +} + +enum AsyncType { loading, error, none, success } + +class _UploadCategoryButtonState extends State { + AsyncType state = AsyncType.none; + + @override + Widget build(BuildContext context) { + Widget result; + switch (state) { + case AsyncType.loading: + result = _buildLoading(); + break; + case AsyncType.error: + result = _buildError(); + break; + case AsyncType.none: + result = _buildDefault(); + break; + case AsyncType.success: + result = _buildSuccess(); + break; + } + return result; + } + + Widget _buildLoading() { + return const CupertinoActivityIndicator(); + } + + Widget _buildError() { + return const Icon( + TolyIcon.error, + size: 28, + color: Colors.green, + ); + } + + Widget _buildDefault() { + return FeedbackWidget( + child: const Icon( + TolyIcon.upload, + size: 28, + ), + onPressed: () async { + CategoryRepository rep = + BlocProvider.of(context).repository; + List loadCategories = await rep.loadCategoryData(); + String json = jsonEncode(loadCategories); + setState(() { + state = AsyncType.loading; + }); + + ResultBean result = await CategoryApi.uploadCategoryData(json); + + if (result.status) { + setState(() { + state = AsyncType.success; + }); + _toDefault(); + } else { + setState(() { + state = AsyncType.error; + }); + } + }); + } + + Widget _buildSuccess() { + return const Icon( + TolyIcon.upload_success, + size: 25, + color: Colors.green, + ); + } + + void _toDefault() async { + await Future.delayed(const Duration(milliseconds: 800)); + setState(() { + state = AsyncType.none; + }); + } +}