137 lines
3.8 KiB
Dart
137 lines
3.8 KiB
Dart
import 'dart:convert';
|
|
import 'dart:math' as math;
|
|
import 'package:birzha/models/categories/relatedProducts.dart';
|
|
import 'package:birzha/models/products/post.dart';
|
|
import 'package:birzha/models/settings/settingsModel.dart';
|
|
import 'package:birzha/models/user/user.dart';
|
|
import 'package:birzha/models/user/userManager.dart';
|
|
import 'package:birzha/services/helpers.dart';
|
|
import 'package:birzha/services/requests.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:birzha/core/orm/orm.dart';
|
|
|
|
const _characLabelKeys = [
|
|
'productNumber',
|
|
'productAmount',
|
|
'productMark',
|
|
'producer',
|
|
'producerCountry',
|
|
];
|
|
|
|
const _characValueKeys = ['id', 'quantity', 'mark', 'manufacturer', 'country'];
|
|
|
|
class ProductCharacteristics implements PostCharacteristics {
|
|
@override
|
|
final String translatedLabel;
|
|
|
|
@override
|
|
final String value;
|
|
|
|
ProductCharacteristics(this.translatedLabel, this.value);
|
|
}
|
|
|
|
class Product extends Post with RemoteDetailsMixin, CopyAbleMixin<Product>, TranslatableMixin<int> {
|
|
Product({required Map<String, dynamic> data}) : super(data) {
|
|
configureTranslationModel();
|
|
}
|
|
|
|
int get id => primaryKey;
|
|
|
|
@protected
|
|
String get quantity => jSON['quantity'] ?? '0';
|
|
|
|
String get quantityFormatted => quantity.toString();
|
|
|
|
@protected
|
|
DateTime? get expiryDate => DateTime.tryParse(jSON['ends_at'] ?? "");
|
|
|
|
@protected
|
|
double get price => double.tryParse('${jSON["price"]}') ?? 0;
|
|
|
|
String get expiryDateFormatted => safeValueDate(expiryDate);
|
|
|
|
String get priceFormatted => priceFormatter(price);
|
|
|
|
String get _defaultName => jSON['name'] ?? "";
|
|
|
|
String get _defaultDescription => jSON['description'] ?? "";
|
|
|
|
String get unit => jSON['unit']["name"] ?? "";
|
|
|
|
String get name {
|
|
return translationModel?.translationOf('name', _defaultName) ?? _defaultName;
|
|
}
|
|
|
|
String get description {
|
|
return translationModel?.translationOf('description', _defaultDescription) ?? _defaultDescription;
|
|
}
|
|
|
|
List<String> get images => [
|
|
for (var image in [...?jSON['images']]) image['path']
|
|
]..removeWhere((element) => element.isEmpty);
|
|
|
|
String get mainImage => images.isEmpty ? '' : images.first;
|
|
|
|
@override
|
|
Future<void> loadDetails(context) async {
|
|
var user = AppUserManager.of(context).dataSync;
|
|
var data = await http.get(baseUrl(path: kApiPath + '/products/' + id.toString()),
|
|
headers: {if (user.isRegistered) 'Authorization': 'Bearer ${user.token}', 'Content-Type': 'application/json'});
|
|
var result = jsonDecode(data.body);
|
|
var category = RelatedProductsCategory.fromProduct(this);
|
|
await category.getPosts(context, 1);
|
|
jSON = {...jSON, ...result, ...category.getProductsForDetails()};
|
|
}
|
|
|
|
List<Product> get relatedProducts => [
|
|
for (var productRaw in [...?jSON['relatedProducts']]) Product(data: {...productRaw})
|
|
];
|
|
|
|
@override
|
|
Product get copy => Product(data: {...jSON});
|
|
|
|
List<PostCharacteristics> get characteristics => [
|
|
...[
|
|
for (var i = 0; i < math.min(_characLabelKeys.length, _characValueKeys.length); i++)
|
|
ProductCharacteristics(_characLabelKeys[i].translation, jSON[_characValueKeys[i]]?.toString() ?? ""),
|
|
]..insert(1, ProductCharacteristics('startingPrice'.translation, priceFormatted))
|
|
];
|
|
|
|
Vendor? get vendor {
|
|
if (jSON['vendor'] == null) {
|
|
return null;
|
|
} else {
|
|
return Vendor(data: {...jSON['vendor']});
|
|
}
|
|
}
|
|
}
|
|
|
|
/* {
|
|
id: 4,
|
|
deleted_at: null,
|
|
created_at: 2021-06-24 06:01:38,
|
|
updated_at: 2021-10-19 16:42:48,
|
|
name: in.m,
|
|
code: in.m,
|
|
translations: [
|
|
{
|
|
locale: en,
|
|
model_id: 4,
|
|
attribute_data:
|
|
{
|
|
"name":"sq.m",
|
|
"code":"sq.m"
|
|
}
|
|
},
|
|
{
|
|
locale: ru,
|
|
model_id: 4,
|
|
attribute_data:
|
|
{
|
|
"name":"кв.м",
|
|
"code":"кв.м"
|
|
}
|
|
}
|
|
]
|
|
} */ |