Skip to content

Commit d98523d

Browse files
committed
Update
1 parent ffe5419 commit d98523d

File tree

9 files changed

+327
-104
lines changed

9 files changed

+327
-104
lines changed

lib/app/app.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,10 @@ class MyApp extends StatelessWidget {
237237
child: BlocProvider<RelatedRoomsTabBloc>(
238238
child: RoomDetailPage(),
239239
blocSupplier: () {
240-
return RelatedRoomsTabBloc(injector.roomRepository)
241-
..fetch.call();
240+
return RelatedRoomsTabBloc(
241+
injector.roomRepository,
242+
roomId,
243+
)..fetch.call();
242244
},
243245
),
244246
),

lib/data/rooms/firestore_room_repository.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@ abstract class FirestoreRoomRepository {
3434
Duration timeout = const Duration(seconds: 10),
3535
});
3636

37-
Future<List<RoomEntity>> getRelatedRooms();
37+
Future<List<RoomEntity>> getRelatedRoomsBy({@required String roomId});
3838
}

lib/data/rooms/firestore_room_repository_impl.dart

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,32 @@ class FirestoreRoomRepositoryImpl implements FirestoreRoomRepository {
197197
}
198198

199199
@override
200-
Future<List<RoomEntity>> getRelatedRooms() async {
201-
return [];
200+
Future<List<RoomEntity>> getRelatedRoomsBy({String roomId}) async {
201+
if (roomId == null) {
202+
throw ArgumentError.notNull('roomId');
203+
}
204+
final room = await _firestore
205+
.document('motelrooms/$roomId')
206+
.get()
207+
.then((snapshot) => RoomEntity.fromDocumentSnapshot(snapshot));
208+
209+
const deltaPrice = 500000;
210+
const limit = 21;
211+
212+
return (await _firestore
213+
.collection('motelrooms')
214+
.where('approve', isEqualTo: true)
215+
.where('category', isEqualTo: room.category)
216+
.where('district', isEqualTo: room.district)
217+
.where('price', isGreaterThanOrEqualTo: room.price - deltaPrice)
218+
.where('price', isLessThanOrEqualTo: room.price + deltaPrice)
219+
.orderBy('price', descending: false)
220+
.orderBy('count_view', descending: true)
221+
.limit(limit)
222+
.getDocuments()
223+
.then(_toEntities))
224+
.item1
225+
.where((room) => room.id != roomId)
226+
.toList(growable: false);
202227
}
203228
}

lib/pages/detail/comments/comments_tab_bloc.dart

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ class CommentsTabBloc implements BaseBloc {
5757
submitAddCommentS
5858
];
5959

60-
final initialVS = CommentsTabState.initial();
60+
final initialVS = CommentsTabState.initial(authBloc.currentUser() != null);
6161

62-
final state$ = getCommentS.exhaustMap((_) {
62+
final scannedState$ = getCommentS.exhaustMap((_) {
6363
return Rx.combineLatest2(
6464
commentsRepository.commentsFor(roomId: roomId),
6565
authBloc.loginState$,
@@ -68,8 +68,22 @@ class CommentsTabBloc implements BaseBloc {
6868
).startWith(const Loading()).onErrorReturnWith((e) => Error(e));
6969
}).scan((state, change, _) => change.reducer(state), initialVS);
7070

71-
final stateDistinct$ =
72-
state$.publishValueSeededDistinct(seedValue: initialVS);
71+
final state$ = Rx.combineLatest2(
72+
scannedState$,
73+
authBloc.loginState$,
74+
(CommentsTabState state, LoginState loginState) {
75+
var isLoggedIn = false;
76+
if (loginState == null) {
77+
isLoggedIn = false;
78+
} else if (loginState is LoggedInUser) {
79+
isLoggedIn = true;
80+
} else if (loginState is Unauthenticated) {
81+
isLoggedIn = false;
82+
}
83+
84+
return state.rebuild((b) => b..isLoggedIn = isLoggedIn);
85+
},
86+
).publishValueSeededDistinct(seedValue: initialVS);
7387

7488
final comment$ = submitAddCommentS
7589
.withLatestFrom(commentChangedS, (_, String comment) => comment)
@@ -116,13 +130,13 @@ class CommentsTabBloc implements BaseBloc {
116130
(comment, content) => updateCommentS.add(Tuple2(comment, content)),
117131
commentChangedS.add,
118132
() => submitAddCommentS.add(null),
119-
stateDistinct$,
133+
state$,
120134
message$,
121135
DisposeBag(
122136
[
123-
stateDistinct$.listen((state) => print(
137+
state$.listen((state) => print(
124138
'$_tag ${state.isLoading} ${state.error} ${state.comments.length}')),
125-
stateDistinct$.connect(),
139+
state$.connect(),
126140
message$.connect(),
127141
commentChangedS.listen(print),
128142
...controllers,

lib/pages/detail/comments/comments_tab_page.dart

Lines changed: 85 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -98,92 +98,97 @@ class _CommentsTabPagesState extends State<CommentsTabPages> {
9898

9999
return Container(
100100
color: Colors.white,
101-
child: Column(
102-
children: <Widget>[
103-
Expanded(
104-
child: StreamBuilder<CommentsTabState>(
105-
stream: bloc.state$,
106-
initialData: bloc.state$.value,
107-
builder: (context, snapshot) {
108-
final state = snapshot.data;
101+
child: StreamBuilder<CommentsTabState>(
102+
stream: bloc.state$,
103+
initialData: bloc.state$.value,
104+
builder: (context, snapshot) {
105+
final state = snapshot.data;
109106

110-
if (state.isLoading) {
111-
return Center(
112-
child: CircularProgressIndicator(),
113-
);
114-
}
115-
116-
if (state.error != null) {
117-
return Center(
118-
child: Text('Error ${state.error}'),
119-
);
120-
}
121-
122-
final comments = state.comments;
123-
return Scrollbar(
124-
child: ListView.separated(
125-
physics: const BouncingScrollPhysics(),
126-
itemCount: comments.length,
127-
itemBuilder: (context, index) => CommentItemWidget(
128-
comment: comments[index],
129-
deleteCallback: showDeleteDialog,
130-
editCallback: showEditDialog,
131-
),
132-
separatorBuilder: (context, index) => Divider(
133-
color: Theme.of(context).dividerColor.withAlpha(128),
134-
),
135-
),
136-
);
137-
},
138-
),
139-
),
140-
Container(
141-
padding: const EdgeInsets.all(8.0),
142-
decoration: BoxDecoration(
143-
color: Colors.white,
144-
boxShadow: [
145-
BoxShadow(
146-
color: Colors.grey,
147-
blurRadius: 4,
148-
)
149-
],
150-
),
151-
child: Row(
107+
return Column(
152108
children: <Widget>[
153109
Expanded(
154-
child: TextField(
155-
controller: commentController,
156-
expands: false,
157-
focusNode: focusNode,
158-
decoration: InputDecoration(
159-
labelText: 'Comment',
160-
border: OutlineInputBorder(
161-
borderRadius: BorderRadius.circular(32),
162-
borderSide: const BorderSide(width: 0),
163-
),
164-
filled: true,
165-
),
166-
onSubmitted: (_) => bloc.submitAddComment(),
167-
keyboardType: TextInputType.multiline,
168-
textInputAction: TextInputAction.newline,
169-
maxLines: null,
110+
child: Builder(
111+
builder: (context) {
112+
if (state.isLoading) {
113+
return Center(
114+
child: CircularProgressIndicator(),
115+
);
116+
}
117+
118+
if (state.error != null) {
119+
return Center(
120+
child: Text('Error ${state.error}'),
121+
);
122+
}
123+
124+
final comments = state.comments;
125+
return Scrollbar(
126+
child: ListView.separated(
127+
physics: const BouncingScrollPhysics(),
128+
itemCount: comments.length,
129+
itemBuilder: (context, index) => CommentItemWidget(
130+
comment: comments[index],
131+
deleteCallback: showDeleteDialog,
132+
editCallback: showEditDialog,
133+
),
134+
separatorBuilder: (context, index) => Divider(
135+
color:
136+
Theme.of(context).dividerColor.withAlpha(128),
137+
),
138+
),
139+
);
140+
},
170141
),
171142
),
172-
const SizedBox(width: 8),
173-
FloatingActionButton(
174-
onPressed: () {
175-
focusNode.unfocus();
176-
bloc.submitAddComment();
177-
},
178-
child: Icon(
179-
Icons.send,
180-
),
181-
)
143+
if (state.isLoggedIn)
144+
Container(
145+
padding: const EdgeInsets.all(8.0),
146+
decoration: BoxDecoration(
147+
color: Colors.white,
148+
boxShadow: [
149+
BoxShadow(
150+
color: Colors.grey,
151+
blurRadius: 4,
152+
)
153+
],
154+
),
155+
child: Row(
156+
children: <Widget>[
157+
Expanded(
158+
child: TextField(
159+
controller: commentController,
160+
expands: false,
161+
focusNode: focusNode,
162+
decoration: InputDecoration(
163+
labelText: 'Comment',
164+
border: OutlineInputBorder(
165+
borderRadius: BorderRadius.circular(32),
166+
borderSide: const BorderSide(width: 0),
167+
),
168+
filled: true,
169+
),
170+
onSubmitted: (_) => bloc.submitAddComment(),
171+
keyboardType: TextInputType.multiline,
172+
textInputAction: TextInputAction.newline,
173+
maxLines: null,
174+
),
175+
),
176+
const SizedBox(width: 8),
177+
FloatingActionButton(
178+
onPressed: () {
179+
focusNode.unfocus();
180+
bloc.submitAddComment();
181+
},
182+
child: Icon(
183+
Icons.send,
184+
),
185+
)
186+
],
187+
),
188+
)
182189
],
183-
),
184-
)
185-
],
186-
),
190+
);
191+
}),
187192
);
188193
}
189194

lib/pages/detail/comments/comments_tab_state.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,19 @@ abstract class CommentsTabState
116116
@nullable
117117
Object get error;
118118

119+
bool get isLoggedIn;
120+
119121
CommentsTabState._();
120122

121123
factory CommentsTabState([updates(CommentsTabStateBuilder b)]) =
122124
_$CommentsTabState;
123125

124-
factory CommentsTabState.initial() => CommentsTabState(
126+
factory CommentsTabState.initial(bool isLoggedIn) => CommentsTabState(
125127
(b) => b
126128
..isLoading = true
127129
..error = null
128-
..comments = ListBuilder<CommentItem>(),
130+
..comments = ListBuilder<CommentItem>()
131+
..isLoggedIn = isLoggedIn,
129132
);
130133
}
131134

lib/pages/detail/comments/comments_tab_state.g.dart

Lines changed: 22 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)