Skip to content

Commit

Permalink
#42 add workaround other species orgid
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Werfling committed Nov 12, 2023
1 parent 26e3c0e commit f84a5f9
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 37 deletions.
20 changes: 20 additions & 0 deletions lib/Controllers/SpeciesController.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,26 @@ class SpeciesController extends GetxController {
speciesList.assignAll(species.map((data) => Species.fromDbJson(data)).toList());
}

Species? getSpeciesById(int id) {
for (var specie in speciesList) {
if (specie.id == id) {
return specie;
}
}

return null;
}

Species? getSpeciesByOrgId(int orgid) {
for (var specie in speciesList) {
if (specie.orgid == orgid) {
return specie;
}
}

return null;
}

/// getSpeciesName
String? getSpeciesName(int id) {
for (var specie in speciesList) {
Expand Down
4 changes: 2 additions & 2 deletions lib/Db/DBHelper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ class DBHelper {

/// insertSighting
static Future<int> insertSighting(Sighting newSighting) async {
return await _db?.insert(_tableNameSighting, newSighting.toJson(false, true)) ?? 1;
return await _db?.insert(_tableNameSighting, newSighting.toJson(false, true, false)) ?? 1;
}

/// querySighting
Expand All @@ -225,7 +225,7 @@ class DBHelper {
static Future<int> updateSighting(Sighting uSighting) async {
return await _db!.update(
_tableNameSighting,
uSighting.toJson(false, true),
uSighting.toJson(false, true, false),
where: 'id=?',
whereArgs: [uSighting.id]
);
Expand Down
31 changes: 30 additions & 1 deletion lib/Models/Sighting.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import 'dart:convert';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:intl/intl.dart';
import 'package:mwpaapp/Constants.dart';
import 'package:mwpaapp/Util/UtilCheckJson.dart';
import 'package:mwpaapp/Util/UtilTourFId.dart';

import '../Controllers/SpeciesController.dart';

/// Sighting
class Sighting {

Expand Down Expand Up @@ -134,7 +140,7 @@ class Sighting {
}

/// toJson
Map<String, dynamic> toJson(bool withId, bool withSyncStatus) {
Map<String, dynamic> toJson(bool withId, bool withSyncStatus, bool? forSync) {
final Map<String, dynamic> data = <String, dynamic>{};

if (withId) {
Expand Down Expand Up @@ -190,6 +196,29 @@ class Sighting {
data['freq_behaviour'] = freq_behaviour;
data['recognizable_animals'] = recognizable_animals;
data['other_species'] = other_species;

// only for repairing a bug
if (forSync != null) {
if (forSync) {
Map<String, dynamic> newdata = {};
Map<String, dynamic> olddata = jsonDecode(other_species!);
final SpeciesController _speciesController = Get.find<SpeciesController>();

olddata.forEach((key, value) {
if (value != "") {
var species = _speciesController.getSpeciesById(int.parse(value));

if (species != null) {
var orgid = species.orgid;
newdata["$key"] = "$orgid";
}
}
});

data['other_species'] = const JsonEncoder().convert(newdata);
}
}

data['other'] = other;
data['other_vehicle'] = other_vehicle;
data['note'] = note;
Expand Down
11 changes: 9 additions & 2 deletions lib/Mwpa/Models/SightingSaveResponse.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import 'package:mwpaapp/Mwpa/Models/DefaultReturn.dart';

class SightingSaveResponse extends DefaultReturn {
final String? unid;
final bool? canDelete;

SightingSaveResponse({required super.statusCode, super.msg, this.unid});
SightingSaveResponse({required super.statusCode, super.msg, this.unid, this.canDelete});

factory SightingSaveResponse.fromJson(Map<String, dynamic> json) {
String? msg;
String? unid;
bool? canDelete;

if (json.containsKey('unid')) {
unid = json['unid'];
Expand All @@ -17,10 +19,15 @@ class SightingSaveResponse extends DefaultReturn {
msg = json['msg'];
}

if (json.containsKey('canDelete')) {
canDelete = json['canDelete'];
}

return SightingSaveResponse(
statusCode: json['statusCode'],
msg: msg,
unid: unid
unid: unid,
canDelete: canDelete
);
}
}
2 changes: 1 addition & 1 deletion lib/Mwpa/MwpaAPI.dart
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ class MwpaApi {
try {
var url = getUrl(MwpaApi.URL_SIGHTING_SAVE);

var postBody = jsonEncode(sigh.toJson(false, false));
var postBody = jsonEncode(sigh.toJson(false, false, true));

var response = await http.post(
Uri.parse(url),
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.7+8
version: 1.0.10+11

environment:
sdk: ">=2.17.5 <3.0.0"
Expand Down
30 changes: 0 additions & 30 deletions test/widget_test.dart

This file was deleted.

0 comments on commit f84a5f9

Please sign in to comment.