Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

持ち物チェックcontroller,interactorを実装 #119

Merged
merged 3 commits into from
May 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions lib/features/trips/controller/trip_belonging_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,23 @@ class TripBelongingsController extends _$TripBelongingsController {
onFinished?.call();
}
}

Future<void> changeCheckStatus(AddedTripBelonging belonging) async {
final result = await ref
.read(tripInteractorProvider)
.changeBelongingCheckStatus(belonging: belonging);

_changeSome(result);
}

void _changeSome(AddedTripBelonging newBelonging) {
state = AsyncValue.data(
state.value?.map(
(belonging) {
return belonging.id == newBelonging.id ? newBelonging : belonging;
},
).toList() ??
[],
);
}
}
2 changes: 1 addition & 1 deletion lib/features/trips/domain/entity/trip/trip_belonging.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:trip_app_nativeapp/features/trips/domain/entity/trip/value/trip_

part 'trip_belonging.freezed.dart';

@Freezed(copyWith: false, fromJson: false, toJson: false)
@Freezed(fromJson: false, toJson: false)
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[memo]
フィールド書き換えはさすがに必要なんでentityはcopyWith開放した!

class TripBelonging with _$TripBelonging {
/// 新規追加時持ち物のエンティティのfactory関数
factory TripBelonging.createNewTripBelonging({
Expand Down
175 changes: 175 additions & 0 deletions lib/features/trips/domain/entity/trip/trip_belonging.freezed.dart

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

12 changes: 11 additions & 1 deletion lib/features/trips/domain/interactor/trip_interactor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class TripInteractor {
required bool isShareAmongMember,
}) {
final belonging = TripBelonging.createNewTripBelonging(
name: TripBelongingName(value: name),
name: TripBelongingName(value: name),
numOf: TripBelongingNum(value: numOf),
isShareAmongMember: isShareAmongMember,
) as NewTripBelonging;
Expand All @@ -75,4 +75,14 @@ class TripInteractor {

Future<List<AddedTripBelonging>> fetchTripBelongings(int tripId) =>
tripRepo.fetchTripBelongings(tripId);

Future<AddedTripBelonging> changeBelongingCheckStatus({
required AddedTripBelonging belonging,
}) async {
final result = await tripRepo.changeBelongingCheckStatus(
belongingId: belonging.id,
isChecked: !belonging.isChecked,
);
return belonging.copyWith(isChecked: result);
}
}
61 changes: 45 additions & 16 deletions test/feature/trips/controller/trip_belonging_controller_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,21 @@ Future<void> main() async {
final dioAdapter = DioAdapter(dio: dio);

const testTripId = 1;
final testFetchBelongings = [
AddedTripBelonging(
id: 1,
name: TripBelongingName(value: '持ち物1'),
numOf: TripBelongingNum(value: 5),
isShareAmongMember: true,
isChecked: false,
),
AddedTripBelonging(
id: 2,
name: TripBelongingName(value: '持ち物2'),
numOf: TripBelongingNum(value: 10),
isShareAmongMember: true,
isChecked: true,
),
];
final testBelonging1 = AddedTripBelonging(
id: 1,
name: TripBelongingName(value: '持ち物1'),
numOf: TripBelongingNum(value: 5),
isShareAmongMember: true,
isChecked: false,
);
final testBelonging2 = AddedTripBelonging(
id: 2,
name: TripBelongingName(value: '持ち物2'),
numOf: TripBelongingNum(value: 10),
isShareAmongMember: true,
isChecked: true,
);
final testFetchBelongings = [testBelonging1, testBelonging2];

final testAddBelonging = AddedTripBelonging(
id: 3,
Expand Down Expand Up @@ -134,4 +133,34 @@ Future<void> main() async {
expect(result, [testAddBelonging, ...testFetchBelongings]);
});
});

group('changeCheckStatus', () {
test('正常系', () async {
dioAdapter.onPut(
'/trip_belongings/${testBelonging1.id}/check_status',
data: {'is_checked': true},
(server) => server.reply(
201,
<String, dynamic>{
'data': {
'is_checked': true,
},
},
),
);
await providerContainer
.read(tripBelongingsControllerProvider(tripId: testTripId).notifier)
.changeCheckStatus(testBelonging1);
final result = await providerContainer.read(
tripBelongingsControllerProvider(tripId: testTripId).future,
);
expect(
result,
[
testBelonging1.copyWith(isChecked: true),
testBelonging2,
],
);
});
});
}
19 changes: 19 additions & 0 deletions test/feature/trips/controller/trip_controller_test.mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,23 @@ class MockTripInteractor extends _i1.Mock implements _i5.TripInteractor {
returnValue: _i6.Future<List<_i4.AddedTripBelonging>>.value(
<_i4.AddedTripBelonging>[]),
) as _i6.Future<List<_i4.AddedTripBelonging>>);
@override
_i6.Future<_i4.AddedTripBelonging> changeBelongingCheckStatus(
{required _i4.AddedTripBelonging? belonging}) =>
(super.noSuchMethod(
Invocation.method(
#changeBelongingCheckStatus,
[],
{#belonging: belonging},
),
returnValue:
_i6.Future<_i4.AddedTripBelonging>.value(_FakeAddedTripBelonging_2(
this,
Invocation.method(
#changeBelongingCheckStatus,
[],
{#belonging: belonging},
),
)),
) as _i6.Future<_i4.AddedTripBelonging>);
}
Loading