Browse Source

post nursery

master
daivph 5 years ago
parent
commit
20c640b27a
6 changed files with 70 additions and 16 deletions
  1. +6
    -3
      lib/data/api/app_exception.dart
  2. +8
    -8
      lib/data/api/dio_provider.dart
  3. +23
    -0
      lib/data/repository/repository.dart
  4. +29
    -3
      lib/presentation/screens/actions/nursery/sc_edit_action_nursery.dart
  5. +2
    -2
      pubspec.lock
  6. +2
    -0
      pubspec.yaml

+ 6
- 3
lib/data/api/app_exception.dart View File

@@ -1,11 +1,14 @@
import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:farm_tpf/utils/const_string.dart';

class AppException {
static String handleError(dynamic error) {
String errorDescription = "";
if (error is DioError) {
DioError dioError = error;

try {
DioError dioError = error as DioError;
switch (dioError.type) {
case DioErrorType.CANCEL:
errorDescription = exception_dio_cancle;
@@ -39,7 +42,7 @@ class AppException {
errorDescription = exception_dio_send_timeout;
break;
}
} else {
} catch (_) {
errorDescription = exception_common;
}
return errorDescription;

+ 8
- 8
lib/data/api/dio_provider.dart View File

@@ -21,24 +21,24 @@ class HttpLogInterceptor extends InterceptorsWrapper {
var token = await pref.getString(DATA_CONST.TOKEN_KEY);
options.headers["Authorization"] = "Bearer $token";
options.receiveTimeout = 20000;
// log("onRequest: ${options.uri}\n"
// "data=${options.data}\n"
// "method=${options.method}\n"
// "headers=${options.headers}\n"
// "queryParameters=${options.queryParameters}");
log("onRequest: ${options.uri}\n"
"data=${options.data}\n"
"method=${options.method}\n"
"headers=${options.headers}\n"
"queryParameters=${options.queryParameters}");
return options;
}

@override
Future onResponse(Response response) {
// log("onResponse: $response");
log("onResponse: $response");
return super.onResponse(response);
}

@override
Future onError(DioError err) {
// log("onError: $err\n"
// "Response: ${err.response}");
log("onError: $err\n"
"Response: ${err.response}");
return super.onError(err);
}
}

+ 23
- 0
lib/data/repository/repository.dart View File

@@ -1,6 +1,10 @@
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:farm_tpf/custom_model/CropPlot.dart';
import 'package:farm_tpf/custom_model/user.dart';
import 'package:farm_tpf/custom_model/user_request.dart';
import 'package:farm_tpf/data/api/app_exception.dart';
import 'package:farm_tpf/data/api/dio_provider.dart';
import 'package:farm_tpf/data/api/rest_client.dart';
import 'package:farm_tpf/models/PagedResult.dart';
@@ -59,4 +63,23 @@ class Repository {
}
return instanceClass;
}

//Action
Future<void> createNursery(Function(dynamic) onSuccess,
Function(String) onError, String activityNursery,
{List<File> files}) async {
FormData formData =
new FormData.fromMap({"activityNursery": activityNursery});
try {
await dio
.post("${ConstCommon.baseUrl}/api/createNursery", data: formData)
.then((value) {
onSuccess(value);
}).catchError((onError) {
onError(AppException.handleError(onError));
});
} catch (e) {
onError(AppException.handleError(e));
}
}
}

+ 29
- 3
lib/presentation/screens/actions/nursery/sc_edit_action_nursery.dart View File

@@ -1,4 +1,7 @@
import 'dart:convert';

import 'package:farm_tpf/custom_model/Nursery.dart';
import 'package:farm_tpf/data/api/app_exception.dart';
import 'package:farm_tpf/data/repository/repository.dart';
import 'package:farm_tpf/models/index.dart';
import 'package:farm_tpf/presentation/custom_widgets/widget_media_helper.dart';
@@ -58,7 +61,7 @@ class _EditActionNurseryState extends State<EditActionNurseryScreen> {
_nursery.nurseryDetail = new List<NurseryDetail>();
flutterToast = FlutterToast(context);
//UPDATE
if (_nursery != null) {
if (_nursery.cropId != null) {
try {
executeTime =
DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").parse(_nursery.executeDate);
@@ -68,10 +71,27 @@ class _EditActionNurseryState extends State<EditActionNurseryScreen> {
var parsedExecuteDate =
DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(executeTime);
_nursery.executeDate = "$parsedExecuteDate";
//TODO: update cropid
_nursery.cropId = 1;
}
executeTimeView = DateFormat("dd/MM/yyyy HH:mm").format(executeTime);
}

_validateInputs() async {
if (_formKey.currentState.validate()) {
_formKey.currentState.save();
var activityNursery = jsonEncode(_nursery.toJson()).toString();
_repository.createNursery((value) {
print("post ok");
}, (error) {
print("--------------------------------");
print(error);
}, activityNursery);
} else {
_autoValidate = true;
}
}

Widget _btnExecuteTimePicker() {
return FlatButton(
padding: EdgeInsets.only(top: 0.0, right: 0.0, bottom: 0.0, left: 0.0),
@@ -141,7 +161,7 @@ class _EditActionNurseryState extends State<EditActionNurseryScreen> {
child: Text(
changeSupply.currentSupply.name == null
? "Loại giá thể"
: _nursery.substrates.toString(),
: changeSupply.currentSupply.name.toString(),
style: TextStyle(
fontSize: 14.0, color: Colors.black87)))),
Icon(
@@ -416,7 +436,13 @@ class _EditActionNurseryState extends State<EditActionNurseryScreen> {
Icons.done,
color: Colors.black,
),
onPressed: () {},
onPressed: () {
FocusScopeNode currentFocus = FocusScope.of(context);
if (!currentFocus.hasPrimaryFocus) {
currentFocus.unfocus();
}
_validateInputs();
},
);
return <Widget>[iconButton];
}

+ 2
- 2
pubspec.lock View File

@@ -353,7 +353,7 @@ packages:
source: hosted
version: "0.14.0+3"
http:
dependency: transitive
dependency: "direct main"
description:
name: http
url: "https://pub.dartlang.org"
@@ -367,7 +367,7 @@ packages:
source: hosted
version: "2.2.0"
http_parser:
dependency: transitive
dependency: "direct main"
description:
name: http_parser
url: "https://pub.dartlang.org"

+ 2
- 0
pubspec.yaml View File

@@ -38,6 +38,8 @@ dependencies:
intl: ^0.16.1
flutter_datetime_picker: ^1.3.8
# analyzer: ^0.39.14
http: ^0.12.2
http_parser: ^3.1.4

dev_dependencies:
flutter_test:

Loading…
Cancel
Save