load a fake list of destinations

pull/2342/head
Miguel Beltran 3 months ago
parent e978f4dcc0
commit 16fa89a9b2

File diff suppressed because it is too large Load Diff

@ -1,5 +1,6 @@
import 'package:compass_app/features/results/business/usecases/search_destination_usecase.dart';
import 'package:compass_app/features/results/data/destination_repository.dart';
import 'package:compass_app/features/results/data/destination_repository_local.dart';
import 'package:compass_app/features/results/presentation/results_viewmodel.dart';
import 'package:provider/provider.dart';
import 'package:provider/single_child_widget.dart';
@ -7,7 +8,8 @@ import 'package:provider/single_child_widget.dart';
// Configure dependencies
List<SingleChildWidget> get providers {
// These dependencies don't need to be in the widget tree (yet?)
final destinationRepository = DestinationRepository();
final destinationRepository = DestinationRepositoryLocal();
// Configure usecase to use the local data repository implementation
final searchDestinationUsecase = SearchDestinationUsecase(
repository: destinationRepository,
);

@ -34,4 +34,16 @@ class Destination {
String toString() {
return 'Destination{ref: $ref, name: $name, country: $country, continent: $continent, knownFor: $knownFor, tags: $tags, imageUrl: $imageUrl}';
}
factory Destination.fromJson(Map<String, dynamic> json) {
return Destination(
ref: json['ref'] as String,
name: json['name'] as String,
country: json['country'] as String,
continent: json['continent'] as String,
knownFor: json['knownFor'] as String,
tags: (json['tags'] as List<dynamic>).map((e) => e as String).toList(),
imageUrl: json['imageUrl'] as String,
);
}
}

@ -3,14 +3,7 @@ import 'package:compass_app/features/results/business/model/destination.dart';
import 'package:flutter/material.dart';
/// Data source with all possible destinations
class DestinationRepository {
abstract class DestinationRepository {
/// Get complete list of destinations
Future<Result<List<Destination>>> getDestinations() {
// TODO: Load some data
return Future.delayed(
// Simulate loading from network
const Duration(milliseconds: 1000),
() => Result.ok([]),
);
}
Future<Result<List<Destination>>> getDestinations();
}

@ -0,0 +1,34 @@
import 'dart:convert';
import 'package:compass_app/common/utils/result.dart';
import 'package:compass_app/features/results/business/model/destination.dart';
import 'package:compass_app/features/results/data/destination_repository.dart';
import 'package:flutter/services.dart' show rootBundle;
/// Local implementation of DestinationRepository
/// Uses data from assets folder
class DestinationRepositoryLocal implements DestinationRepository {
@override
Future<Result<List<Destination>>> getDestinations() async {
try {
final localData = await _loadAsset();
final list = _parse(localData);
return Result.ok(list);
} on Exception catch (error) {
return Result.error(error);
}
}
Future<String> _loadAsset() async {
return await rootBundle.loadString('assets/destinations.json');
}
List<Destination> _parse(String localData) {
final parsed = (jsonDecode(localData) as List).cast<Map<String, dynamic>>();
return parsed
.map<Destination>((json) => Destination.fromJson(json))
.toList();
}
}

@ -0,0 +1,10 @@
import 'package:flutter/material.dart';
class ResultCard extends StatelessWidget {
const ResultCard({super.key});
@override
Widget build(BuildContext context) {
return const Placeholder();
}
}

@ -19,3 +19,5 @@ dev_dependencies:
flutter:
uses-material-design: true
assets:
- assets/destinations.json

@ -0,0 +1,27 @@
import 'package:compass_app/common/utils/result.dart';
import 'package:compass_app/features/results/data/destination_repository_local.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
group('DestinationRepositoryLocal tests', () {
// To load assets
TestWidgetsFlutterBinding.ensureInitialized();
final repository = DestinationRepositoryLocal();
test('should load and parse', () async {
// Should load the json and parse it
final result = await repository.getDestinations();
expect(result, isA<Ok>());
// Check that the list is complete
final list = result.asOk.value;
expect(list.length, 137);
// Check first item
final destination = list.first;
expect(destination.name, 'Alaska');
});
});
}
Loading…
Cancel
Save