Skip to content

Commit

Permalink
Merge pull request #20 from kyushu-silicon-valley/home_backend
Browse files Browse the repository at this point in the history
  • Loading branch information
you22fy authored Jul 17, 2024
2 parents 8d0eb61 + 8d167f7 commit 9e9e900
Show file tree
Hide file tree
Showing 5 changed files with 240 additions and 2 deletions.
22 changes: 20 additions & 2 deletions calorie_go_flutter/lib/pages/home/home_page.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:calorie_go_flutter/components/bottom_app_bar.dart';
import 'package:calorie_go_flutter/gen/assets.gen.dart';
import 'package:calorie_go_flutter/pages/home/home_page_controller.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
Expand All @@ -9,6 +9,17 @@ class HomePage extends HookConsumerWidget {

@override
Widget build(BuildContext context, WidgetRef ref) {
final state = ref.watch(homePageControllerProvider);
final monster = state.monster;

// return monster.when(data: (data){
// return Text(data.monsterImageId.imageUrl);
// }, error: (err, stackTrace){
// return Text("error");
// }, loading: (){
// return Text("loading");
// });

return Scaffold(
appBar: AppBar(
title: const Text('ホーム'),
Expand All @@ -29,7 +40,14 @@ class HomePage extends HookConsumerWidget {
),
ClipRRect(
borderRadius: BorderRadius.circular(40.0),
child: Assets.images.sample.sampleImage.image(),
// child: Assets.images.sample.sampleImage.image(),
child: monster.when(data: (data){
return Image.network(data.monsterImageId?.imageUrl ?? '');
}, error: (err, stackTrace){
return const Text("error");
}, loading: (){
return const Text("loading");
}),
),
const SizedBox(
height: 30,
Expand Down
27 changes: 27 additions & 0 deletions calorie_go_flutter/lib/pages/home/home_page_controller.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import 'package:calorie_go_flutter/pages/home/home_page_state.dart';
import 'package:calorie_go_flutter/repositories/monster_repository.dart';
import 'package:flutter/material.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'home_page_controller.g.dart';

@riverpod
class HomePageController extends _$HomePageController {
@override
HomePageState build() {
WidgetsBinding.instance.addPostFrameCallback((_) {
getMonster();
});
return const HomePageState();
}

Future<void> getMonster() async {
try {
final monster = await MonsterRepository().getMonster();
if(monster == null) throw Exception('モンスターが見つかりませんでした');
state = state.copyWith(monster: AsyncValue.data(monster));
} catch (e) {
state = state.copyWith(errorMessage: '$e');
}
}
}
27 changes: 27 additions & 0 deletions calorie_go_flutter/lib/pages/home/home_page_controller.g.dart

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

13 changes: 13 additions & 0 deletions calorie_go_flutter/lib/pages/home/home_page_state.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'package:calorie_go_client/calorie_go_client.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';

part 'home_page_state.freezed.dart';

@freezed
class HomePageState with _$HomePageState {
const factory HomePageState({
@Default(AsyncValue<Monster>.loading()) AsyncValue<Monster> monster,
String? errorMessage,
}) = _HomePageState;
}
153 changes: 153 additions & 0 deletions calorie_go_flutter/lib/pages/home/home_page_state.freezed.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
// coverage:ignore-file
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint
// ignore_for_file: unused_element, deprecated_member_use, deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named, prefer_expression_function_bodies, annotate_overrides, invalid_annotation_target, unnecessary_question_mark

part of 'home_page_state.dart';

// **************************************************************************
// FreezedGenerator
// **************************************************************************

T _$identity<T>(T value) => value;

final _privateConstructorUsedError = UnsupportedError(
'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#adding-getters-and-methods-to-our-models');

/// @nodoc
mixin _$HomePageState {
AsyncValue<Monster> get monster => throw _privateConstructorUsedError;
String? get errorMessage => throw _privateConstructorUsedError;

@JsonKey(ignore: true)
$HomePageStateCopyWith<HomePageState> get copyWith =>
throw _privateConstructorUsedError;
}

/// @nodoc
abstract class $HomePageStateCopyWith<$Res> {
factory $HomePageStateCopyWith(
HomePageState value, $Res Function(HomePageState) then) =
_$HomePageStateCopyWithImpl<$Res, HomePageState>;
@useResult
$Res call({AsyncValue<Monster> monster, String? errorMessage});
}

/// @nodoc
class _$HomePageStateCopyWithImpl<$Res, $Val extends HomePageState>
implements $HomePageStateCopyWith<$Res> {
_$HomePageStateCopyWithImpl(this._value, this._then);

// ignore: unused_field
final $Val _value;
// ignore: unused_field
final $Res Function($Val) _then;

@pragma('vm:prefer-inline')
@override
$Res call({
Object? monster = null,
Object? errorMessage = freezed,
}) {
return _then(_value.copyWith(
monster: null == monster
? _value.monster
: monster // ignore: cast_nullable_to_non_nullable
as AsyncValue<Monster>,
errorMessage: freezed == errorMessage
? _value.errorMessage
: errorMessage // ignore: cast_nullable_to_non_nullable
as String?,
) as $Val);
}
}

/// @nodoc
abstract class _$$HomePageStateImplCopyWith<$Res>
implements $HomePageStateCopyWith<$Res> {
factory _$$HomePageStateImplCopyWith(
_$HomePageStateImpl value, $Res Function(_$HomePageStateImpl) then) =
__$$HomePageStateImplCopyWithImpl<$Res>;
@override
@useResult
$Res call({AsyncValue<Monster> monster, String? errorMessage});
}

/// @nodoc
class __$$HomePageStateImplCopyWithImpl<$Res>
extends _$HomePageStateCopyWithImpl<$Res, _$HomePageStateImpl>
implements _$$HomePageStateImplCopyWith<$Res> {
__$$HomePageStateImplCopyWithImpl(
_$HomePageStateImpl _value, $Res Function(_$HomePageStateImpl) _then)
: super(_value, _then);

@pragma('vm:prefer-inline')
@override
$Res call({
Object? monster = null,
Object? errorMessage = freezed,
}) {
return _then(_$HomePageStateImpl(
monster: null == monster
? _value.monster
: monster // ignore: cast_nullable_to_non_nullable
as AsyncValue<Monster>,
errorMessage: freezed == errorMessage
? _value.errorMessage
: errorMessage // ignore: cast_nullable_to_non_nullable
as String?,
));
}
}

/// @nodoc
class _$HomePageStateImpl implements _HomePageState {
const _$HomePageStateImpl(
{this.monster = const AsyncValue<Monster>.loading(), this.errorMessage});

@override
@JsonKey()
final AsyncValue<Monster> monster;
@override
final String? errorMessage;

@override
String toString() {
return 'HomePageState(monster: $monster, errorMessage: $errorMessage)';
}

@override
bool operator ==(Object other) {
return identical(this, other) ||
(other.runtimeType == runtimeType &&
other is _$HomePageStateImpl &&
(identical(other.monster, monster) || other.monster == monster) &&
(identical(other.errorMessage, errorMessage) ||
other.errorMessage == errorMessage));
}

@override
int get hashCode => Object.hash(runtimeType, monster, errorMessage);

@JsonKey(ignore: true)
@override
@pragma('vm:prefer-inline')
_$$HomePageStateImplCopyWith<_$HomePageStateImpl> get copyWith =>
__$$HomePageStateImplCopyWithImpl<_$HomePageStateImpl>(this, _$identity);
}

abstract class _HomePageState implements HomePageState {
const factory _HomePageState(
{final AsyncValue<Monster> monster,
final String? errorMessage}) = _$HomePageStateImpl;

@override
AsyncValue<Monster> get monster;
@override
String? get errorMessage;
@override
@JsonKey(ignore: true)
_$$HomePageStateImplCopyWith<_$HomePageStateImpl> get copyWith =>
throw _privateConstructorUsedError;
}

0 comments on commit 9e9e900

Please sign in to comment.