Skip to content

Commit

Permalink
fix: Saving sponsors to Isar DB
Browse files Browse the repository at this point in the history
  • Loading branch information
1grzyb1 committed Mar 17, 2024
1 parent aa227ba commit a4f3920
Show file tree
Hide file tree
Showing 20 changed files with 989 additions and 163 deletions.
256 changes: 128 additions & 128 deletions lib/app/router.gr.dart

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions lib/data/api/api_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:odyssey_mobile/data/api/models/info.dart';
import 'package:odyssey_mobile/data/api/models/info_category.dart';
import 'package:odyssey_mobile/data/api/models/performance.dart';
import 'package:odyssey_mobile/data/api/models/problem.dart';
import 'package:odyssey_mobile/data/api/models/sponsor.dart';
import 'package:odyssey_mobile/data/api/models/stage.dart';
import 'package:retrofit/retrofit.dart';
import 'package:dio/dio.dart';
Expand All @@ -25,6 +26,9 @@ abstract class ApiService {
@GET('/info')
Future<List<InfoModelApi>> getInfo();

@GET('/sponsor')
Future<List<List<SponsorModelApi>>> getSponsor();

@GET('/info/category')
Future<List<InfoCategoryModelApi>> getInfoCategories();

Expand Down
27 changes: 27 additions & 0 deletions lib/data/api/api_service.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions lib/data/api/models/sponsor.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'package:json_annotation/json_annotation.dart';

part 'sponsor.g.dart';

@JsonSerializable(createToJson: false)
class SponsorModelApi {
SponsorModelApi({
required this.id,
required this.row,
required this.column,
});

final int id;
final int row;
final int column;

factory SponsorModelApi.fromJson(Map<String, dynamic> json) => _$SponsorModelApiFromJson(json);
}
14 changes: 14 additions & 0 deletions lib/data/api/models/sponsor.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion lib/data/data_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:odyssey_mobile/data/api/models/info.dart';
import 'package:odyssey_mobile/data/api/models/info_category.dart';
import 'package:odyssey_mobile/data/api/models/performance.dart';
import 'package:odyssey_mobile/data/api/models/problem.dart';
import 'package:odyssey_mobile/data/api/models/sponsor.dart';
import 'package:odyssey_mobile/data/api/models/stage.dart';
import 'package:odyssey_mobile/data/db/db_service.dart';
import 'package:odyssey_mobile/domain/core/failures.dart';
Expand All @@ -34,14 +35,15 @@ class DataRepositoryImpl implements DataRepository {
final externalVersion = versionHttpResult.data['version'] as int;
final savedVersion = _sharedPrefs.getInt('version') ?? -1;

if (forceUpdate || externalVersion > savedVersion) {
if (true || externalVersion > savedVersion) {
final futures = await Future.wait([
// _apiService.getCities(),
_apiService.getInfo(),
_apiService.getInfoCategories(),
_apiService.getProblems(),
_apiService.getSchedule(),
_apiService.getStages(),
_apiService.getSponsor(),
], eagerError: true);

/// TODO update for future editions with multiple cities
Expand All @@ -52,6 +54,7 @@ class DataRepositoryImpl implements DataRepository {
final problems = futures[2] as List<ProblemModelApi>;
final performances = futures[3] as List<PerformanceModelApi>;
final stages = futures[4] as List<StageModelApi>;
final sponsors = futures[5] as List<List<SponsorModelApi>>;

List<int> previousFavIds = [];
if (savedVersion != -1 && keepFavsOnUpdate) {
Expand All @@ -69,6 +72,7 @@ class DataRepositoryImpl implements DataRepository {
stageModels: stages,
problemModels: problems,
previousFavIds: previousFavIds,
sponsors: sponsors
);

// On full success, save version.
Expand Down
4 changes: 3 additions & 1 deletion lib/data/db/db_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:odyssey_mobile/data/api/models/info_category.dart';
import 'package:odyssey_mobile/data/api/models/performance.dart';
import 'package:odyssey_mobile/data/api/models/problem.dart';
import 'package:odyssey_mobile/data/api/models/stage.dart';
import 'package:odyssey_mobile/data/api/models/sponsor.dart';
import 'package:odyssey_mobile/domain/entities/city_data.dart';
import 'package:odyssey_mobile/domain/entities/performance.dart';
import 'package:odyssey_mobile/domain/entities/schedule_category_entity.dart';
Expand All @@ -22,7 +23,8 @@ abstract class DbService {
required List<PerformanceModelApi> performanceModels,
required List<StageModelApi> stageModels,
required List<ProblemModelApi> problemModels,
required List<int> previousFavIds});
required List<int> previousFavIds,
required List<List<SponsorModelApi>> sponsors});

Future<CityData?> readCityData(int cityId);

Expand Down
4 changes: 4 additions & 0 deletions lib/data/db/hive/hive_data_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import 'package:odyssey_mobile/data/db/hive/models/performance_group.dart';
import 'package:odyssey_mobile/data/db/hive/models/stage.dart';
import 'package:odyssey_mobile/data/other/divisions.dart';

import '../../api/models/sponsor.dart';

// Adapt for multiple cities
abstract class HiveDataAdapter {
static List<CityDataHiveModel> convertCityData({
Expand All @@ -24,6 +26,7 @@ abstract class HiveDataAdapter {
required List<ProblemModelApi> problemModels,
required List<int> previousFavIds,
required Box<PerformanceHiveModel> performanceBox,
required List<List<SponsorModelApi>> sponsors
}) {
final List<CityDataHiveModel> citiesDb = [];
for (final city in cityModels) {
Expand All @@ -38,6 +41,7 @@ abstract class HiveDataAdapter {
previousFavIds: previousFavIds,
performanceBox: performanceBox),
stages: convertStages(stageModels),
sponsors: sponsors
));
}
return citiesDb;
Expand Down
4 changes: 4 additions & 0 deletions lib/data/db/hive/hive_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import 'package:odyssey_mobile/data/db/hive/models/stage.dart';
import 'package:odyssey_mobile/domain/entities/performance.dart';
import 'package:odyssey_mobile/domain/entities/schedule_category_entity.dart';

import '../../api/models/sponsor.dart';

/// Requires awaiting [init] method.
class HiveDbService extends DbService {
late final Box<CityDataHiveModel> _box;
Expand Down Expand Up @@ -71,6 +73,7 @@ class HiveDbService extends DbService {
required List<StageModelApi> stageModels,
required List<ProblemModelApi> problemModels,
required List<int> previousFavIds,
required List<List<SponsorModelApi>> sponsors
}) async {
// save performances first, to allow them to work as HiveObjects
final performances = HiveDataAdapter.convertPerformances(performanceModels, previousFavIds);
Expand All @@ -85,6 +88,7 @@ class HiveDbService extends DbService {
problemModels: problemModels,
previousFavIds: previousFavIds,
performanceBox: _performanceBox,
sponsors: sponsors
);
await _box.addAll(data);
// for (final group in data.first.performanceGroups) {
Expand Down
7 changes: 7 additions & 0 deletions lib/data/db/hive/models/city_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import 'package:odyssey_mobile/data/db/hive/models/stage.dart';

import 'package:odyssey_mobile/domain/entities/city_data.dart';

import '../../../api/models/sponsor.dart';

part 'city_data.g.dart';

@HiveType(typeId: 0)
Expand All @@ -15,6 +17,7 @@ class CityDataHiveModel extends CityData with HiveObjectMixin {
required this.infoGroups,
required this.performanceGroups,
required this.stages,
required this.sponsors,
});

@override
Expand All @@ -36,4 +39,8 @@ class CityDataHiveModel extends CityData with HiveObjectMixin {
@HiveField(4)
@override
final List<StageHiveModel> stages;

@HiveField(5)
@override
final List<List<SponsorModelApi>> sponsors;
}
9 changes: 7 additions & 2 deletions lib/data/db/hive/models/city_data.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 49 additions & 24 deletions lib/data/db/isar/isar_data_adapters.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:odyssey_mobile/data/api/models/info.dart';
import 'package:odyssey_mobile/data/api/models/info_category.dart';
import 'package:odyssey_mobile/data/api/models/performance.dart';
import 'package:odyssey_mobile/data/api/models/problem.dart';
import 'package:odyssey_mobile/data/api/models/sponsor.dart';
import 'package:odyssey_mobile/data/api/models/stage.dart';
import 'package:odyssey_mobile/data/db/isar/models/city_data.dart';
import 'package:odyssey_mobile/data/db/isar/models/info.dart';
Expand All @@ -13,30 +14,35 @@ import 'package:odyssey_mobile/data/db/isar/models/problem.dart';
import 'package:odyssey_mobile/data/db/isar/models/stage.dart';
import 'package:odyssey_mobile/data/other/divisions.dart';

import 'models/sponsor.dart';

abstract class IsarDataAdapters {
static List<ProblemModelDb> convertProblems(List<ProblemModelApi> apiModels) => apiModels
.map((e) => ProblemModelDb()
..name = e.name
..number = e.id)
.toList(growable: false);
static List<ProblemModelDb> convertProblems(
List<ProblemModelApi> apiModels) =>
apiModels
.map((e) => ProblemModelDb()
..name = e.name
..number = e.id)
.toList(growable: false);

// TODO Take city into account
static List<CityDataModelDb> convertCityData({
required List<CityModelApi> cityModels,
required List<InfoModelApi> infoModels,
required List<InfoCategoryModelApi> infoCategories,
required List<PerformanceModelApi> performanceModels,
required List<StageModelApi> stageModels,
required List<ProblemModelApi> problemModels,
required List<int> previousFavIds,
}) {
static List<CityDataModelDb> convertCityData(
{required List<CityModelApi> cityModels,
required List<InfoModelApi> infoModels,
required List<InfoCategoryModelApi> infoCategories,
required List<PerformanceModelApi> performanceModels,
required List<StageModelApi> stageModels,
required List<ProblemModelApi> problemModels,
required List<int> previousFavIds,
required List<List<SponsorModelApi>> sponsors}) {
List<CityDataModelDb> citiesDb = [];
for (final city in cityModels) {
final cityDb = CityDataModelDb()
..id = city.id
..cityId = city.id
..cityName = city.name
..infoIsarLinks.addAll(convertInfoCategories(infoCategories, infoModels))
..infoIsarLinks
.addAll(convertInfoCategories(infoCategories, infoModels))
..performanceGroupIsarLinks.addAll(convertPerformanceGroups(
performances: performanceModels,
problems: problemModels,
Expand All @@ -45,14 +51,16 @@ abstract class IsarDataAdapters {
))
// Sneaky mistake during refactoring led to adding stages to a getter...
// ..stages.addAll(convertStages(stageModels));
..stageIsarLinks.addAll(convertStages(stageModels));
..stageIsarLinks.addAll(convertStages(stageModels))
..sponsorIsarLinks.addAll(convertSponsors(sponsors));
citiesDb.add(cityDb);
}
return citiesDb;
}

static List<InfoGroupModelDb> convertInfoCategories(
List<InfoCategoryModelApi> infoCategoryModels, List<InfoModelApi> infoModels) {
List<InfoCategoryModelApi> infoCategoryModels,
List<InfoModelApi> infoModels) {
final List<InfoGroupModelDb> infoGroups = [];

for (final infoCategory in infoCategoryModels) {
Expand Down Expand Up @@ -109,8 +117,8 @@ abstract class IsarDataAdapters {
..age = division.number
..part = part
..league = league
..performancesIsarLinks
.addAll(convertPerformances(filteredPerformances, previousFavIds))
..performancesIsarLinks.addAll(convertPerformances(
filteredPerformances, previousFavIds))
..day = day);
++groupId;
}
Expand Down Expand Up @@ -143,9 +151,26 @@ abstract class IsarDataAdapters {
..isFavourite = previousFavIds.contains(e.id),
);

static List<StageModelDb> convertStages(List<StageModelApi> apiModels) => apiModels
.map((e) => StageModelDb()
..number = e.number
..name = e.name)
.toList(growable: false);
static List<StageModelDb> convertStages(List<StageModelApi> apiModels) =>
apiModels
.map((e) => StageModelDb()
..number = e.number
..name = e.name)
.toList(growable: false);

static List<SponsorModelDb> convertSponsors(
List<List<SponsorModelApi>> sponsorsApi) {
List<SponsorModelDb> sponsorsModelDbList = [];

for (List<SponsorModelApi> sponsorListApi in sponsorsApi) {
for (SponsorModelApi s in sponsorListApi) {
sponsorsModelDbList.add(SponsorModelDb()
..sponsorId = s.id
..row = s.row
..column = s.column);
}
}

return sponsorsModelDbList;
}
}
Loading

0 comments on commit a4f3920

Please sign in to comment.