Skip to content

Commit

Permalink
adds HomePage
Browse files Browse the repository at this point in the history
  • Loading branch information
AyushBherwani1998 committed Aug 3, 2023
1 parent e3e7bb3 commit 1d28982
Show file tree
Hide file tree
Showing 16 changed files with 279 additions and 115 deletions.
2 changes: 1 addition & 1 deletion codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ ignore:
- '**/entities/'
- 'lib/core/error/'
- 'lib/core/usecases/'
- 'dependency_injection.dart'
- 'lib/dependency_injection.dart'
20 changes: 20 additions & 0 deletions lib/core/widgets/error_tile.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:flutter/material.dart';

class ErrorTile extends StatelessWidget {
final VoidCallback onTap;
final String message;

const ErrorTile({
super.key,
required this.onTap,
required this.message,
});

@override
Widget build(BuildContext context) {
return ListTile(
title: Text(message),
onTap: onTap,
);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:developer';

import 'package:dio/dio.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:unplash_sample/features/home/data/models/image_model.dart';
import 'package:unplash_sample/features/home/domain/entities/image.dart';

Expand All @@ -16,7 +17,15 @@ class UnsplashRemoteDataSourceImpl implements UnsplashRemoteDataSource {
@override
Future<List<UnsplashImage>> fetchImages(int pageNumber) async {
try {
final response = await dio.get('https://api.unsplash.com/photos');
final response = await dio.get(
'https://api.unsplash.com/photos',
queryParameters: {"page": 1},
options: Options(
headers: {
"Authorization": "Client-ID ${dotenv.env["UNSPLASH_API_KEY"]}"
},
),
);
if (response.statusCode == 200) {
final imageModelListModel = UnsplashImageListModel.fromJson(
List.from(response.data),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class UnsplashImageBloc extends Bloc<UnsplashImageEvent, UnsplashImageState> {
on<UnsplashImageEvent>((event, emit) async {
if (event is FetchImageEvent) {
emit(UnsplashImageLoadingState());

final imagesEither = await fetchImages(event.params);
imagesEither.fold((error) {
emit(const UnsplashImageErrorState(serverErrorMessage));
Expand Down
55 changes: 55 additions & 0 deletions lib/features/home/presentation/pages/home_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:unplash_sample/core/widgets/error_tile.dart';
import 'package:unplash_sample/dependency_injection.dart';
import 'package:unplash_sample/features/home/domain/usecases/fetch_images.dart';
import 'package:unplash_sample/features/home/presentation/bloc/unsplash_image_bloc.dart';

class HomePage extends StatefulWidget {
const HomePage({super.key});

@override
State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
late final UnsplashImageBloc bloc;

@override
void initState() {
super.initState();
bloc = DependencyInjection.getIt<UnsplashImageBloc>();
_fetchImageEvent();
}

void _fetchImageEvent() {
bloc.add(FetchImageEvent(FetchImageParams(1, 30)));
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Unsplash Demo"),
),
body: BlocBuilder<UnsplashImageBloc, UnsplashImageState>(
bloc: bloc,
builder: (context, state) {
if (state is UnsplashImageErrorState) {
return ErrorTile(
onTap: () {
_fetchImageEvent();
},
message: state.errorMessage,
);
} else if (state is UnsplashImageLoadedState) {
return Container();
}
return const Center(
child: CircularProgressIndicator.adaptive(),
);
},
),
);
}
}
112 changes: 6 additions & 106 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,127 +1,27 @@
import 'package:flutter/material.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:unplash_sample/dependency_injection.dart';
import 'package:unplash_sample/features/home/presentation/pages/home_page.dart';

void main() {
void main() async {
WidgetsFlutterBinding.ensureInitialized();
DependencyInjection.initialize();
await dotenv.load(fileName: "./lib/core/.env");
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
const MyApp({super.key});

// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// TRY THIS: Try running your application with "flutter run". You'll see
// the application has a blue toolbar. Then, without quitting the app,
// try changing the seedColor in the colorScheme below to Colors.green
// and then invoke "hot reload" (save your changes or press the "hot
// reload" button in a Flutter-supported IDE, or press "r" if you used
// the command line to start the app).
//
// Notice that the counter didn't reset back to zero; the application
// state is not lost during the reload. To reset the state, use hot
// restart instead.
//
// This works for code too, not just values: Most code changes can be
// tested with just a hot reload.
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});

// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.

// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".

final String title;

@override
State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;

void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}

@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// TRY THIS: Try changing the color here to a specific color (to
// Colors.amber, perhaps?) and trigger a hot reload to see the AppBar
// change color while the other colors stay the same.
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
//
// TRY THIS: Invoke "debug painting" (choose the "Toggle Debug Paint"
// action in the IDE, or press "p" in the console), to see the
// wireframe for each widget.
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
home: const HomePage(),
);
}
}
2 changes: 1 addition & 1 deletion macos/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0920;
LastUpgradeCheck = 1430;
LastUpgradeCheck = 1300;
ORGANIZATIONNAME = "";
TargetAttributes = {
331C80D4294CF70F00263BE5 = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1430"
LastUpgradeVersion = "1300"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
2 changes: 2 additions & 0 deletions macos/Runner/DebugProfile.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@
<true/>
<key>com.apple.security.network.server</key>
<true/>
<key>com.apple.security.network.client</key>
<true/>
</dict>
</plist>
24 changes: 24 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_bloc:
dependency: "direct main"
description:
name: flutter_bloc
sha256: e74efb89ee6945bcbce74a5b3a5a3376b088e5f21f55c263fc38cbdc6237faae
url: "https://pub.dev"
source: hosted
version: "8.1.3"
flutter_dotenv:
dependency: "direct main"
description:
Expand Down Expand Up @@ -296,6 +304,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.3.0"
nested:
dependency: transitive
description:
name: nested
sha256: "03bac4c528c64c95c722ec99280375a6f2fc708eec17c7b3f07253b626cd2a20"
url: "https://pub.dev"
source: hosted
version: "1.0.0"
node_preamble:
dependency: transitive
description:
Expand Down Expand Up @@ -328,6 +344,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.5.1"
provider:
dependency: transitive
description:
name: provider
sha256: cdbe7530b12ecd9eb455bdaa2fcb8d4dad22e80b8afb4798b41479d5ce26847f
url: "https://pub.dev"
source: hosted
version: "6.0.5"
pub_semver:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ dependencies:
dartz: ^0.10.1
dio: ^5.3.0
equatable: ^2.0.5
flutter_bloc: ^8.1.3
flutter_dotenv: ^5.1.0
get_it: ^7.6.0
go_router: ^10.0.0
Expand Down
6 changes: 6 additions & 0 deletions test/core/mocks/unsplash_image_bloc_mock.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import 'package:mocktail/mocktail.dart';
import 'package:unplash_sample/features/home/presentation/bloc/unsplash_image_bloc.dart';

class UnsplashImageBlocMock extends Mock implements UnsplashImageBloc {}

class UnsplashImageFakeState extends Fake implements UnsplashImageState {}
40 changes: 40 additions & 0 deletions test/core/widgets/error_tile_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:unplash_sample/core/widgets/error_tile.dart';

void main() {
const text = "test";
final List<String> logs = [];

Widget pumpErrorTileWidget() {
return MaterialApp(
home: Scaffold(
body: ErrorTile(
onTap: () {
logs.add(text);
},
message: text,
)),
);
}

group("ErrorTile tests", () {
testWidgets("text is displayed on screen", (tester) async {
await tester.pumpWidget(pumpErrorTileWidget());

final errorText = find.text(text);
expect(errorText, findsOneWidget);
});

testWidgets("onTap is called when tile is pressed", (tester) async {
await tester.pumpWidget(pumpErrorTileWidget());

expect(logs.isEmpty, isTrue);

await tester.tap(find.byType(ErrorTile));
await tester.pumpAndSettle();

expect(logs.isEmpty, isFalse);
});
});
}
Loading

0 comments on commit 1d28982

Please sign in to comment.