Skip to content

Commit

Permalink
Merge pull request #190 from kosukesaigusa/entire_refactor
Browse files Browse the repository at this point in the history
Entire refactor
  • Loading branch information
kosukesaigusa committed Sep 21, 2023
2 parents a92615d + f1269ee commit 6564965
Show file tree
Hide file tree
Showing 30 changed files with 975 additions and 1,045 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@ import 'package:json_annotation/json_annotation.dart';

part 'host_location.flutterfire_gen.dart';

// NOT: ドキュメント ID は hostId と一致する。
@FirestoreDocument(path: 'hostLocations', documentName: 'hostLocation')
class HostLocation {
const HostLocation({
required this.hostId,
required this.address,
required this.geo,
this.createdAt,
this.updatedAt,
});

final String hostId;

@ReadDefault('')
final String address;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ class ReadHostLocation {
const ReadHostLocation({
required this.hostLocationId,
required this.path,
required this.hostId,
required this.address,
required this.geo,
required this.createdAt,
Expand All @@ -19,8 +18,6 @@ class ReadHostLocation {

final String path;

final String hostId;

final String address;

final Geo geo;
Expand All @@ -33,7 +30,6 @@ class ReadHostLocation {
return ReadHostLocation(
hostLocationId: json['hostLocationId'] as String,
path: json['path'] as String,
hostId: json['hostId'] as String,
address: json['address'] as String? ?? '',
geo: _geoConverter.fromJson(json['geo'] as Map<String, dynamic>),
createdAt: (json['createdAt'] as Timestamp?)?.toDate(),
Expand All @@ -53,18 +49,15 @@ class ReadHostLocation {

class CreateHostLocation {
const CreateHostLocation({
required this.hostId,
required this.address,
required this.geo,
});

final String hostId;
final String address;
final Geo geo;

Map<String, dynamic> toJson() {
return {
'hostId': hostId,
'address': address,
'geo': _geoConverter.toJson(geo),
'createdAt': FieldValue.serverTimestamp(),
Expand All @@ -75,20 +68,17 @@ class CreateHostLocation {

class UpdateHostLocation {
const UpdateHostLocation({
this.hostId,
this.address,
this.geo,
this.createdAt,
});

final String? hostId;
final String? address;
final Geo? geo;
final DateTime? createdAt;

Map<String, dynamic> toJson() {
return {
if (hostId != null) 'hostId': hostId,
if (address != null) 'address': address,
if (geo != null) 'geo': _geoConverter.toJson(geo!),
if (createdAt != null) 'createdAt': createdAt,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Worker {
const Worker({
required this.displayName,
required this.imageUrl,
required this.introduction,
required this.isHost,
this.createdAt,
this.updatedAt,
Expand All @@ -20,6 +21,10 @@ class Worker {
@CreateDefault('')
final String imageUrl;

@ReadDefault('')
@CreateDefault('')
final String introduction;

@ReadDefault(false)
final bool isHost;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class ReadWorker {
required this.path,
required this.displayName,
required this.imageUrl,
required this.introduction,
required this.isHost,
required this.createdAt,
required this.updatedAt,
Expand All @@ -23,6 +24,8 @@ class ReadWorker {

final String imageUrl;

final String introduction;

final bool isHost;

final DateTime? createdAt;
Expand All @@ -35,6 +38,7 @@ class ReadWorker {
path: json['path'] as String,
displayName: json['displayName'] as String? ?? '',
imageUrl: json['imageUrl'] as String? ?? '',
introduction: json['introduction'] as String? ?? '',
isHost: json['isHost'] as bool? ?? false,
createdAt: (json['createdAt'] as Timestamp?)?.toDate(),
updatedAt: (json['updatedAt'] as Timestamp?)?.toDate(),
Expand All @@ -55,17 +59,20 @@ class CreateWorker {
const CreateWorker({
required this.displayName,
this.imageUrl = '',
this.introduction = '',
required this.isHost,
});

final String displayName;
final String imageUrl;
final String introduction;
final bool isHost;

Map<String, dynamic> toJson() {
return {
'displayName': displayName,
'imageUrl': imageUrl,
'introduction': introduction,
'isHost': isHost,
'createdAt': FieldValue.serverTimestamp(),
'updatedAt': FieldValue.serverTimestamp(),
Expand All @@ -77,19 +84,22 @@ class UpdateWorker {
const UpdateWorker({
this.displayName,
this.imageUrl,
this.introduction,
this.isHost,
this.createdAt,
});

final String? displayName;
final String? imageUrl;
final String? introduction;
final bool? isHost;
final DateTime? createdAt;

Map<String, dynamic> toJson() {
return {
if (displayName != null) 'displayName': displayName,
if (imageUrl != null) 'imageUrl': imageUrl,
if (introduction != null) 'introduction': introduction,
if (isHost != null) 'isHost': isHost,
if (createdAt != null) 'createdAt': createdAt,
'updatedAt': FieldValue.serverTimestamp(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,29 @@
// import '../firestore_documents/host_location.dart';

import '../../firebase_common.dart';
import '../firestore_documents/host_location.dart';

class HostLocationRepository {
final _query = HostLocationQuery();

/// 指定した [HostLocation] を取得する。
/// 指定した [hostLocationId][HostLocation] を取得する。
Future<ReadHostLocation?> fetchHostLocation({
required String hostLocationId,
}) =>
_query.fetchDocument(hostLocationId: hostLocationId);

/// [Host]から関連する[HostLocation]をすべて取得する
Future<List<ReadHostLocation>> fetchHostLocationsFromHost({
required String hostId,
/// 指定した [hostLocationId][HostLocation] を購読する
Stream<ReadHostLocation?> subscribeHostLocation({
required String hostLocationId,
}) =>
_query.fetchDocuments(
queryBuilder: (query) => query.where('hostId', isEqualTo: hostId),
);
_query.subscribeDocument(hostLocationId: hostLocationId);

/// [HostLocation] の情報を作成する。
Future<void> create({
required String hostLocationId,
required String hostId,
required String address,
required Geo geo,
}) =>
_query.set(
hostLocationId: hostLocationId,
createHostLocation: CreateHostLocation(
hostId: hostId,
address: address,
geo: geo,
),
hostLocationId: hostId,
createHostLocation: CreateHostLocation(address: address, geo: geo),
);

/// [HostLocation] の情報を更新する。
Expand All @@ -44,9 +35,6 @@ class HostLocationRepository {
}) =>
_query.update(
hostLocationId: hostLocationId,
updateHostLocation: UpdateHostLocation(
address: address,
geo: geo,
),
updateHostLocation: UpdateHostLocation(address: address, geo: geo),
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import 'package:cloud_firestore/cloud_firestore.dart';
import '../firestore_documents/_export.dart';

class ReviewRepository {
final _query = ReviewQuery();

/// [fetchReviewsWithIdCursor] メソッドにおいて、最後に読み込んだ
/// [QueryDocumentSnapshot] を保持するための [Map] 型の変数。
/// キーはそのドキュメント ID で、バリューが [QueryDocumentSnapshot].
Expand Down Expand Up @@ -43,4 +45,13 @@ class ReviewRepository {
_updateLastReadQueryDocumentSnapshotCache(lastFetchedQds);
return reviews;
}

/// 指定した [workerId] の投稿した [Review] 一覧を購読する。
Stream<List<ReadReview>> subscribeUserReviews({required String workerId}) {
return _query.subscribeDocuments(
queryBuilder: (query) => query
.where('workerId', isEqualTo: workerId)
.orderBy('createdAt', descending: true),
);
}
}
8 changes: 4 additions & 4 deletions packages/mottai_flutter_app/lib/auth/auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ class AuthService {

// サインインダイアログでキャンセルが選択された場合には、AppException をスローし、キャンセルされたことを通知する
if (googleUser == null) {
throw const AppException(message: 'キャンセルされました');
throw const AppException(message: 'キャンセルされました');
}

final googleAuth = await googleUser.authentication; // アカウントからトークン生成
Expand All @@ -298,7 +298,7 @@ class AuthService {
if (e.code == 'network_error') {
throw const AppException(message: '接続できませんでした。\nネットワーク状況を確認してください。');
}
throw const AppException(message: 'Google認証に失敗しました');
throw const AppException(message: 'Google 認証に失敗しました。');
}
}

Expand Down Expand Up @@ -328,7 +328,7 @@ class AuthService {
if (e.code == AuthorizationErrorCode.canceled) {
throw const AppException(message: 'キャンセルされました');
}
throw const AppException(message: 'Apple認証に失敗しました');
throw const AppException(message: 'Apple 認証に失敗しました。');
}
}

Expand All @@ -348,7 +348,7 @@ class AuthService {
} on PlatformException catch (e) {
// サインインダイアログでユーザーによりキャンセルが選択された場合には、AppException をスローし、キャンセルされたことを通知する
if (e.message == 'User cancelled or interrupted the login process.') {
throw const AppException(message: 'キャンセルされました');
throw const AppException(message: 'キャンセルされました');
}
rethrow;
}
Expand Down
11 changes: 11 additions & 0 deletions packages/mottai_flutter_app/lib/auth/ui/auth_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:hooks_riverpod/hooks_riverpod.dart';

import '../../../scaffold_messenger_controller.dart';
import '../../exception.dart';
import '../../loading/ui/loading.dart';
import '../../user/host.dart';
import '../../user/user_mode.dart';
import '../../user_fcm_token/user_fcm_token.dart';
Expand All @@ -15,6 +16,8 @@ final authControllerProvider = Provider.autoDispose<AuthController>(
hostService: ref.watch(hostServiceProvider),
fcmTokenService: ref.watch(fcmTokenServiceProvider),
userModeStateController: ref.watch(userModeStateProvider.notifier),
overlayLoadingStateController:
ref.watch(overlayLoadingStateProvider.notifier),
appScaffoldMessengerController:
ref.watch(appScaffoldMessengerControllerProvider),
),
Expand All @@ -26,11 +29,13 @@ class AuthController {
required HostService hostService,
required FcmTokenService fcmTokenService,
required StateController<UserMode> userModeStateController,
required StateController<bool> overlayLoadingStateController,
required AppScaffoldMessengerController appScaffoldMessengerController,
}) : _authService = authService,
_hostService = hostService,
_fcmTokenService = fcmTokenService,
_userModeStateController = userModeStateController,
_overlayLoadingStateController = overlayLoadingStateController,
_appScaffoldMessengerController = appScaffoldMessengerController;

final AuthService _authService;
Expand All @@ -41,16 +46,20 @@ class AuthController {

final StateController<UserMode> _userModeStateController;

final StateController<bool> _overlayLoadingStateController;

final AppScaffoldMessengerController _appScaffoldMessengerController;

/// 選択した [SignInMethod] でサインインする。
/// サインイン後、必要性を確認して [UserMode]`UserMode.Host` にする。
/// サインインに成功した際は、[UserFcmToken] を登録する。
Future<void> signIn(SignInMethod signInMethod) async {
_overlayLoadingStateController.update((state) => true);
try {
final userCredential = await _signIn(signInMethod);
await _maybeSetUserModeToHost(userCredential);
await _setFcmToken(userCredential);
_appScaffoldMessengerController.showSnackBar('サインインしました');
} on AppException catch (e) {
_appScaffoldMessengerController.showSnackBarByException(e);
} on FirebaseAuthException catch (e) {
Expand All @@ -59,6 +68,8 @@ class AuthController {
} else {
_appScaffoldMessengerController.showSnackBarByFirebaseException(e);
}
} finally {
_overlayLoadingStateController.update((state) => false);
}
}

Expand Down
Loading

0 comments on commit 6564965

Please sign in to comment.