Skip to content

Commit

Permalink
apply flyer to use cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Stift committed Jan 23, 2025
1 parent c46ed27 commit 1812c05
Show file tree
Hide file tree
Showing 23 changed files with 324 additions and 126 deletions.
4 changes: 4 additions & 0 deletions lib/app/services/converters.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ 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_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_create_model.dart';
Expand Down Expand Up @@ -40,3 +42,5 @@ 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';
part 'converters/flyer_create_model_parsing.dart';
part 'converters/flyer_update_model_parsing.dart';
14 changes: 14 additions & 0 deletions lib/app/services/converters/campaign_action_parsing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ extension CampaignActionParsing on CampaignAction {
return model;
}

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

return model;
}

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

return model;
}

PosterListItemModel getPosterUpdateAsPosterListItem(DateTime originalCreatedAt) {
var updateModel = getAsPosterUpdate().transformToPosterDetailModel();
return PosterListItemModel(
Expand Down
22 changes: 22 additions & 0 deletions lib/app/services/converters/flyer_create_model_parsing.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
part of '../converters.dart';

extension FlyerCreateModelParsing on FlyerCreateModel {
FlyerDetailModel transformToFlyerDetailModel(String temporaryId) {
return FlyerDetailModel(
id: temporaryId,
address: address,
flyerCount: flyerCount,
location: location,
createdAt: '${DateTime.now().getAsLocalDateTimeString()}*', // should mark this as preliminary
isCached: true,
);
}

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

extension FlyerUpdateModelParsing on FlyerUpdateModel {
FlyerDetailModel transformToFlyerDetailModel() {
var newFlyerDetail = oldFlyerDetail.copyWith(
address: address,
flyerCount: flyerCount,
isCached: true,
);
return newFlyerDetail;
}

MarkerItemModel transformToVirtualMarkerItem() {
return MarkerItemModel.virtual(
id: int.parse(id),
status: PoiServiceType.flyer.name,
location: location,
);
}
}
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 @@ -54,6 +54,7 @@ extension PoiParsing on Poi {
address: poi.address.transformToAddressModel(),
flyerCount: poi.flyerSpot!.flyerCount.toInt(),
createdAt: poi.createdAt.getAsLocalDateTimeString(),
location: coords.transformToLatLng(),
);
}

Expand Down
44 changes: 1 addition & 43 deletions lib/app/services/gruene_api_campaigns_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@ 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/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_list_item_model.dart';
import 'package:gruene_app/swagger_generated_code/gruene_api.swagger.dart';
import 'package:maplibre_gl/maplibre_gl.dart';

class GrueneApiCampaignsService {
abstract class GrueneApiCampaignsService {
late GrueneApi grueneApi;

final PoiServiceType poiType;
Expand Down Expand Up @@ -42,25 +38,6 @@ class GrueneApiCampaignsService {
return getPoisResult.body!.data.map((layerItem) => layerItem.transformToMapLayer()).toList();
}

Future<MarkerItemModel> createNewFlyer(FlyerCreateModel newFlyer) async {
final requestParam = CreatePoi(
coords: newFlyer.location.transformToGeoJsonCoords(),
type: poiType.transformToApiCreateType(),
address: newFlyer.address.transformToPoiAddress(),
flyerSpot: PoiFlyerSpot(
flyerCount: newFlyer.flyerCount.toDouble(),
),
);
// saving POI
final newPoiResponse = await grueneApi.v1CampaignsPoisPost(body: requestParam);

return newPoiResponse.body!.transformToMarkerItem();
}

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

Future<T> getPoi<T>(String poiId, T Function(Poi) transform) async {
final poiResponse = await grueneApi.v1CampaignsPoisPoiIdGet(poiId: poiId);
return transform(poiResponse.body!);
Expand All @@ -70,23 +47,4 @@ class GrueneApiCampaignsService {
// ignore: unused_local_variable
final deletePoiResponse = await grueneApi.v1CampaignsPoisPoiIdDelete(poiId: poiId);
}

Future<MarkerItemModel> updateFlyer(FlyerUpdateModel flyerUpdate) async {
var dtoUpdate = UpdatePoi(
address: flyerUpdate.address.transformToPoiAddress(),
flyerSpot: PoiFlyerSpot(
flyerCount: flyerUpdate.flyerCount.toDouble(),
),
);
var updatePoiResponse = await grueneApi.v1CampaignsPoisPoiIdPut(poiId: flyerUpdate.id, body: dtoUpdate);

return updatePoiResponse.body!.transformToMarkerItem();
}

Future<List<PosterListItemModel>> getMyPosters() async {
final getPoisType = poiType.transformToApiSelfGetType();

final getPoisResult = await grueneApi.v1CampaignsPoisSelfGet(type: getPoisType);
return getPoisResult.body!.data.map((p) => p.transformToPosterListItem()).toList();
}
}
43 changes: 43 additions & 0 deletions lib/app/services/gruene_api_flyer_service.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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/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/marker_item_model.dart';
import 'package:gruene_app/swagger_generated_code/gruene_api.swagger.dart';

class GrueneApiFlyerService extends GrueneApiCampaignsService {
GrueneApiFlyerService() : super(poiType: PoiServiceType.flyer);

Future<MarkerItemModel> createNewFlyer(FlyerCreateModel newFlyer) async {
final requestParam = CreatePoi(
coords: newFlyer.location.transformToGeoJsonCoords(),
type: poiType.transformToApiCreateType(),
address: newFlyer.address.transformToPoiAddress(),
flyerSpot: PoiFlyerSpot(
flyerCount: newFlyer.flyerCount.toDouble(),
),
);
// saving POI
final newPoiResponse = await grueneApi.v1CampaignsPoisPost(body: requestParam);

return newPoiResponse.body!.transformToMarkerItem();
}

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

Future<MarkerItemModel> updateFlyer(FlyerUpdateModel flyerUpdate) async {
var dtoUpdate = UpdatePoi(
address: flyerUpdate.address.transformToPoiAddress(),
flyerSpot: PoiFlyerSpot(
flyerCount: flyerUpdate.flyerCount.toDouble(),
),
);
var updatePoiResponse = await grueneApi.v1CampaignsPoisPoiIdPut(poiId: flyerUpdate.id, body: dtoUpdate);

return updatePoiResponse.body!.transformToMarkerItem();
}
}
7 changes: 7 additions & 0 deletions lib/app/services/gruene_api_poster_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,11 @@ class GrueneApiPosterService extends GrueneApiCampaignsService {
Future<PosterListItemModel> getPoiAsPosterListItem(String poiId) {
return getPoi(poiId, (p) => p.transformToPosterListItem());
}

Future<List<PosterListItemModel>> getMyPosters() async {
final getPoisType = poiType.transformToApiSelfGetType();

final getPoisResult = await grueneApi.v1CampaignsPoisSelfGet(type: getPoisType);
return getPoisResult.body!.data.map((p) => p.transformToPosterListItem()).toList();
}
}
2 changes: 1 addition & 1 deletion lib/app/widgets/app_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,6 @@ class _RefreshButtonState extends State<RefreshButton> {
}

void _flushCachedData() {
campaignActionCache.flushCachedItems();
campaignActionCache.flushCache();
}
}
Loading

0 comments on commit 1812c05

Please sign in to comment.