diff --git a/lib/community/community_page.dart b/lib/community/community_page.dart index 61a91406..52ae871f 100644 --- a/lib/community/community_page.dart +++ b/lib/community/community_page.dart @@ -7,6 +7,7 @@ import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:huixiang/view_widget/my_tab.dart'; import '../generated/l10n.dart'; +import '../utils/upload_async.dart'; import 'community_course.dart'; import 'headlines/article_page.dart'; @@ -18,7 +19,7 @@ class CommunityPage extends StatefulWidget { } class _CommunityPage extends State - with SingleTickerProviderStateMixin,AutomaticKeepAliveClientMixin { + with SingleTickerProviderStateMixin,AutomaticKeepAliveClientMixin,UploadObserver { TabController tabcontroller; CommunityChildPage guanzhu,tuijian ; @@ -39,13 +40,20 @@ class _CommunityPage extends State @override void initState() { super.initState(); - + UploadInstance.instance.uploadObserverList.add(this); if (tabcontroller == null) tabcontroller = TabController(length: lables.length, vsync: this, initialIndex: 1); } + + @override + void dispose() { + super.dispose(); + UploadInstance.instance.uploadObserverList.remove(this); + } + void onChildScroll(){ if(scrollCount == 0){ setState(() { @@ -63,8 +71,8 @@ class _CommunityPage extends State }); } - _toRelease() async { - await Navigator.of(context).pushNamed('/router/release_dynamic'); + @override + void onUploadFinish(){ if(tabcontroller.index == 0){ CommunityChildPageState page = guanzhuKey.currentState; page.onRefresh(); @@ -74,6 +82,10 @@ class _CommunityPage extends State } } + _toRelease() async { + Navigator.of(context).pushNamed('/router/release_dynamic'); + } + @override Widget build(BuildContext context) { return Scaffold( diff --git a/lib/community/release_dynamic.dart b/lib/community/release_dynamic.dart index 5b279e4f..5268345a 100644 --- a/lib/community/release_dynamic.dart +++ b/lib/community/release_dynamic.dart @@ -1,21 +1,19 @@ +import 'dart:convert'; import 'dart:io'; import 'package:dio/dio.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; -import 'package:flutter_easyloading/flutter_easyloading.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:flutter_svg/flutter_svg.dart'; import 'package:huixiang/generated/l10n.dart'; import 'package:huixiang/retrofit/data/address.dart'; -import 'package:huixiang/retrofit/data/base_data.dart'; -import 'package:huixiang/retrofit/data/upload_result.dart'; import 'package:huixiang/retrofit/retrofit_api.dart'; import 'package:huixiang/utils/font_weight.dart'; import 'package:huixiang/view_widget/my_appbar.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:image_pickers/image_pickers.dart'; import 'package:shared_preferences/shared_preferences.dart'; -import 'package:thumbnails/thumbnails.dart'; +import '../utils/upload_async.dart'; class ReleaseDynamic extends StatefulWidget { @override @@ -170,99 +168,9 @@ class _ReleaseDynamic extends State { SmartDialog.showToast("请输入您此刻的想法!"); return; } - EasyLoading.show(status: "上传中"); - fileUpload().then((value) async { - String subjectType = "text"; - if (dynamicType == 0) { - subjectType = "text"; - } else if (dynamicType == 1) { - subjectType = "image"; - } else if (dynamicType == 2) { - subjectType = "video"; - } - - List remoteImageUrls = []; - String remoteVideoUrl = ""; - String remoteVideoCoverImg = ""; - if (mediaPaths.length > 0) { - if (dynamicType == 1) { - remoteImageUrls = mediaPaths.map((e) => e.remotePath).toList(); - } else if (dynamicType == 2) { - remoteVideoUrl = mediaPaths[0].remotePath; - remoteVideoCoverImg = mediaPaths[0].thumbPath; - } - } - - BaseData baseData = await apiService.trend({ - "images": remoteImageUrls, - "subject": dynamicText, - "subjectType": subjectType, - "video": remoteVideoUrl, - "coverImg": remoteVideoCoverImg, - "latitude": "", - "location": addressController.text == "" ? "" : addressController.text, - "longitude": "", - }).catchError((onError) { - EasyLoading.dismiss(); - }); - if (baseData != null && baseData.isSuccess) { - SmartDialog.showToast("发布成功!"); - Future.delayed(Duration(seconds: 1), () { - Navigator.of(context).pop(true); - }); - } else { - SmartDialog.showToast(baseData.msg, alignment: Alignment.center); - } - EasyLoading.dismiss(); - }); - } - - ///文件上传 - Future fileUpload() async { - if (mediaPaths != null && mediaPaths.length > 0) { - await Future.forEach(mediaPaths, (element) async { - if ((element.remotePath == null || element.remotePath == "") && - (element != null && - element.path != null && - element.path != "" && - await File(element.path).exists())) { - File file = File(element.path); - if (dynamicType == 2) { - String thumbnail; - if (element.thumbPath != null && - element.thumbPath != "" && - await File(element.thumbPath).exists()) { - thumbnail = element.thumbPath; - } else { - thumbnail = await Thumbnails.getThumbnail( - videoFile: file.path, - imageType: ThumbFormat.JPEG, - quality: 10, - ); - } - if (thumbnail != null && - thumbnail != "" && - await File(thumbnail).exists()) { - BaseData baseData = await apiService.upload( - File(thumbnail), 123123123, dynamicType == 2); - if (baseData != null && baseData.isSuccess) { - UploadResult uploadResult = baseData.data; - mediaPaths[mediaPaths.indexOf(element)].thumbPath = - uploadResult.url; - } - } - } - BaseData baseData = - await apiService.upload(file, 123123123, dynamicType == 2); - if (baseData != null && baseData.isSuccess) { - UploadResult uploadResult = baseData.data; - mediaPaths[mediaPaths.indexOf(element)].remotePath = - uploadResult.url; - } - } - }); - } + UploadAsync.upload(dynamicType, mediaPaths, addressController.text, dynamicText); + Navigator.of(context).pop(); } Widget imageItem(Medias media) { diff --git a/lib/utils/upload_async.dart b/lib/utils/upload_async.dart new file mode 100644 index 00000000..b4fb5339 --- /dev/null +++ b/lib/utils/upload_async.dart @@ -0,0 +1,170 @@ +import 'dart:io'; + +import 'package:dio/dio.dart'; +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_screenutil/flutter_screenutil.dart'; +import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; +import 'package:shared_preferences/shared_preferences.dart'; +import 'package:thumbnails/thumbnails.dart'; + +import '../community/release_dynamic.dart'; +import '../retrofit/data/base_data.dart'; +import '../retrofit/data/upload_result.dart'; +import '../retrofit/retrofit_api.dart'; + +class UploadAsync { + static void upload(int dynamicType, List mediaPaths, + String addressText, String dynamicText) async { + SmartDialog.show( + widget: Container( + alignment: Alignment.centerRight, + margin: EdgeInsets.only( + right: 10.w, + ), + child: CircularProgressIndicator( + strokeWidth: 4.0, + backgroundColor: Colors.green, + // value: 0.4, + valueColor: new AlwaysStoppedAnimation(Colors.grey), + ), + ), + maskWidgetTemp: SizedBox(), + ); + var sp = await SharedPreferences.getInstance(); + ApiService apiService = ApiService( + Dio(), + token: sp.getString("token"), + showLoading: false, + ); + + fileUpload(apiService, mediaPaths, dynamicType).then((value) async { + String subjectType = "text"; + if (dynamicType == 0) { + subjectType = "text"; + } else if (dynamicType == 1) { + subjectType = "image"; + } else if (dynamicType == 2) { + subjectType = "video"; + } + + List remoteImageUrls = []; + String remoteVideoUrl = ""; + String remoteVideoCoverImg = ""; + if (mediaPaths.length > 0) { + if (dynamicType == 1) { + remoteImageUrls = mediaPaths.map((e) => e.remotePath).toList(); + } else if (dynamicType == 2) { + remoteVideoUrl = mediaPaths[0].remotePath; + remoteVideoCoverImg = mediaPaths[0].thumbPath; + } + } + + BaseData baseData = await apiService.trend({ + "images": remoteImageUrls, + "subject": dynamicText, + "subjectType": subjectType, + "video": remoteVideoUrl, + "coverImg": remoteVideoCoverImg, + "latitude": "", + "location": addressText, + "longitude": "", + }).catchError((onError) { + SmartDialog.dismiss(); + }); + if (baseData != null && baseData.isSuccess) { + SmartDialog.showToast("发布成功!"); + UploadInstance.instance.notifyAllObservers(); + } else { + SmartDialog.showToast(baseData.msg, alignment: Alignment.center); + } + SmartDialog.dismiss(); + }); + } + + ///文件上传 + static Future fileUpload( + ApiService apiService, List mediaPaths, int dynamicType) async { + if (mediaPaths != null && mediaPaths.length > 0) { + await Future.forEach(mediaPaths, (element) async { + if ((element.remotePath == null || element.remotePath == "") && + (element != null && + element.path != null && + element.path != "" && + await File(element.path).exists())) { + File file = File(element.path); + if (dynamicType == 2) { + String thumbnail; + if (element.thumbPath != null && + element.thumbPath != "" && + await File(element.thumbPath).exists()) { + thumbnail = element.thumbPath; + } else { + thumbnail = await Thumbnails.getThumbnail( + videoFile: file.path, + imageType: ThumbFormat.JPEG, + quality: 10, + ); + } + if (thumbnail != null && + thumbnail != "" && + await File(thumbnail).exists()) { + BaseData baseData = await apiService.upload( + File(thumbnail), 123123123, dynamicType == 2); + if (baseData != null && baseData.isSuccess) { + UploadResult uploadResult = baseData.data; + mediaPaths[mediaPaths.indexOf(element)].thumbPath = + uploadResult.url; + } + } + } + BaseData baseData = + await apiService.upload(file, 123123123, dynamicType == 2); + if (baseData != null && baseData.isSuccess) { + UploadResult uploadResult = baseData.data; + mediaPaths[mediaPaths.indexOf(element)].remotePath = + uploadResult.url; + } + } + }); + } + } +} + +abstract class UploadObserver { + void onUploadFinish(); +} + +class UploadInstance { + factory UploadInstance() => _getInstance(); + + static UploadInstance get instance => _getInstance(); + + static UploadInstance _instance; + + List _uploadObserverList; + + List get uploadObserverList => _uploadObserverList; + + set uploadObserverList(List value) { + _uploadObserverList = value; + } + + UploadInstance._internal() { + //单例初始化 + _uploadObserverList = []; + } + + static UploadInstance _getInstance() { + if (_instance == null) { + _instance = UploadInstance._internal(); + } + return _instance; + } + + void notifyAllObservers(){ + _uploadObserverList.forEach((element) { + element.onUploadFinish(); + }); + } +} diff --git a/lib/vip/vip_page.dart b/lib/vip/vip_page.dart index 90a25727..2b4939b6 100644 --- a/lib/vip/vip_page.dart +++ b/lib/vip/vip_page.dart @@ -59,12 +59,14 @@ class _VipPageState extends State with AutomaticKeepAliveClientMixin { await apiService.vipCardIndex().catchError((onError) { SmartDialog.showToast(AppUtils.dioErrorTypeToString(onError.type), alignment: Alignment.center); - _refreshController.refreshFailed();}); + _refreshController.loadFailed(); + _refreshController.refreshFailed(); + }); if (baseData != null && baseData.isSuccess) { vipHome = baseData.data; _refreshController.refreshCompleted(); + _refreshController.loadComplete(); }else{ - _refreshController.refreshFailed(); SmartDialog.showToast(baseData.msg ?? "", alignment: Alignment.center); } }finally{ diff --git a/lib/vip/vip_view/vip_top.dart b/lib/vip/vip_view/vip_top.dart index 54f11b73..41982fd1 100644 --- a/lib/vip/vip_view/vip_top.dart +++ b/lib/vip/vip_view/vip_top.dart @@ -51,7 +51,7 @@ class _VipTop extends State { }); } - recharge() async { + rechargeVIP() async { if (apiService == null) { SharedPreferences value = await SharedPreferences.getInstance(); apiService = ApiService(Dio(), @@ -895,7 +895,7 @@ class _VipTop extends State { GestureDetector( onTap: () { Navigator.of(context).pop(); - recharge(); + rechargeVIP(); }, child: Container( height: double.infinity, diff --git a/pubspec.lock b/pubspec.lock index 5589cb7d..f930e720 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -269,7 +269,7 @@ packages: name: flutter_smart_dialog url: "https://pub.flutter-io.cn" source: hosted - version: "2.1.6" + version: "3.5.0" flutter_spinkit: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index fd79f61e..f6b9acd2 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -57,7 +57,7 @@ dependencies: event_bus: ^2.0.0 intl: ^0.17.0 shared_preferences: ^2.0.6 - flutter_smart_dialog: ^2.1.6 + flutter_smart_dialog: ^3.0.6 barcode_widget: ^2.0.1 qr_flutter: ^4.0.0