|
|
|
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:image_pickers/image_pickers.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';
|
|
|
|
import '../view_widget/custom_image.dart';
|
|
|
|
import 'font_weight.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: Stack(
|
|
|
|
alignment: Alignment.center,
|
|
|
|
children: [
|
|
|
|
Container(
|
|
|
|
width: 50.h,
|
|
|
|
height: 70.h,
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
color: Colors.grey, borderRadius: BorderRadius.circular(2)),
|
|
|
|
child: dynamicType == 0
|
|
|
|
? Expanded(child: Column(
|
|
|
|
children: [
|
|
|
|
Text(
|
|
|
|
dynamicText,
|
|
|
|
maxLines: 5,
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
style: TextStyle(
|
|
|
|
fontWeight: MyFontWeight.semi_bold,
|
|
|
|
fontSize: dynamicText.length > 10 ? (dynamicText.length > 20?8.sp:10.sp):15.sp,
|
|
|
|
color: Color(0xFFCDCCCC),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Expanded(child:
|
|
|
|
Container(
|
|
|
|
height: 20.h,
|
|
|
|
))
|
|
|
|
]))
|
|
|
|
: ClipRRect(
|
|
|
|
borderRadius: BorderRadius.circular(2),
|
|
|
|
child: Image.file(
|
|
|
|
File(mediaPaths[0].galleryMode == GalleryMode.video
|
|
|
|
? mediaPaths[0].thumbPath
|
|
|
|
: mediaPaths[0].path),
|
|
|
|
fit: BoxFit.cover,
|
|
|
|
width: double.infinity,
|
|
|
|
height: double.infinity,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
CircularProgressIndicator(
|
|
|
|
strokeWidth: 3.0,
|
|
|
|
backgroundColor: Colors.white,
|
|
|
|
// 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();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|