service updates
This commit is contained in:
parent
edd1f23079
commit
4a54b00a3b
|
|
@ -1,24 +1,9 @@
|
||||||
import 'dart:io';
|
|
||||||
|
|
||||||
import 'package:flutter/foundation.dart';
|
|
||||||
import 'package:image_cropper/image_cropper.dart';
|
|
||||||
import 'package:image_picker/image_picker.dart';
|
import 'package:image_picker/image_picker.dart';
|
||||||
|
|
||||||
class MediaService {
|
class MediaService {
|
||||||
static Future<File?> pickImage({required ImageSource source}) async {
|
static Future<XFile?> pickImage({required ImageSource source}) async {
|
||||||
final imagePicker = ImagePicker();
|
final imagePicker = ImagePicker();
|
||||||
final XFile? pickedFile = await imagePicker.pickImage(source: source);
|
final XFile? pickedFile = await imagePicker.pickImage(source: source);
|
||||||
if (pickedFile == null) {
|
return pickedFile;
|
||||||
return null;
|
|
||||||
}
|
|
||||||
if (kIsWeb) {
|
|
||||||
return File(pickedFile.path);
|
|
||||||
}
|
|
||||||
final cropedFile = await ImageCropper.cropImage(
|
|
||||||
sourcePath: pickedFile.path,
|
|
||||||
aspectRatio: const CropAspectRatio(ratioX: 1, ratioY: 1),
|
|
||||||
compressQuality: 70,
|
|
||||||
);
|
|
||||||
return cropedFile;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,25 +1,26 @@
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'dart:developer';
|
import 'dart:developer';
|
||||||
import 'dart:io';
|
|
||||||
import 'package:http/http.dart' as http;
|
import 'package:http/http.dart' as http;
|
||||||
import 'package:http_parser/http_parser.dart' as parser;
|
import 'package:http_parser/http_parser.dart' as parser;
|
||||||
|
import 'package:image_picker/image_picker.dart';
|
||||||
|
|
||||||
class RequestService {
|
class RequestService {
|
||||||
static late String token;
|
static late String token;
|
||||||
|
|
||||||
Map<String, dynamic> get result => _body['result'] ?? const {};
|
Map<String, dynamic> get result => _body?['result'] ?? const {};
|
||||||
Map<String, dynamic> get errors => _body['errors'] ?? const {};
|
Map<String, dynamic> get errors => _body?['errors'] ?? const {};
|
||||||
|
|
||||||
String errorMessage =
|
String errorMessage =
|
||||||
'خطا! لطفا اتصال اینترنت خود را بررسی و مجددا تلاش نمایید.';
|
'خطا! لطفا اتصال اینترنت خود را بررسی و مجددا تلاش نمایید.';
|
||||||
|
|
||||||
dynamic _body;
|
Map? _body;
|
||||||
|
|
||||||
final Map<String, String> _headers = {
|
final Map<String, String> _headers = {
|
||||||
"accept": "*/*",
|
"accept": "*/*",
|
||||||
"Content-Type": "application/json; charset=UTF-8",
|
"Content-Type": "application/json; charset=UTF-8",
|
||||||
};
|
};
|
||||||
final String url;
|
final String url;
|
||||||
dynamic _requestBody;
|
Map? _requestBody;
|
||||||
|
|
||||||
bool isSuccess = false;
|
bool isSuccess = false;
|
||||||
|
|
||||||
|
|
@ -99,16 +100,17 @@ class RequestService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> multipart(File file) async {
|
Future<void> multipart(XFile file) async {
|
||||||
try {
|
try {
|
||||||
final request = http.MultipartRequest('PUT', Uri.parse(url));
|
final request = http.MultipartRequest('PUT', Uri.parse(url));
|
||||||
_headers.update('Content-Type', (_) => 'multipart/form-data');
|
_headers.update('Content-Type', (_) => 'multipart/form-data');
|
||||||
request.headers.addAll(_headers);
|
request.headers.addAll(_headers);
|
||||||
|
final length = await file.length();
|
||||||
request.files.add(
|
request.files.add(
|
||||||
http.MultipartFile(
|
http.MultipartFile(
|
||||||
'photo',
|
'photo',
|
||||||
file.readAsBytes().asStream(),
|
file.readAsBytes().asStream(),
|
||||||
file.lengthSync(),
|
length,
|
||||||
filename: 'profile-photo',
|
filename: 'profile-photo',
|
||||||
contentType: parser.MediaType('image', 'jpg'),
|
contentType: parser.MediaType('image', 'jpg'),
|
||||||
),
|
),
|
||||||
|
|
@ -116,7 +118,7 @@ class RequestService {
|
||||||
final streamedResponse = await request
|
final streamedResponse = await request
|
||||||
.send()
|
.send()
|
||||||
.timeout(
|
.timeout(
|
||||||
const Duration(seconds: 10),
|
const Duration(seconds: 30),
|
||||||
)
|
)
|
||||||
.catchError(
|
.catchError(
|
||||||
(e) => throw e,
|
(e) => throw e,
|
||||||
|
|
@ -154,7 +156,7 @@ class RequestService {
|
||||||
_body = json.decode(response.body);
|
_body = json.decode(response.body);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!isSuccess) errorMessage = _errorMessageGenerator(response);
|
errorMessage = _errorMessageGenerator(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool _handleError(http.Response? response) {
|
bool _handleError(http.Response? response) {
|
||||||
|
|
@ -189,7 +191,7 @@ class RequestService {
|
||||||
String _errorMessageGenerator(http.Response? response) {
|
String _errorMessageGenerator(http.Response? response) {
|
||||||
String? error;
|
String? error;
|
||||||
if (response != null) {
|
if (response != null) {
|
||||||
if (!response.body.contains('<!DOCTYPE html>')) {
|
if (_body != null) {
|
||||||
if (result.isNotEmpty) {
|
if (result.isNotEmpty) {
|
||||||
error = result['msg'];
|
error = result['msg'];
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,6 @@
|
||||||
|
import 'package:didvan/models/requests/news.dart';
|
||||||
|
import 'package:didvan/models/requests/radar.dart';
|
||||||
|
|
||||||
class RequestHelper {
|
class RequestHelper {
|
||||||
static const String baseUrl = 'https://api.didvan.app';
|
static const String baseUrl = 'https://api.didvan.app';
|
||||||
static const String _baseUserUrl = baseUrl + '/user';
|
static const String _baseUserUrl = baseUrl + '/user';
|
||||||
|
|
@ -11,62 +14,59 @@ class RequestHelper {
|
||||||
static const String updateUserProfile = _baseUserUrl + '/profile/photo';
|
static const String updateUserProfile = _baseUserUrl + '/profile/photo';
|
||||||
static const String checkUsername = _baseUserUrl + '/CheckUsername';
|
static const String checkUsername = _baseUserUrl + '/CheckUsername';
|
||||||
static const String updateProfile = _baseUserUrl + '/profile/edit';
|
static const String updateProfile = _baseUserUrl + '/profile/edit';
|
||||||
|
static String bookmarks(String? type) =>
|
||||||
|
_baseUserUrl + '/marked/${type ?? ''}';
|
||||||
|
|
||||||
static const String directTypes = baseUrl + '/direct/types';
|
static const String directTypes = baseUrl + '/direct/types';
|
||||||
static const String addComment = baseUrl + '/comment/add';
|
|
||||||
static String feedbackComment(int id) => baseUrl + '/comment/$id/feedback';
|
|
||||||
|
|
||||||
static String direct(int id) => _baseUserUrl + '/direct/$id';
|
static String direct(int id) => _baseUserUrl + '/direct/$id';
|
||||||
|
|
||||||
static String markRadar(int id) => _baseRadarUrl + '/$id/mark';
|
static String markRadar(int id) => _baseRadarUrl + '/$id/mark';
|
||||||
static String radarDetails(int id) => _baseRadarUrl + '/$id';
|
|
||||||
static String radarComments(int id) => _baseRadarUrl + '/$id/comments';
|
static String radarComments(int id) => _baseRadarUrl + '/$id/comments';
|
||||||
static String markNews(int id) => _baseNewsUrl + '/$id/mark';
|
static String addRadarComment(int id) => _baseRadarUrl + '/$id/comments/add';
|
||||||
static String newsDetails(int id) => _baseNewsUrl + '/$id';
|
static String feedbackRadarComment(int radarId, int id) =>
|
||||||
static String newsComments(int id) => _baseNewsUrl + '/$id/comments';
|
_baseRadarUrl + '/$radarId/comments/$id/feedback';
|
||||||
static String radarOverviews({
|
static String radarDetails(int id, RadarRequestArgs args) =>
|
||||||
required int page,
|
_baseRadarUrl +
|
||||||
List<int> categories = const [],
|
'/$id' +
|
||||||
String? startDate,
|
_urlConcatGenerator([
|
||||||
String? endDate,
|
MapEntry('page', args.page.toString()),
|
||||||
String? search,
|
MapEntry('start', args.startDate),
|
||||||
}) {
|
MapEntry('end', args.endDate),
|
||||||
String? cats;
|
MapEntry('search', args.search),
|
||||||
if (categories.isNotEmpty) {
|
MapEntry('categories', _urlListConcatGenerator(args.categories)),
|
||||||
cats = '';
|
]);
|
||||||
for (var i = 0; i < categories.length; i++) {
|
static String radarOverviews({required RadarRequestArgs args}) =>
|
||||||
cats = cats! + categories[i].toString();
|
_baseRadarUrl +
|
||||||
if (i != categories.length - 1) {
|
_urlConcatGenerator([
|
||||||
cats += ',';
|
MapEntry('page', args.page.toString()),
|
||||||
}
|
MapEntry('start', args.startDate),
|
||||||
}
|
MapEntry('end', args.endDate),
|
||||||
}
|
MapEntry('search', args.search),
|
||||||
return _baseRadarUrl +
|
MapEntry('categories', _urlListConcatGenerator(args.categories)),
|
||||||
_urlConcatGenerator([
|
]);
|
||||||
MapEntry('page', page.toString()),
|
|
||||||
MapEntry('start', startDate),
|
|
||||||
MapEntry('end', endDate),
|
|
||||||
MapEntry('search', search),
|
|
||||||
MapEntry('categories', cats),
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
static String newsOverviews({
|
static String markNews(int id) => _baseNewsUrl + '/$id/mark';
|
||||||
required int page,
|
static String newsComments(int id) => _baseNewsUrl + '/$id/comments';
|
||||||
String? startDate,
|
static String addNewsComment(int id) => _baseNewsUrl + '/$id/comments/add';
|
||||||
String? endDate,
|
static String feedbackNewsComment(int radarId, int id) =>
|
||||||
String? search,
|
_baseNewsUrl + '/$radarId/comments/$id/feedback';
|
||||||
}) {
|
static String newsDetails(int id, NewsRequestArgs args) =>
|
||||||
String? cats;
|
_baseNewsUrl +
|
||||||
return _baseNewsUrl +
|
'/$id' +
|
||||||
_urlConcatGenerator([
|
_urlConcatGenerator([
|
||||||
MapEntry('page', page.toString()),
|
MapEntry('page', args.page.toString()),
|
||||||
MapEntry('start', startDate),
|
MapEntry('start', args.startDate),
|
||||||
MapEntry('end', endDate),
|
MapEntry('end', args.endDate),
|
||||||
MapEntry('search', search),
|
MapEntry('search', args.search),
|
||||||
MapEntry('categories', cats),
|
]);
|
||||||
]);
|
static String newsOverviews({required NewsRequestArgs args}) =>
|
||||||
}
|
_baseNewsUrl +
|
||||||
|
_urlConcatGenerator([
|
||||||
|
MapEntry('page', args.page.toString()),
|
||||||
|
MapEntry('start', args.startDate),
|
||||||
|
MapEntry('end', args.endDate),
|
||||||
|
MapEntry('search', args.search),
|
||||||
|
]);
|
||||||
|
|
||||||
static String _urlConcatGenerator(List<MapEntry<String, String?>> additions) {
|
static String _urlConcatGenerator(List<MapEntry<String, String?>> additions) {
|
||||||
String result = '';
|
String result = '';
|
||||||
|
|
@ -82,4 +82,19 @@ class RequestHelper {
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static String? _urlListConcatGenerator(List? input) {
|
||||||
|
String? result;
|
||||||
|
if (input == null) return null;
|
||||||
|
if (input.isNotEmpty) {
|
||||||
|
result = '';
|
||||||
|
for (var i = 0; i < input.length; i++) {
|
||||||
|
result = result! + input[i].toString();
|
||||||
|
if (i != input.length - 1) {
|
||||||
|
result += ',';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ class DateTimeUtils {
|
||||||
static String normalizeTimeDuration(Duration input) {
|
static String normalizeTimeDuration(Duration input) {
|
||||||
String minute;
|
String minute;
|
||||||
String second;
|
String second;
|
||||||
|
|
||||||
if (input.inMinutes < 10) {
|
if (input.inMinutes < 10) {
|
||||||
minute = '0${input.inMinutes}';
|
minute = '0${input.inMinutes}';
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -25,22 +26,58 @@ class DateTimeUtils {
|
||||||
return '$minute:$second';
|
return '$minute:$second';
|
||||||
}
|
}
|
||||||
|
|
||||||
static Future<String?> showDatePicker(
|
static Future<String?> showDatePicker({
|
||||||
{String? initialDate, String? startDate, String? endDate}) async {
|
String? initialDate,
|
||||||
|
String? startDate,
|
||||||
|
String? endDate,
|
||||||
|
}) async {
|
||||||
final initDate = initialDate == null ? null : DateTime.parse(initialDate);
|
final initDate = initialDate == null ? null : DateTime.parse(initialDate);
|
||||||
final initialJalali = Jalali.fromDateTime(initDate ?? DateTime.now());
|
final initialJalali = Jalali.fromDateTime(initDate ?? DateTime.now());
|
||||||
|
|
||||||
final firstDate = Jalali.fromDateTime(
|
final firstDate = Jalali.fromDateTime(
|
||||||
startDate == null ? DateTime(2021) : DateTime.parse(startDate),
|
startDate == null ? DateTime(2021) : DateTime.parse(startDate),
|
||||||
);
|
);
|
||||||
|
|
||||||
final lastDate = Jalali.fromDateTime(
|
final lastDate = Jalali.fromDateTime(
|
||||||
endDate == null ? DateTime.now() : DateTime.parse(endDate),
|
endDate == null ? DateTime.now() : DateTime.parse(endDate),
|
||||||
);
|
);
|
||||||
|
|
||||||
final Jalali? result = await showPersianDatePicker(
|
final Jalali? result = await showPersianDatePicker(
|
||||||
context: DesignConfig.context,
|
context: DesignConfig.context,
|
||||||
initialDate: initialJalali,
|
initialDate: initialJalali,
|
||||||
firstDate: firstDate,
|
firstDate: firstDate,
|
||||||
lastDate: lastDate,
|
lastDate: lastDate,
|
||||||
);
|
);
|
||||||
|
|
||||||
return result?.toDateTime().toString();
|
return result?.toDateTime().toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static String momentGenerator(String input) {
|
||||||
|
final date = DateTime.parse(input);
|
||||||
|
final int seconds = (DateTime.now().difference(date).inSeconds).floor();
|
||||||
|
|
||||||
|
double interval = seconds / 31536000;
|
||||||
|
|
||||||
|
if (interval > 1) {
|
||||||
|
return interval.floor().toString() + " سال پیش";
|
||||||
|
}
|
||||||
|
interval = seconds / 2592000;
|
||||||
|
if (interval > 1) {
|
||||||
|
return interval.floor().toString() + " ماه پیش";
|
||||||
|
}
|
||||||
|
interval = seconds / 86400;
|
||||||
|
if (interval > 1) {
|
||||||
|
return interval.floor().toString() + " روز پیش";
|
||||||
|
}
|
||||||
|
interval = seconds / 3600;
|
||||||
|
if (interval > 1) {
|
||||||
|
return interval.floor().toString() + " ساعت پیش";
|
||||||
|
}
|
||||||
|
interval = seconds / 60;
|
||||||
|
if (interval > 1) {
|
||||||
|
return interval.floor().toString() + " دقیقه پیش";
|
||||||
|
}
|
||||||
|
return 'هم اکنون';
|
||||||
|
// return seconds.floor().toString() + " ثانیه پیش";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue