birzha_mobile/lib/models/products/composableProduct.dart

127 lines
4.9 KiB
Dart

import 'dart:collection';
import 'dart:convert';
import 'package:birzha/models/attributes/attribute.dart';
import 'package:birzha/models/attributes/category.dart';
import 'package:birzha/models/attributes/market_type.dart';
import 'package:birzha/models/attributes/miscelaneous.dart';
import 'package:birzha/models/attributes/packaging.dart';
import 'package:birzha/models/products/composable.dart';
import 'package:birzha/models/products/my_product.dart';
import 'package:birzha/models/user/userManager.dart';
import 'package:birzha/services/requests.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:birzha/core/orm/orm.dart';
import 'package:http/http.dart' as http;
abstract class ImageModel extends Orm<String> {
ImageModel._({required Map<String, dynamic> data}) : super(data);
@override
String get primaryKeyField => 'path';
}
class LocalImageModel extends ImageModel {
LocalImageModel._fromPath(String path) : super._(data: {'path': path});
LocalImageModel._fromModel({required Map<String, dynamic> data}) : super._(data: {...data});
}
class RemoteImageModel extends ImageModel {
RemoteImageModel._fromPath(String path, int id) : super._(data: {'path': path, 'id': id});
RemoteImageModel._fromModel({required Map<String, dynamic> data}) : super._(data: {...data});
int? get id => jSON['id'];
}
class ComposableProduct extends Orm<int?> with ComposableMixin<int?>, ChangeNotifier {
ComposableProduct._() : super({});
static ComposableProduct? _singletone;
factory ComposableProduct() {
return _singletone ??= ComposableProduct._();
}
static Future<void> getAttributes(BuildContext context, MyProduct? product) async {
List<OptionedAttributeMixin> options = [];
if (ComposableProduct().attrbiutes.isEmpty) {
options = await _getAttributes(context);
} else {
options = await SynchronousFuture([...ComposableProduct().attrbiutes]);
}
ComposableProduct()._attributes = [...options];
if (product != null) {
await ComposableProduct()._changeAttributes(context, product.primaryKey);
}
}
List<OptionedAttributeMixin> _attributes = [];
List<OptionedAttributeMixin> get attrbiutes => UnmodifiableListView(_attributes);
Future<void> _changeAttributes(BuildContext context, int productId) async {
var token = AppUserManager.of(context).dataSync.token;
var editedData = await http.get(baseUrl(path: kApiPath + '/my-products/$productId'), headers: {'Authorization': 'Bearer $token'});
var decoded = jsonDecode(editedData.body);
jSON = {...decoded};
print(jSON['old_img']);
jSON['old_img'] = [
for (var item in ((decoded['old_img'] ?? []) as Iterable).where((element) => element['path'] is String && (element['path'] as String).isNotEmpty))
RemoteImageModel._fromPath(item?['path'] ?? "", item['id']).jSON
];
for (var attribute in _attributes) {
var id = jSON[attribute.key];
if (id != null) {
try {
var matchingOption = attribute.options.firstWhere((element) => element.primaryKey == id);
setModelField(attribute.key, matchingOption);
} catch (e) {}
}
}
saveProgress(decoded['id']);
}
void saveProgress(int? id) {
jSON['productForEditing'] = id;
notifyListeners();
}
Map<String, dynamic> sendData() {
return {...jSON};
}
void removeImage(BuildContext context, ImageModel image) {
if (image is LocalImageModel) {
(jSON['new_img'] as List).removeWhere((element) => LocalImageModel._fromModel(data: element).primaryKey == image.primaryKey);
} else if (image is RemoteImageModel) {
AppUserManager.of(context).deleteImage(context, this, image, () {
try {
(jSON['old_img'] as List).removeWhere((element) => RemoteImageModel._fromModel(data: element).primaryKey == image.primaryKey);
} catch (e) {}
});
}
notifyListeners();
}
void pickImages() async {
var result = await FilePicker.platform.pickFiles(type: FileType.custom, allowMultiple: true, withReadStream: true, allowedExtensions: ['jpg', 'png']);
if (result != null) {
jSON['new_img'] = [for (var image in result.files.where((element) => element.path != null)) LocalImageModel._fromPath(image.path!).jSON];
notifyListeners();
}
}
List<LocalImageModel> get localImages => [for (var item in jSON['new_img'] ?? []) LocalImageModel._fromModel(data: item)];
List<RemoteImageModel> get remoteImages => [for (var item in jSON['old_img'] ?? []) RemoteImageModel._fromPath(item['path'], item['id'])];
@override
String get primaryKeyField => 'productForEditing';
}
Future<List<OptionedAttributeMixin>> _getAttributes(BuildContext context) async {
return [await CategoryAttribute.getAttribute(), ...await MiscAttribute.getAttrbutes(), MarketTypeAttribute.getAttribute(), PackagingAttribute.getAttribute()];
}