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';
|
||||
|
||||
class MediaService {
|
||||
static Future<File?> pickImage({required ImageSource source}) async {
|
||||
static Future<XFile?> pickImage({required ImageSource source}) async {
|
||||
final imagePicker = ImagePicker();
|
||||
final XFile? pickedFile = await imagePicker.pickImage(source: source);
|
||||
if (pickedFile == null) {
|
||||
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;
|
||||
return pickedFile;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,25 +1,26 @@
|
|||
import 'dart:convert';
|
||||
import 'dart:developer';
|
||||
import 'dart:io';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:http_parser/http_parser.dart' as parser;
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
|
||||
class RequestService {
|
||||
static late String token;
|
||||
|
||||
Map<String, dynamic> get result => _body['result'] ?? const {};
|
||||
Map<String, dynamic> get errors => _body['errors'] ?? const {};
|
||||
Map<String, dynamic> get result => _body?['result'] ?? const {};
|
||||
Map<String, dynamic> get errors => _body?['errors'] ?? const {};
|
||||
|
||||
String errorMessage =
|
||||
'خطا! لطفا اتصال اینترنت خود را بررسی و مجددا تلاش نمایید.';
|
||||
|
||||
dynamic _body;
|
||||
Map? _body;
|
||||
|
||||
final Map<String, String> _headers = {
|
||||
"accept": "*/*",
|
||||
"Content-Type": "application/json; charset=UTF-8",
|
||||
};
|
||||
final String url;
|
||||
dynamic _requestBody;
|
||||
Map? _requestBody;
|
||||
|
||||
bool isSuccess = false;
|
||||
|
||||
|
|
@ -99,16 +100,17 @@ class RequestService {
|
|||
}
|
||||
}
|
||||
|
||||
Future<void> multipart(File file) async {
|
||||
Future<void> multipart(XFile file) async {
|
||||
try {
|
||||
final request = http.MultipartRequest('PUT', Uri.parse(url));
|
||||
_headers.update('Content-Type', (_) => 'multipart/form-data');
|
||||
request.headers.addAll(_headers);
|
||||
final length = await file.length();
|
||||
request.files.add(
|
||||
http.MultipartFile(
|
||||
'photo',
|
||||
file.readAsBytes().asStream(),
|
||||
file.lengthSync(),
|
||||
length,
|
||||
filename: 'profile-photo',
|
||||
contentType: parser.MediaType('image', 'jpg'),
|
||||
),
|
||||
|
|
@ -116,7 +118,7 @@ class RequestService {
|
|||
final streamedResponse = await request
|
||||
.send()
|
||||
.timeout(
|
||||
const Duration(seconds: 10),
|
||||
const Duration(seconds: 30),
|
||||
)
|
||||
.catchError(
|
||||
(e) => throw e,
|
||||
|
|
@ -154,7 +156,7 @@ class RequestService {
|
|||
_body = json.decode(response.body);
|
||||
}
|
||||
}
|
||||
if (!isSuccess) errorMessage = _errorMessageGenerator(response);
|
||||
errorMessage = _errorMessageGenerator(response);
|
||||
}
|
||||
|
||||
bool _handleError(http.Response? response) {
|
||||
|
|
@ -189,7 +191,7 @@ class RequestService {
|
|||
String _errorMessageGenerator(http.Response? response) {
|
||||
String? error;
|
||||
if (response != null) {
|
||||
if (!response.body.contains('<!DOCTYPE html>')) {
|
||||
if (_body != null) {
|
||||
if (result.isNotEmpty) {
|
||||
error = result['msg'];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,6 @@
|
|||
import 'package:didvan/models/requests/news.dart';
|
||||
import 'package:didvan/models/requests/radar.dart';
|
||||
|
||||
class RequestHelper {
|
||||
static const String baseUrl = 'https://api.didvan.app';
|
||||
static const String _baseUserUrl = baseUrl + '/user';
|
||||
|
|
@ -11,62 +14,59 @@ class RequestHelper {
|
|||
static const String updateUserProfile = _baseUserUrl + '/profile/photo';
|
||||
static const String checkUsername = _baseUserUrl + '/CheckUsername';
|
||||
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 addComment = baseUrl + '/comment/add';
|
||||
static String feedbackComment(int id) => baseUrl + '/comment/$id/feedback';
|
||||
|
||||
static String direct(int id) => _baseUserUrl + '/direct/$id';
|
||||
|
||||
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 markNews(int id) => _baseNewsUrl + '/$id/mark';
|
||||
static String newsDetails(int id) => _baseNewsUrl + '/$id';
|
||||
static String newsComments(int id) => _baseNewsUrl + '/$id/comments';
|
||||
static String radarOverviews({
|
||||
required int page,
|
||||
List<int> categories = const [],
|
||||
String? startDate,
|
||||
String? endDate,
|
||||
String? search,
|
||||
}) {
|
||||
String? cats;
|
||||
if (categories.isNotEmpty) {
|
||||
cats = '';
|
||||
for (var i = 0; i < categories.length; i++) {
|
||||
cats = cats! + categories[i].toString();
|
||||
if (i != categories.length - 1) {
|
||||
cats += ',';
|
||||
}
|
||||
}
|
||||
}
|
||||
return _baseRadarUrl +
|
||||
_urlConcatGenerator([
|
||||
MapEntry('page', page.toString()),
|
||||
MapEntry('start', startDate),
|
||||
MapEntry('end', endDate),
|
||||
MapEntry('search', search),
|
||||
MapEntry('categories', cats),
|
||||
]);
|
||||
}
|
||||
static String addRadarComment(int id) => _baseRadarUrl + '/$id/comments/add';
|
||||
static String feedbackRadarComment(int radarId, int id) =>
|
||||
_baseRadarUrl + '/$radarId/comments/$id/feedback';
|
||||
static String radarDetails(int id, RadarRequestArgs args) =>
|
||||
_baseRadarUrl +
|
||||
'/$id' +
|
||||
_urlConcatGenerator([
|
||||
MapEntry('page', args.page.toString()),
|
||||
MapEntry('start', args.startDate),
|
||||
MapEntry('end', args.endDate),
|
||||
MapEntry('search', args.search),
|
||||
MapEntry('categories', _urlListConcatGenerator(args.categories)),
|
||||
]);
|
||||
static String radarOverviews({required RadarRequestArgs args}) =>
|
||||
_baseRadarUrl +
|
||||
_urlConcatGenerator([
|
||||
MapEntry('page', args.page.toString()),
|
||||
MapEntry('start', args.startDate),
|
||||
MapEntry('end', args.endDate),
|
||||
MapEntry('search', args.search),
|
||||
MapEntry('categories', _urlListConcatGenerator(args.categories)),
|
||||
]);
|
||||
|
||||
static String newsOverviews({
|
||||
required int page,
|
||||
String? startDate,
|
||||
String? endDate,
|
||||
String? search,
|
||||
}) {
|
||||
String? cats;
|
||||
return _baseNewsUrl +
|
||||
_urlConcatGenerator([
|
||||
MapEntry('page', page.toString()),
|
||||
MapEntry('start', startDate),
|
||||
MapEntry('end', endDate),
|
||||
MapEntry('search', search),
|
||||
MapEntry('categories', cats),
|
||||
]);
|
||||
}
|
||||
static String markNews(int id) => _baseNewsUrl + '/$id/mark';
|
||||
static String newsComments(int id) => _baseNewsUrl + '/$id/comments';
|
||||
static String addNewsComment(int id) => _baseNewsUrl + '/$id/comments/add';
|
||||
static String feedbackNewsComment(int radarId, int id) =>
|
||||
_baseNewsUrl + '/$radarId/comments/$id/feedback';
|
||||
static String newsDetails(int id, NewsRequestArgs args) =>
|
||||
_baseNewsUrl +
|
||||
'/$id' +
|
||||
_urlConcatGenerator([
|
||||
MapEntry('page', args.page.toString()),
|
||||
MapEntry('start', args.startDate),
|
||||
MapEntry('end', args.endDate),
|
||||
MapEntry('search', args.search),
|
||||
]);
|
||||
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) {
|
||||
String result = '';
|
||||
|
|
@ -82,4 +82,19 @@ class RequestHelper {
|
|||
}
|
||||
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) {
|
||||
String minute;
|
||||
String second;
|
||||
|
||||
if (input.inMinutes < 10) {
|
||||
minute = '0${input.inMinutes}';
|
||||
} else {
|
||||
|
|
@ -25,22 +26,58 @@ class DateTimeUtils {
|
|||
return '$minute:$second';
|
||||
}
|
||||
|
||||
static Future<String?> showDatePicker(
|
||||
{String? initialDate, String? startDate, String? endDate}) async {
|
||||
static Future<String?> showDatePicker({
|
||||
String? initialDate,
|
||||
String? startDate,
|
||||
String? endDate,
|
||||
}) async {
|
||||
final initDate = initialDate == null ? null : DateTime.parse(initialDate);
|
||||
final initialJalali = Jalali.fromDateTime(initDate ?? DateTime.now());
|
||||
|
||||
final firstDate = Jalali.fromDateTime(
|
||||
startDate == null ? DateTime(2021) : DateTime.parse(startDate),
|
||||
);
|
||||
|
||||
final lastDate = Jalali.fromDateTime(
|
||||
endDate == null ? DateTime.now() : DateTime.parse(endDate),
|
||||
);
|
||||
|
||||
final Jalali? result = await showPersianDatePicker(
|
||||
context: DesignConfig.context,
|
||||
initialDate: initialJalali,
|
||||
firstDate: firstDate,
|
||||
lastDate: lastDate,
|
||||
);
|
||||
|
||||
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