1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
package com.mapswithme.maps.ugc;
import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.mapswithme.maps.R;
import com.mapswithme.maps.bookmarks.data.MapObject;
import com.mapswithme.maps.widget.RatingView;
import com.mapswithme.maps.widget.placepage.PlacePageView;
import com.mapswithme.maps.widget.recycler.ItemDecoratorFactory;
import com.mapswithme.util.DateUtils;
import com.mapswithme.util.UiUtils;
import java.text.DateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;
public class UGCController implements View.OnClickListener, UGC.ReceiveUGCListener
{
@NonNull
private final View mUgcRootView;
@NonNull
private final View mUgcAddRatingView;
@NonNull
private final View mUgcMoreReviews;
@NonNull
private final UGCReviewAdapter mUGCReviewAdapter = new UGCReviewAdapter();
@NonNull
private final UGCRatingRecordsAdapter mUGCRatingRecordsAdapter = new UGCRatingRecordsAdapter();
@NonNull
private final UGCRatingRecordsAdapter mUGCUserRatingRecordsAdapter = new UGCRatingRecordsAdapter();
@NonNull
private final TextView mReviewCount;
@NonNull
private final PlacePageView mPlacePage;
@NonNull
private final Button mLeaveReviewButton;
@NonNull
private final View mPreviewUgcInfoView;
@NonNull
private final View mUserRatingRecordsContainer;
@NonNull
private final View mUserReviewView;
@NonNull
private final View mUserReviewDivider;
@NonNull
private final View mReviewListDivider;
@NonNull
private final View mSummaryRootView;
@NonNull
private final TextView mSummaryReviewCount;
@NonNull
private final View.OnClickListener mLeaveReviewClickListener = new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if (mMapObject == null)
return;
EditParams.Builder builder = EditParams.Builder.fromMapObject(mMapObject)
.setDefaultRating(UGC.RATING_NONE)
.setFromPP(true);
UGCEditorActivity.start((Activity) mPlacePage.getContext(), builder.build());
}
};
@NonNull
private final View.OnClickListener mMoreReviewsClickListener = new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO: coming soon.
}
};
@Nullable
private MapObject mMapObject;
public UGCController(@NonNull final PlacePageView placePage)
{
mPlacePage = placePage;
final Context context = mPlacePage.getContext();
mUgcRootView = mPlacePage.findViewById(R.id.ll__pp_ugc);
mPreviewUgcInfoView = mPlacePage.findViewById(R.id.preview_rating_info);
mUgcMoreReviews = mPlacePage.findViewById(R.id.tv__pp_ugc_reviews_more);
mUgcMoreReviews.setOnClickListener(mMoreReviewsClickListener);
mUgcAddRatingView = mPlacePage.findViewById(R.id.ll__pp_ugc_add_rating);
mUgcAddRatingView.findViewById(R.id.ll__horrible).setOnClickListener(this);
mUgcAddRatingView.findViewById(R.id.ll__bad).setOnClickListener(this);
mUgcAddRatingView.findViewById(R.id.ll__normal).setOnClickListener(this);
mUgcAddRatingView.findViewById(R.id.ll__good).setOnClickListener(this);
mUgcAddRatingView.findViewById(R.id.ll__excellent).setOnClickListener(this);
mReviewCount = (TextView) mPlacePage.findViewById(R.id.tv__review_count);
mLeaveReviewButton = (Button) mPlacePage.findViewById(R.id.leaveReview);
mLeaveReviewButton.setOnClickListener(mLeaveReviewClickListener);
RecyclerView rvUGCReviews = (RecyclerView) mPlacePage.findViewById(R.id.rv__pp_ugc_reviews);
rvUGCReviews.setLayoutManager(new LinearLayoutManager(context));
rvUGCReviews.getLayoutManager().setAutoMeasureEnabled(true);
rvUGCReviews.addItemDecoration(
ItemDecoratorFactory.createDefaultDecorator(context, LinearLayoutManager.VERTICAL));
rvUGCReviews.setNestedScrollingEnabled(false);
rvUGCReviews.setHasFixedSize(false);
rvUGCReviews.setAdapter(mUGCReviewAdapter);
mSummaryRootView = mPlacePage.findViewById(R.id.ll__summary_container);
View summaryRatingContainer = mSummaryRootView.findViewById(R.id.summary_rating_records);
RecyclerView rvRatingRecords = (RecyclerView) summaryRatingContainer.findViewById(R.id.rv__summary_rating_records);
rvRatingRecords.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
rvRatingRecords.getLayoutManager().setAutoMeasureEnabled(true);
rvRatingRecords.setNestedScrollingEnabled(false);
rvRatingRecords.setHasFixedSize(false);
rvRatingRecords.addItemDecoration(
ItemDecoratorFactory.createRatingRecordDecorator(context, LinearLayoutManager.HORIZONTAL));
rvRatingRecords.setAdapter(mUGCRatingRecordsAdapter);
mSummaryReviewCount = (TextView) mSummaryRootView.findViewById(R.id.tv__review_count);
mUserRatingRecordsContainer = mPlacePage.findViewById(R.id.user_rating_records);
RecyclerView userRatingRecords = (RecyclerView) mUserRatingRecordsContainer.findViewById(R.id.rv__summary_rating_records);
userRatingRecords.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
userRatingRecords.getLayoutManager().setAutoMeasureEnabled(true);
userRatingRecords.setNestedScrollingEnabled(false);
userRatingRecords.setHasFixedSize(false);
userRatingRecords.addItemDecoration(
ItemDecoratorFactory.createRatingRecordDecorator(context, LinearLayoutManager.HORIZONTAL));
userRatingRecords.setAdapter(mUGCUserRatingRecordsAdapter);
mUserReviewView = mPlacePage.findViewById(R.id.rl_user_review);
mUserReviewView.findViewById(R.id.rating).setVisibility(View.GONE);
mReviewListDivider = mPlacePage.findViewById(R.id.ugc_review_list_divider);
mUserReviewDivider = mPlacePage.findViewById(R.id.user_review_divider);
UGC.setReceiveListener(this);
}
public void clearViewsFor(@NonNull MapObject mapObject)
{
UiUtils.hide(mUgcRootView, mLeaveReviewButton, mPreviewUgcInfoView);
mUGCReviewAdapter.setItems(Collections.emptyList());
mUGCRatingRecordsAdapter.setItems(Collections.emptyList());
mUGCUserRatingRecordsAdapter.setItems(Collections.emptyList());
mReviewCount.setText("");
mSummaryReviewCount.setText("");
}
@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.ll__horrible:
onAggRatingTapped(UGC.RATING_HORRIBLE);
break;
case R.id.ll__bad:
onAggRatingTapped(UGC.RATING_BAD);
break;
case R.id.ll__normal:
onAggRatingTapped(UGC.RATING_NORMAL);
break;
case R.id.ll__good:
onAggRatingTapped(UGC.RATING_GOOD);
break;
case R.id.ll__excellent:
onAggRatingTapped(UGC.RATING_EXCELLENT);
break;
default:
throw new AssertionError("Unknown rating view:");
}
}
public void getUGC(@NonNull MapObject mapObject)
{
if (!mapObject.shouldShowUGC())
return;
mMapObject = mapObject;
UGC.nativeRequestUGC(mMapObject.getFeatureId());
}
@Override
public void onUGCReceived(@Nullable UGC ugc, @Nullable UGCUpdate ugcUpdate, @UGC.Impress int impress,
@NonNull String rating)
{
UiUtils.show(mUgcRootView);
UiUtils.showIf(ugc != null || canUserRate(ugcUpdate) || ugcUpdate != null, mPreviewUgcInfoView);
UiUtils.showIf(canUserRate(ugcUpdate), mLeaveReviewButton, mUgcAddRatingView);
UiUtils.showIf(ugc != null, mUgcMoreReviews);
UiUtils.showIf(ugc != null && impress != UGC.RATING_NONE, mSummaryRootView);
RatingView ratingView = mPreviewUgcInfoView.findViewById(R.id.rating_view);
if (ugc == null)
{
mReviewCount.setText(ugcUpdate != null ? R.string.placepage_reviewed : R.string.placepage_no_reviews);
ratingView.setRating(ugcUpdate == null ? Impress.NONE : Impress.COMING_SOON, rating);
setUserReviewAndRatingsView(ugcUpdate);
return;
}
Context context = mPlacePage.getContext();
int reviewsCount = ugc.getBasedOnCount();
if (impress != UGC.RATING_NONE)
{
mReviewCount.setText(context.getResources().getQuantityString(
R.plurals.placepage_summary_rating_description, reviewsCount, reviewsCount));
setSummaryViews(ugc, impress, rating);
}
ratingView.setRating(Impress.values()[impress], rating);
setUserReviewAndRatingsView(ugcUpdate);
List<UGC.Review> reviews = ugc.getReviews();
if (reviews != null)
mUGCReviewAdapter.setItems(ugc.getReviews());
UiUtils.showIf(reviews != null, mReviewListDivider);
// TODO: don't show "more reviews" button while reviews screen activity is not ready.
UiUtils.showIf(false /* reviews != null && reviews.size() > UGCReviewAdapter.MAX_COUNT */,
mUgcMoreReviews);
}
private void setSummaryViews(@NonNull UGC ugc, @UGC.Impress int impress, @NonNull String rating)
{
RatingView ratingView = (RatingView) mSummaryRootView.findViewById(R.id.rv__summary_rating);
ratingView.setRating(Impress.values()[impress], rating);
Context context = mPlacePage.getContext();
int reviewsCount = ugc.getBasedOnCount();
mSummaryReviewCount.setText(context.getResources().getQuantityString(
R.plurals.placepage_summary_rating_description, reviewsCount, reviewsCount));
mUGCRatingRecordsAdapter.setItems(ugc.getRatings());
}
private boolean canUserRate(@Nullable UGCUpdate ugcUpdate)
{
return mMapObject != null && mMapObject.canBeRated() && ugcUpdate == null;
}
private void onAggRatingTapped(@UGC.Impress int rating)
{
if (mMapObject == null)
return;
EditParams.Builder builder = EditParams.Builder.fromMapObject(mMapObject)
.setDefaultRating(rating);
UGCEditorActivity.start((Activity) mPlacePage.getContext(), builder.build());
}
private void setUserReviewAndRatingsView(@Nullable UGCUpdate update)
{
UiUtils.showIf(update != null, mUserReviewView, mUserReviewDivider,
mUserRatingRecordsContainer);
if (update == null)
return;
TextView name = mUserReviewView.findViewById(R.id.name);
TextView date = mUserReviewView.findViewById(R.id.date);
name.setText(R.string.placepage_reviews_your_comment);
DateFormat formatter = DateUtils.getMediumDateFormatter();
date.setText(formatter.format(new Date(update.getTimeMillis())));
TextView review = mUserReviewView.findViewById(R.id.review);
UiUtils.showIf(!TextUtils.isEmpty(update.getText()), review);
review.setText(update.getText());
mUGCUserRatingRecordsAdapter.setItems(update.getRatings());
}
}
|