wurong
2 years ago
9 changed files with 240 additions and 145 deletions
@ -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<Medias> 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<Color>(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<String> 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<bool> 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<void> fileUpload( |
||||||
|
ApiService apiService, List<Medias> 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<UploadResult> 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<UploadResult> 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<UploadObserver> _uploadObserverList; |
||||||
|
|
||||||
|
List<UploadObserver> get uploadObserverList => _uploadObserverList; |
||||||
|
|
||||||
|
set uploadObserverList(List<UploadObserver> value) { |
||||||
|
_uploadObserverList = value; |
||||||
|
} |
||||||
|
|
||||||
|
UploadInstance._internal() { |
||||||
|
//单例初始化 |
||||||
|
_uploadObserverList = []; |
||||||
|
} |
||||||
|
|
||||||
|
static UploadInstance _getInstance() { |
||||||
|
if (_instance == null) { |
||||||
|
_instance = UploadInstance._internal(); |
||||||
|
} |
||||||
|
return _instance; |
||||||
|
} |
||||||
|
|
||||||
|
void notifyAllObservers(){ |
||||||
|
_uploadObserverList.forEach((element) { |
||||||
|
element.onUploadFinish(); |
||||||
|
}); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue