Skip to content

Commit

Permalink
feat: show thumbs of videos
Browse files Browse the repository at this point in the history
  • Loading branch information
Ascenio committed May 8, 2024
1 parent 791698c commit f132c3d
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 33 deletions.
9 changes: 9 additions & 0 deletions lib/core/date_formatters/date_formatter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'package:intl/intl.dart';

final class DateFormatter {
const DateFormatter._();

static final _yyyyMMddFormat = DateFormat('yyyy-MM-dd');

static String yyyyMMdd(DateTime date) => _yyyyMMddFormat.format(date);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:flutter/foundation.dart';
import 'package:http/http.dart';
import 'package:nasa_potday/features/picture_of_the_day/data/datasources/remote_nasa_datasource.dart';
import 'package:nasa_potday/features/picture_of_the_day/data/models/load_picture_of_the_day_request_model.dart';
import 'package:nasa_potday/features/picture_of_the_day/data/models/picture_model.dart';
import 'package:nasa_potday/features/picture_of_the_day/data/models/remote_picture_model.dart';
import 'package:nasa_potday/features/picture_of_the_day/domain/entities/pictures_page_entity.dart';

final class HttpRemoteNasaDataSource implements RemoteNasaDataSource {
Expand Down Expand Up @@ -42,7 +42,7 @@ final class HttpRemoteNasaDataSource implements RemoteNasaDataSource {
);
}

Future<List<PictureModel>> _request(
Future<List<RemotePictureModel>> _request(
LoadPictureOfTheDayRequestModel request,
) async {
final uri = Uri.parse(baseUrl).replace(
Expand All @@ -51,7 +51,7 @@ final class HttpRemoteNasaDataSource implements RemoteNasaDataSource {
final response = await get(uri);
return (jsonDecode(response.body) as List)
.cast<Map<String, dynamic>>()
.map(PictureModel.fromJson)
.map(RemotePictureModel.fromJson)
.toList()
.reversed
.toList();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:nasa_potday/core/date_formatters/date_formatter.dart';
import 'package:nasa_potday/features/picture_of_the_day/data/datasources/local_nasa_datasource.dart';
import 'package:nasa_potday/features/picture_of_the_day/data/models/picture_model.dart';
import 'package:nasa_potday/features/picture_of_the_day/data/models/local_picture_model.dart';
import 'package:nasa_potday/features/picture_of_the_day/domain/entities/picture_entity.dart';
import 'package:nasa_potday/features/picture_of_the_day/domain/entities/pictures_page_entity.dart';
import 'package:sqflite/sqflite.dart';
Expand All @@ -22,16 +22,15 @@ CREATE TABLE $_picturesTable (
'date' TEXT PRIMARY KEY,
url TEXT,
title TEXT,
explanation TEXT
explanation TEXT,
is_video INTEGER
)
''');
},
);
return database;
}

String _dateString(DateTime date) => DateFormat('yyyy-MM-dd').format(date);

@override
Future<void> save(PicturesPageEntity page) async {
try {
Expand All @@ -40,12 +39,7 @@ CREATE TABLE $_picturesTable (
for (final picture in page.pictures) {
database.insert(
_picturesTable,
{
'date': _dateString(picture.date),
'url': picture.url.toString(),
'title': picture.title,
'explanation': picture.explanation,
},
LocalPictureModel.fromEntity(picture).toJson(),
conflictAlgorithm: ConflictAlgorithm.replace,
);
}
Expand All @@ -67,13 +61,13 @@ CREATE TABLE $_picturesTable (
where: 'DATE(date) BETWEEN ? AND ?',
orderBy: 'DATE(date) DESC',
whereArgs: [
_dateString(startDate),
_dateString(endDate),
DateFormatter.yyyyMMdd(startDate),
DateFormatter.yyyyMMdd(endDate),
],
);
return PicturesPageEntity(
startDate: startDate,
pictures: pictures.map(PictureModel.fromJson).toList(),
pictures: pictures.map(LocalPictureModel.fromJson).toList(),
);
} on DatabaseException catch (error) {
debugPrint('Could not load page $startDate: $error');
Expand All @@ -96,7 +90,7 @@ CREATE TABLE $_picturesTable (
whereArgs: ['%$title%'],
orderBy: 'DATE(date) DESC',
);
return result.map(PictureModel.fromJson).toList();
return result.map(LocalPictureModel.fromJson).toList();
} on DatabaseException catch (error) {
debugPrint('Could not search for "$title": $error');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class LoadPictureOfTheDayRequestModel {
'api_key': apiKey,
'start_date': _dateToString(startDate),
'end_date': _dateToString(endDate),
'thumbs': 'true',
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import 'package:nasa_potday/core/date_formatters/date_formatter.dart';
import 'package:nasa_potday/features/picture_of_the_day/domain/entities/picture_entity.dart';

class LocalPictureModel extends PictureEntity {
const LocalPictureModel({
required super.url,
required super.title,
required super.explanation,
required super.date,
required super.isVideo,
});

factory LocalPictureModel.fromEntity(PictureEntity entity) {
return LocalPictureModel(
url: entity.url,
title: entity.title,
explanation: entity.explanation,
date: entity.date,
isVideo: entity.isVideo,
);
}

Map<String, dynamic> toJson() {
return {
'date': DateFormatter.yyyyMMdd(date),
'url': url.toString(),
'title': title,
'explanation': explanation,
'is_video': isVideo ? 1 : 0,
};
}

factory LocalPictureModel.fromJson(Map<String, dynamic> json) {
final isVideo = json['media_type'] == 'video';

return LocalPictureModel(
url: Uri.parse(isVideo ? json['thumbnail_url'] : json['url']),
title: json['title'],
explanation: json['explanation'],
date: DateTime.parse(json['date']),
isVideo: json['is_video'] == 1,
);
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import 'package:nasa_potday/features/picture_of_the_day/domain/entities/picture_entity.dart';

class PictureModel extends PictureEntity {
const PictureModel({
class RemotePictureModel extends PictureEntity {
const RemotePictureModel({
required super.url,
required super.title,
required super.explanation,
required super.date,
required super.isVideo,
});

factory PictureModel.fromJson(Map<String, dynamic> json) {
final url = Uri.parse(json['url']);
factory RemotePictureModel.fromJson(Map<String, dynamic> json) {
final isVideo = json['media_type'] == 'video';

return PictureModel(
url: url,
return RemotePictureModel(
url: Uri.parse(isVideo ? json['thumbnail_url'] : json['url']),
title: json['title'],
explanation: json['explanation'],
date: DateTime.parse(json['date']),
isVideo: json['media_type'] == 'video',
isVideo: isVideo,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,9 @@ class ImageOrVideoWidget extends StatelessWidget {
tag: url,
child: SizedBox(
height: size.height / 3,
child: switch (isVideo) {
true => Container(
width: double.infinity,
color: surfaceColor,
child: Image.asset('assets/youtube-logo.png'),
),
false => CachedNetworkImage(
child: Stack(
children: [
CachedNetworkImage(
imageUrl: url.toString(),
width: double.infinity,
height: size.height / 3,
Expand Down Expand Up @@ -73,7 +69,9 @@ class ImageOrVideoWidget extends StatelessWidget {
);
},
),
},
if (isVideo) Center(child: Image.asset('assets/youtube-logo.png')),
],
),
),
);
}
Expand Down

0 comments on commit f132c3d

Please sign in to comment.