Skip to content

Commit

Permalink
apply door to use cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Stift committed Jan 23, 2025
1 parent e477cb9 commit c46ed27
Show file tree
Hide file tree
Showing 23 changed files with 443 additions and 151 deletions.
4 changes: 4 additions & 0 deletions lib/app/services/converters.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import 'dart:convert';
import 'package:gruene_app/app/services/enums.dart';
import 'package:gruene_app/app/services/nominatim_service.dart';
import 'package:gruene_app/features/campaigns/helper/campaign_action.dart';
import 'package:gruene_app/features/campaigns/models/doors/door_create_model.dart';
import 'package:gruene_app/features/campaigns/models/doors/door_detail_model.dart';
import 'package:gruene_app/features/campaigns/models/doors/door_update_model.dart';
import 'package:gruene_app/features/campaigns/models/flyer/flyer_detail_model.dart';
import 'package:gruene_app/features/campaigns/models/map_layer_model.dart';
import 'package:gruene_app/features/campaigns/models/marker_item_model.dart';
Expand Down Expand Up @@ -36,3 +38,5 @@ part 'converters/poster_update_model_parsing.dart';
part 'converters/campaign_action_parsing.dart';
part 'converters/map_string_dynamic_converter.dart';
part 'converters/string_extension.dart';
part 'converters/door_update_model_parsing.dart';
part 'converters/door_create_model_parsing.dart';
18 changes: 16 additions & 2 deletions lib/app/services/converters/campaign_action_parsing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,22 @@ extension CampaignActionParsing on CampaignAction {
return model;
}

DoorCreateModel getAsDoorCreate() {
var data = jsonDecode(serialized!) as Map<String, dynamic>;
var model = DoorCreateModel.fromJson(data.convertLatLongField());

return model;
}

DoorUpdateModel getAsDoorUpdate() {
var data = jsonDecode(serialized!) as Map<String, dynamic>;
var model = DoorUpdateModel.fromJson(data.updateIdField(poiId!).convertLatLongField());

return model;
}

PosterListItemModel getPosterUpdateAsPosterListItem(DateTime originalCreatedAt) {
var updateModel = getAsPosterUpdate().transformToPosterDetailModel(poiId!);
var updateModel = getAsPosterUpdate().transformToPosterDetailModel();
return PosterListItemModel(
id: updateModel.id,
thumbnailUrl: updateModel.thumbnailUrl,
Expand All @@ -30,7 +44,7 @@ extension CampaignActionParsing on CampaignAction {
}

PosterListItemModel getPosterCreateAsPosterListItem() {
var createModel = getAsPosterCreate().transformToPosterDetailModel(poiTempId);
var createModel = getAsPosterCreate().transformToPosterDetailModel(poiTempId.toString());
return PosterListItemModel(
id: createModel.id,
thumbnailUrl: createModel.thumbnailUrl,
Expand Down
23 changes: 23 additions & 0 deletions lib/app/services/converters/door_create_model_parsing.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
part of '../converters.dart';

extension DoorCreateModelParsing on DoorCreateModel {
DoorDetailModel transformToDoorDetailModel(String temporaryId) {
return DoorDetailModel(
id: temporaryId,
address: address,
closedDoors: closedDoors,
openedDoors: openedDoors,
location: location,
createdAt: '${DateTime.now().getAsLocalDateTimeString()}*', // should mark this as preliminary
isCached: true,
);
}

MarkerItemModel transformToVirtualMarkerItem(int temporaryId) {
return MarkerItemModel.virtual(
id: temporaryId,
status: PoiServiceType.door.name,
location: location,
);
}
}
21 changes: 21 additions & 0 deletions lib/app/services/converters/door_update_model_parsing.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
part of '../converters.dart';

extension DoorUpdateModelParsing on DoorUpdateModel {
DoorDetailModel transformToDoorDetailModel() {
var newDoorDetail = oldDoorDetail.copyWith(
address: address,
closedDoors: closedDoors,
openedDoors: openedDoors,
isCached: true,
);
return newDoorDetail;
}

MarkerItemModel transformToVirtualMarkerItem() {
return MarkerItemModel.virtual(
id: int.parse(id),
status: PoiServiceType.door.name,
location: location,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ part of '../converters.dart';

extension MapStringDynamicConverter on Map<String, dynamic> {
Map<String, dynamic> convertLatLongField({String fieldName = 'location'}) {
this[fieldName] = (this[fieldName] as List<dynamic>).cast<double>();
if (containsKey(fieldName) && this[fieldName] is List<dynamic>) {
this[fieldName] = (this[fieldName] as List<dynamic>).cast<double>().toList();
}
return this;
}

Expand Down
1 change: 1 addition & 0 deletions lib/app/services/converters/poi_parsing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ extension PoiParsing on Poi {
address: poi.address.transformToAddressModel(),
openedDoors: poi.house!.countOpenedDoors.toInt(),
closedDoors: poi.house!.countClosedDoors.toInt(),
location: poi.coords.transformToLatLng(),
createdAt: poi.createdAt.getAsLocalDateTimeString(),
);
}
Expand Down
33 changes: 33 additions & 0 deletions lib/app/services/converters/poi_service_type_parsing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,37 @@ extension PoiServiceTypeParsing on PoiServiceType {
return typeName;
}
}

CampaignActionType getCacheDeleteAction() {
switch (this) {
case PoiServiceType.poster:
return CampaignActionType.deletePoster;
case PoiServiceType.door:
return CampaignActionType.deleteDoor;
case PoiServiceType.flyer:
return CampaignActionType.deleteFlyer;
}
}

CampaignActionType getCacheEditAction() {
switch (this) {
case PoiServiceType.poster:
return CampaignActionType.editPoster;
case PoiServiceType.door:
return CampaignActionType.editDoor;
case PoiServiceType.flyer:
return CampaignActionType.editFlyer;
}
}

CampaignActionType getCacheAddAction() {
switch (this) {
case PoiServiceType.poster:
return CampaignActionType.addPoster;
case PoiServiceType.door:
return CampaignActionType.addDoor;
case PoiServiceType.flyer:
return CampaignActionType.addFlyer;
}
}
}
4 changes: 2 additions & 2 deletions lib/app/services/converters/poster_create_model_parsing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ extension PosterCreateModelParsing on PosterCreateModel {
);
}

PosterDetailModel transformToPosterDetailModel(int temporaryId) {
PosterDetailModel transformToPosterDetailModel(String temporaryId) {
return PosterDetailModel(
id: temporaryId.toString(),
id: temporaryId,
status: PosterStatus.ok,
address: address,
thumbnailUrl: imageFileLocation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ extension PosterUpdateModelParsing on PosterUpdateModel {
);
}

PosterDetailModel transformToPosterDetailModel(int temporaryId) {
PosterDetailModel transformToPosterDetailModel() {
var newPosterDetail = oldPosterDetail.copyWith(
status: status,
address: address,
Expand Down
49 changes: 2 additions & 47 deletions lib/app/services/gruene_api_campaigns_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@ import 'package:flutter/foundation.dart';
import 'package:get_it/get_it.dart';
import 'package:gruene_app/app/services/converters.dart';
import 'package:gruene_app/app/services/enums.dart';
import 'package:gruene_app/features/campaigns/models/doors/door_create_model.dart';
import 'package:gruene_app/features/campaigns/models/doors/door_detail_model.dart';
import 'package:gruene_app/features/campaigns/models/doors/door_update_model.dart';
import 'package:gruene_app/features/campaigns/models/flyer/flyer_create_model.dart';
import 'package:gruene_app/features/campaigns/models/flyer/flyer_detail_model.dart';
import 'package:gruene_app/features/campaigns/models/flyer/flyer_update_model.dart';
import 'package:gruene_app/features/campaigns/models/map_layer_model.dart';
import 'package:gruene_app/features/campaigns/models/marker_item_model.dart';
import 'package:gruene_app/features/campaigns/models/posters/poster_detail_model.dart';
import 'package:gruene_app/features/campaigns/models/posters/poster_list_item_model.dart';
import 'package:gruene_app/swagger_generated_code/gruene_api.swagger.dart';
import 'package:maplibre_gl/maplibre_gl.dart';
Expand Down Expand Up @@ -46,22 +42,6 @@ class GrueneApiCampaignsService {
return getPoisResult.body!.data.map((layerItem) => layerItem.transformToMapLayer()).toList();
}

Future<MarkerItemModel> createNewDoor(DoorCreateModel newDoor) async {
final requestParam = CreatePoi(
coords: newDoor.location.transformToGeoJsonCoords(),
type: poiType.transformToApiCreateType(),
address: newDoor.address.transformToPoiAddress(),
house: PoiHouse(
countOpenedDoors: newDoor.openedDoors.toDouble(),
countClosedDoors: newDoor.closedDoors.toDouble(),
),
);
// saving POI
final newPoiResponse = await grueneApi.v1CampaignsPoisPost(body: requestParam);

return newPoiResponse.body!.transformToMarkerItem();
}

Future<MarkerItemModel> createNewFlyer(FlyerCreateModel newFlyer) async {
final requestParam = CreatePoi(
coords: newFlyer.location.transformToGeoJsonCoords(),
Expand All @@ -77,23 +57,11 @@ class GrueneApiCampaignsService {
return newPoiResponse.body!.transformToMarkerItem();
}

Future<PosterDetailModel> getPoiAsPosterDetail(String poiId) async {
return _getPoi(poiId, (p) => p.transformPoiToPosterDetail());
}

Future<PosterListItemModel> getPoiAsPosterListItem(String poiId) {
return _getPoi(poiId, (p) => p.transformToPosterListItem());
}

Future<DoorDetailModel> getPoiAsDoorDetail(String poiId) {
return _getPoi(poiId, (p) => p.transformPoiToDoorDetail());
}

Future<FlyerDetailModel> getPoiAsFlyerDetail(String poiId) {
return _getPoi(poiId, (p) => p.transformPoiToFlyerDetail());
return getPoi(poiId, (p) => p.transformPoiToFlyerDetail());
}

Future<T> _getPoi<T>(String poiId, T Function(Poi) transform) async {
Future<T> getPoi<T>(String poiId, T Function(Poi) transform) async {
final poiResponse = await grueneApi.v1CampaignsPoisPoiIdGet(poiId: poiId);
return transform(poiResponse.body!);
}
Expand All @@ -103,19 +71,6 @@ class GrueneApiCampaignsService {
final deletePoiResponse = await grueneApi.v1CampaignsPoisPoiIdDelete(poiId: poiId);
}

Future<MarkerItemModel> updateDoor(DoorUpdateModel doorUpdate) async {
var dtoUpdate = UpdatePoi(
address: doorUpdate.address.transformToPoiAddress(),
house: PoiHouse(
countOpenedDoors: doorUpdate.openedDoors.toDouble(),
countClosedDoors: doorUpdate.closedDoors.toDouble(),
),
);
var updatePoiResponse = await grueneApi.v1CampaignsPoisPoiIdPut(poiId: doorUpdate.id, body: dtoUpdate);

return updatePoiResponse.body!.transformToMarkerItem();
}

Future<MarkerItemModel> updateFlyer(FlyerUpdateModel flyerUpdate) async {
var dtoUpdate = UpdatePoi(
address: flyerUpdate.address.transformToPoiAddress(),
Expand Down
45 changes: 45 additions & 0 deletions lib/app/services/gruene_api_door_service.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import 'package:gruene_app/app/services/converters.dart';
import 'package:gruene_app/app/services/enums.dart';
import 'package:gruene_app/app/services/gruene_api_campaigns_service.dart';
import 'package:gruene_app/features/campaigns/models/doors/door_create_model.dart';
import 'package:gruene_app/features/campaigns/models/doors/door_detail_model.dart';
import 'package:gruene_app/features/campaigns/models/doors/door_update_model.dart';
import 'package:gruene_app/features/campaigns/models/marker_item_model.dart';
import 'package:gruene_app/swagger_generated_code/gruene_api.swagger.dart';

class GrueneApiDoorService extends GrueneApiCampaignsService {
GrueneApiDoorService() : super(poiType: PoiServiceType.door);

Future<MarkerItemModel> createNewDoor(DoorCreateModel newDoor) async {
final requestParam = CreatePoi(
coords: newDoor.location.transformToGeoJsonCoords(),
type: poiType.transformToApiCreateType(),
address: newDoor.address.transformToPoiAddress(),
house: PoiHouse(
countOpenedDoors: newDoor.openedDoors.toDouble(),
countClosedDoors: newDoor.closedDoors.toDouble(),
),
);
// saving POI
final newPoiResponse = await grueneApi.v1CampaignsPoisPost(body: requestParam);

return newPoiResponse.body!.transformToMarkerItem();
}

Future<MarkerItemModel> updateDoor(DoorUpdateModel doorUpdate) async {
var dtoUpdate = UpdatePoi(
address: doorUpdate.address.transformToPoiAddress(),
house: PoiHouse(
countOpenedDoors: doorUpdate.openedDoors.toDouble(),
countClosedDoors: doorUpdate.closedDoors.toDouble(),
),
);
var updatePoiResponse = await grueneApi.v1CampaignsPoisPoiIdPut(poiId: doorUpdate.id, body: dtoUpdate);

return updatePoiResponse.body!.transformToMarkerItem();
}

Future<DoorDetailModel> getPoiAsDoorDetail(String poiId) {
return getPoi(poiId, (p) => p.transformPoiToDoorDetail());
}
}
10 changes: 10 additions & 0 deletions lib/app/services/gruene_api_poster_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import 'package:gruene_app/app/services/gruene_api_campaigns_service.dart';
import 'package:gruene_app/features/campaigns/helper/file_cache_manager.dart';
import 'package:gruene_app/features/campaigns/models/marker_item_model.dart';
import 'package:gruene_app/features/campaigns/models/posters/poster_create_model.dart';
import 'package:gruene_app/features/campaigns/models/posters/poster_detail_model.dart';
import 'package:gruene_app/features/campaigns/models/posters/poster_list_item_model.dart';
import 'package:gruene_app/features/campaigns/models/posters/poster_update_model.dart';
import 'package:gruene_app/swagger_generated_code/gruene_api.swagger.dart';
import 'package:http/http.dart' as http;
Expand Down Expand Up @@ -76,4 +78,12 @@ class GrueneApiPosterService extends GrueneApiCampaignsService {
fileManager.deleteFile(imageFileLocation);
return savePoiPhotoResponse;
}

Future<PosterDetailModel> getPoiAsPosterDetail(String poiId) async {
return getPoi(poiId, (p) => p.transformPoiToPosterDetail());
}

Future<PosterListItemModel> getPoiAsPosterListItem(String poiId) {
return getPoi(poiId, (p) => p.transformToPosterListItem());
}
}
Loading

0 comments on commit c46ed27

Please sign in to comment.