Skip to content

Commit

Permalink
feat: refactor codes
Browse files Browse the repository at this point in the history
  • Loading branch information
kosukesaigusa committed Aug 26, 2023
1 parent 44e4828 commit 8d1c8be
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 180 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import '../auth.dart';

/// Firebase に Auth にサインイン済みの場合にのみ [onAuthenticated] で渡した
/// ウィジェットを表示する。
/// その最、サインイン済みのユーザーの `userId` が使用できる。
/// その際、サインイン済みのユーザーの `userId` が使用できる。
class AuthDependentBuilder extends ConsumerWidget {
const AuthDependentBuilder({
super.key,
required this.onAuthenticated,
this.onUnAuthenticated,
});

/// サインイン済みの場合に表示されるウィジェットを `userId` とともに返す
/// ビルダー関数
/// Firebase に Auth にサインイン済みの場合に表示されるウィジェットを `userId` とともに
/// 返すビルダー関数
final Widget Function(String userId) onAuthenticated;

/// サインインアウトの場合に表示されるウィジェットを返すビルダー関数(任意)。
/// Firebase Auth にサインインしていない場合に表示されるウィジェットを返すビルダー関数(任意)。
/// 渡さなければ共通の [_SignedOut] ウィジェットが表示される。
final Widget Function()? onUnAuthenticated;

Expand All @@ -43,3 +43,39 @@ class _SignedOut extends StatelessWidget {
return const Center(child: Text('ログインしてください。'));
}
}

/// Firebase Auth にサインイン済みであり、そのユーザーが指定した [userId] に一致する場合
/// にのみ [onUserAuthenticated] で渡したウィジェットを表示する。
/// その際、サインイン済みのユーザーの [userId] が使用できる。
class UserAuthDependentBuilder extends ConsumerWidget {
const UserAuthDependentBuilder({
super.key,
required this.userId,
required this.onUserAuthenticated,
this.onUserUnAuthenticated,
});

/// Firebase Auth にサインイン済みであり、そのユーザーが指定した [userId] に一致する場合
/// に表示されるウィジェットを [userId] とともに返すビルダー関数。
final Widget Function(String userId) onUserAuthenticated;

/// Firebase Auth にサインインしていない、またはサインイン済みでもそのユーザーが指定した
/// [userId] に一致しない場合表示されるウィジェットを返すビルダー関数(任意)。
final Widget Function()? onUserUnAuthenticated;

/// 表示するユーザーの uid.
final String userId;

@override
Widget build(BuildContext context, WidgetRef ref) {
final userId = ref.watch(userIdProvider);
if (userId == null || userId != this.userId) {
if (onUserUnAuthenticated != null) {
return onUserUnAuthenticated!();
} else {
return const SizedBox();
}
}
return onUserAuthenticated(userId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:auto_route/auto_route.dart';
import 'package:dart_flutter_common/dart_flutter_common.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:gap/gap.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';

import '../../../scaffold_messenger_controller.dart';
Expand Down Expand Up @@ -42,7 +43,7 @@ class _ImagePickerSamplePageState extends ConsumerState<ImagePickerSamplePage> {
),
body: ListView(
children: [
const SizedBox(height: 60),
const Gap(60),
const Center(child: Text('1 枚の画像を選択')),
if (_pickedImageFromGallery == null)
GestureDetector(
Expand All @@ -67,7 +68,7 @@ class _ImagePickerSamplePageState extends ConsumerState<ImagePickerSamplePage> {
child: Image.file(_pickedImageFromGallery!),
),
),
const SizedBox(height: 60),
const Gap(60),
const Center(child: Text('1 枚の画像を撮影して選択')),
if (_pickedImageFromCamera == null)
GestureDetector(
Expand All @@ -92,7 +93,7 @@ class _ImagePickerSamplePageState extends ConsumerState<ImagePickerSamplePage> {
child: Image.file(_pickedImageFromCamera!),
),
),
const SizedBox(height: 60),
const Gap(60),
const Center(child: Text('複数の画像を選択')),
if (_pickedImagesFromGallery.isEmpty)
GestureDetector(
Expand Down
102 changes: 27 additions & 75 deletions packages/mottai_flutter_app/lib/host/ui/host.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:gap/gap.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';

import '../../auth/ui/auth_dependent_builder.dart';
import '../../job/job.dart';
import '../../user/host.dart';
import '../../user/ui/identity_dependent_builder.dart';
import '../../user/ui/user_mode.dart';
import '../../user/user_mode.dart';
import 'create_or_update_host.dart';
Expand Down Expand Up @@ -74,8 +74,8 @@ class HostPageBody extends ConsumerWidget {
overflow: TextOverflow.ellipsis,
),
),
IdentityDependentBuilder(
buildForIdentity: () {
UserAuthDependentBuilder(
onUserAuthenticated: (userId) {
return CircleAvatar(
backgroundColor: Theme.of(context).focusColor,
child: IconButton(
Expand All @@ -90,22 +90,22 @@ class HostPageBody extends ConsumerWidget {
),
);
},
targetUserId: userId,
userId: userId,
),
],
),
IdentityDependentBuilder(
buildForIdentity: () {
return const Column(
UserAuthDependentBuilder(
userId: userId,
onUserAuthenticated: (_) {
return Column(
children: [
Gap(16),
UserModeSection(),
const Gap(16),
UserModeSection(userId: userId),
],
);
},
targetUserId: userId,
),
const SizedBox(height: 24),
const Gap(24),
// TODO 自己紹介をDBに追加する
Section(
title: '自己紹介',
Expand All @@ -116,7 +116,7 @@ class HostPageBody extends ConsumerWidget {
神奈川県小田原市で農家や漁師をしています。夏の時期にレモンの収穫のお手伝いをしてくれる方を募集しています。こんな感じでここには自己紹介文を表示する。表示するのは最大 8 行表くらいでいいだろうか。あいうえお、かきくけこ、さしすせそ、たちつてと、なにぬねの、はひふへほ、まみむめも、やゆよ、わをん、あいうえお、かきくけこ、さしすせそ、たちつてと、なにぬねの、はひふへほ、まみむめも、やゆよ...''',
),
),
const SizedBox(height: 24),
const Gap(24),
Section(
title: 'ホストタイプ',
titleStyle: Theme.of(context).textTheme.titleLarge,
Expand Down Expand Up @@ -145,7 +145,7 @@ class HostPageBody extends ConsumerWidget {
child: Text('通信に失敗しました。'),
),
),
const SizedBox(height: 24),
const Gap(24),
Section(
title: '公開する場所・住所',
titleStyle: Theme.of(context).textTheme.titleLarge,
Expand All @@ -155,62 +155,14 @@ class HostPageBody extends ConsumerWidget {
children: [
Text('''
農場や主な作業場所などの、公開される場所・住所です。ワーカーは地図上から近所や興味がある地域のホストを探します。必ずしも正確で細かい住所である必要はありません。'''),
SizedBox(height: 12),
Gap(12),
// TODO ここは後でデータを取得する
Text('神奈川県小田原市石322 (hostLocation.address)'),
],
),
),
IdentityDependentBuilder(
buildForIdentity: () {
return Column(
children: [
const SizedBox(
height: 12,
),
Section(
titleBottomMargin: 4,
title: 'ユーザーモード',
content: Text(
currentUserMode == UserMode.host
? '''
ホストとしてアプリを使用します。あなたが募集するお手伝いに興味があるワーカーとやりとりをして、お手伝いを受け入れるモードです。'''
: '''ワーカーとしてアプリを使用します。興味のあるホストやお手伝いを探して、お手伝いに応募するモードです。''',
),
),
SegmentedButton<UserMode>(
style: ButtonStyle(
shape:
MaterialStateProperty.all<RoundedRectangleBorder>(
RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
),
segments: const <ButtonSegment<UserMode>>[
ButtonSegment<UserMode>(
value: UserMode.host,
label: Text('ホスト'),
),
ButtonSegment<UserMode>(
value: UserMode.worker,
label: Text('ワーカー'),
),
],
selected: <UserMode>{currentUserMode},
onSelectionChanged: (newSelection) {
ref
.read(userModeStateProvider.notifier)
.update((_) => newSelection.first);
},
),
],
);
},
targetUserId: userId,
),

const SizedBox(height: 24),
UserModeSection(userId: userId),
const Gap(24),
// TODO 自己紹介をDBに追加する
const Section(
titleBottomMargin: 4,
Expand All @@ -220,7 +172,7 @@ class HostPageBody extends ConsumerWidget {
神奈川県小田原市で農家や漁師をしています。夏の時期にレモンの収穫のお手伝いをしてくれる方を募集しています。こんな感じでここには自己紹介文を表示する。表示するのは最大 8 行表くらいでいいだろうか。あいうえお、かきくけこ、さしすせそ、たちつてと、なにぬねの、はひふへほ、まみむめも、やゆよ、わをん、あいうえお、かきくけこ、さしすせそ、たちつてと、なにぬねの、はひふへほ、まみむめも、やゆよ...''',
),
),
const SizedBox(height: 24),
const Gap(24),
const Section(
titleBottomMargin: 4,
title: 'ホストタイプ',
Expand Down Expand Up @@ -248,7 +200,7 @@ class HostPageBody extends ConsumerWidget {
child: Text('通信に失敗しました。'),
),
),
const SizedBox(height: 24),
const Gap(24),
const Section(
titleBottomMargin: 4,
title: '公開する場所・住所',
Expand All @@ -257,7 +209,7 @@ class HostPageBody extends ConsumerWidget {
children: [
Text('''
農場や主な作業場所などの、公開される場所・住所です。ワーカーは地図上から近所や興味がある地域のホストを探します。必ずしも正確で細かい住所である必要はありません。'''),
SizedBox(height: 12),
Gap(12),
// TODO ここは後でデータを取得する
Text('神奈川県小田原市石322 (hostLocation.address)'),
],
Expand Down Expand Up @@ -298,8 +250,9 @@ class HostPageBody extends ConsumerWidget {
),
),
),
IdentityDependentBuilder(
buildForIdentity: () {
UserAuthDependentBuilder(
userId: userId,
onUserAuthenticated: (_) {
return Column(
children: [
const Divider(height: 36),
Expand All @@ -314,7 +267,7 @@ class HostPageBody extends ConsumerWidget {
FontAwesomeIcons.google,
size: 30,
),
SizedBox(width: 10),
Gap(10),
Text('Google'),
// TODO google連携済みかどうかで出し分けられるようにする
Expanded(
Expand All @@ -325,14 +278,14 @@ class HostPageBody extends ConsumerWidget {
),
],
),
SizedBox(height: 12),
Gap(12),
Row(
children: [
FaIcon(
FontAwesomeIcons.apple,
size: 40,
),
SizedBox(width: 10),
Gap(10),
Text('Apple'),
// TODO apple連携済みかどうかで出し分けられるようにする
Expanded(
Expand All @@ -343,15 +296,15 @@ class HostPageBody extends ConsumerWidget {
),
],
),
SizedBox(height: 12),
Gap(12),
Row(
children: [
FaIcon(
FontAwesomeIcons.line,
color: Color(0xff06c755),
size: 30,
),
SizedBox(width: 10),
Gap(10),
Text('LINE'),
// TODO line連携済みかどうかで出し分けられるようにする
Expanded(
Expand All @@ -368,7 +321,6 @@ class HostPageBody extends ConsumerWidget {
],
);
},
targetUserId: userId,
),
const Gap(32),
],
Expand Down
1 change: 1 addition & 0 deletions packages/mottai_flutter_app/lib/map/ui/map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ class _HostLocationPageView extends ConsumerWidget {

@override
Widget build(BuildContext context, WidgetRef ref) {
// TODO: 表示する [HostLocation] がない場合の UI を考える。
return PageView.builder(
controller: PageController(viewportFraction: _viewportFraction),
itemCount: readHostLocations.length,
Expand Down
5 changes: 4 additions & 1 deletion packages/mottai_flutter_app/lib/root/ui/root.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ class _DrawerChild extends ConsumerWidget {
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(packageInfo.packageName),
Text(
'${packageInfo.packageName} '
'(${packageInfo.version}+${packageInfo.buildNumber})',
),
if (ref.watch(isHostProvider)) ...[
const Gap(8),
Text(
Expand Down

This file was deleted.

Loading

0 comments on commit 8d1c8be

Please sign in to comment.