Skip to content

Commit b958887

Browse files
committed
Extracted params creation logic to SelectedParamsFactory
1 parent 400954a commit b958887

File tree

2 files changed

+58
-54
lines changed

2 files changed

+58
-54
lines changed

app/src/main/java/com/saulmm/cui/OrderDialogFragment.java

Lines changed: 58 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import de.hdodenhof.circleimageview.CircleImageView;
4343

4444

45+
4546
public class OrderDialogFragment extends BottomSheetDialogFragment {
4647
public static final String ID_SIZE_SUFFIX = "txt_size";
4748
public static final String ID_COLOR_SUFFIX = "img_color";
@@ -110,13 +111,11 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
110111
binding.imgProduct.setImageDrawable(createProductImageDrawable(getProduct()));
111112

112113
initOrderStepOneView(binding.layoutStep1);
113-
114-
115114
}
116115

117-
private void transitionToSecondStep() {
116+
private void showDeliveryForm() {
118117
final LayoutFormOrderStep2Binding step2Binding = LayoutFormOrderStep2Binding.inflate(
119-
LayoutInflater.from(getContext()), binding.formContainer, false);//
118+
LayoutInflater.from(getContext()), binding.formContainer, false);
120119

121120
final Scene deliveryFormScene = new Scene(binding.formContainer,
122121
((ViewGroup) step2Binding.getRoot()));
@@ -143,7 +142,7 @@ private void transitionSelectedView(View v) {
143142
v.setSelected(true);
144143

145144
// Create the cloned view from the selected view at the same position
146-
final View clonedView = createClonedView(v);
145+
final View clonedView = createSelectionView(v);
147146

148147
// Add the cloned view to the constraint layout
149148
if (clonedViews.size() < 2) {
@@ -161,18 +160,17 @@ private void startCloneAnimation(View clonedView, View targetView) {
161160
TransitionManager.beginDelayedTransition(
162161
(ViewGroup) binding.getRoot(), selectedViewTransition);
163162

164-
ConstraintLayout.LayoutParams fakeEndParams = createFakeEndParams(clonedView, targetView);
165-
clonedView.setLayoutParams(fakeEndParams); // Fire the transition
163+
clonedView.setLayoutParams(SelectedParamsFactory.endParams(clonedView, targetView)); // Fire the transition
166164
}
167165
});
168166
}
169167

170-
private View createClonedView(View v) {
168+
private View createSelectionView(View v) {
171169
final String resourceName = getResources().getResourceEntryName(v.getId());
172170

173171
return (resourceName.startsWith(ID_COLOR_SUFFIX))
174-
? createFakeSelectedColorView((ImageView) v)
175-
: createFakeSelectedTextView(v);
172+
? createSelectedColorView((ImageView) v)
173+
: createSelectedTextView(v);
176174
}
177175

178176
private View getTargetView(View v) {
@@ -201,7 +199,7 @@ private void initOrderStepOneView(LayoutFormOrderStep1Binding layoutStep1) {
201199

202200
clonedViews.clear();
203201

204-
transitionToSecondStep();
202+
showDeliveryForm();
205203
});
206204

207205
layoutStep1.setListener(new Step1Listener() {
@@ -247,61 +245,24 @@ public void onTimeSelected(View v) {
247245
});
248246
}
249247

250-
private View createFakeSelectedTextView(View v) {
248+
private View createSelectedTextView(View v) {
251249
final TextView fakeSelectedTextView = new TextView(
252250
getContext(), null, R.attr.selectedTextStyle);
253251

254252
fakeSelectedTextView.setText("This is a test"); // TODO, set the proper text
255-
fakeSelectedTextView.setLayoutParams(createNewViewLayoutParams(v));
253+
fakeSelectedTextView.setLayoutParams(SelectedParamsFactory.startTextParams(v));
256254
return fakeSelectedTextView;
257255
}
258256

259-
private View createFakeSelectedColorView(ImageView imageView) {
257+
private View createSelectedColorView(ImageView selectedView) {
260258
final ImageView fakeImageView = new CircleImageView(
261259
getContext(), null, R.attr.colorStyle);
262260

263-
fakeImageView.setImageDrawable(imageView.getDrawable());
264-
fakeImageView.setLayoutParams(createCloneLayoutParams(imageView));
261+
fakeImageView.setImageDrawable(selectedView.getDrawable());
262+
fakeImageView.setLayoutParams(SelectedParamsFactory.startColorParams(selectedView));
265263
return fakeImageView;
266264
}
267265

268-
private ConstraintLayout.LayoutParams createFakeEndParams(View v, View targetView) {
269-
final ConstraintLayout.LayoutParams layoutParams =
270-
(ConstraintLayout.LayoutParams) v.getLayoutParams();
271-
272-
final int marginLeft = getContext().getResources()
273-
.getDimensionPixelOffset(R.dimen.spacing_medium);
274-
275-
layoutParams.setMargins(marginLeft, 0, 0, 0);
276-
layoutParams.topToTop = targetView.getId();
277-
layoutParams.startToEnd = targetView.getId();
278-
layoutParams.bottomToBottom = targetView.getId();
279-
280-
return layoutParams;
281-
}
282-
283-
private ConstraintLayout.LayoutParams createCloneLayoutParams(View v) {
284-
final ConstraintLayout.LayoutParams layoutParams =
285-
new ConstraintLayout.LayoutParams(v.getWidth(), v.getHeight());
286-
287-
layoutParams.topToTop = binding.formContainer.getId();
288-
layoutParams.leftToLeft = binding.formContainer.getId();
289-
layoutParams.setMargins((int) v.getX(), (int) v.getY(), 0, 0);
290-
return layoutParams;
291-
}
292-
293-
private ConstraintLayout.LayoutParams createNewViewLayoutParams(View v) {
294-
final ConstraintLayout.LayoutParams layoutParams =
295-
new ConstraintLayout.LayoutParams(
296-
ViewGroup.LayoutParams.WRAP_CONTENT,
297-
ViewGroup.LayoutParams.WRAP_CONTENT);
298-
299-
layoutParams.topToTop = binding.formContainer.getId();
300-
layoutParams.leftToLeft = binding.formContainer.getId();
301-
layoutParams.setMargins((int) v.getX(), (int) v.getY(), 0, 0);
302-
return layoutParams;
303-
}
304-
305266
private void changeToConfirmScene() {
306267
final LayoutOrderConfirmationBinding confirmationBinding = LayoutOrderConfirmationBinding
307268
.inflate(LayoutInflater.from(getContext()), binding.mainContainer, false);
@@ -353,4 +314,48 @@ private Runnable onEnterConfirmScene(LayoutOrderConfirmationBinding confirmation
353314
.start();
354315
};
355316
}
317+
318+
private static class SelectedParamsFactory {
319+
private static ConstraintLayout.LayoutParams startColorParams(View selectedView) {
320+
final int colorSize = selectedView.getContext().getResources()
321+
.getDimensionPixelOffset(R.dimen.product_color_size);
322+
323+
final ConstraintLayout.LayoutParams layoutParams =
324+
new ConstraintLayout.LayoutParams(colorSize, colorSize);
325+
326+
setStartState(selectedView, layoutParams);
327+
return layoutParams;
328+
}
329+
330+
private static ConstraintLayout.LayoutParams startTextParams(View selectedView) {
331+
final ConstraintLayout.LayoutParams layoutParams =
332+
new ConstraintLayout.LayoutParams(
333+
ViewGroup.LayoutParams.WRAP_CONTENT,
334+
ViewGroup.LayoutParams.WRAP_CONTENT);
335+
336+
setStartState(selectedView, layoutParams);
337+
return layoutParams;
338+
}
339+
340+
private static void setStartState(View selectedView, ConstraintLayout.LayoutParams layoutParams) {
341+
layoutParams.topToTop = ((ViewGroup) selectedView.getParent().getParent()).getId();
342+
layoutParams.leftToLeft = ((ViewGroup) selectedView.getParent().getParent()).getId();
343+
layoutParams.setMargins((int) selectedView.getX(), (int) selectedView.getY(), 0, 0);
344+
}
345+
346+
private static ConstraintLayout.LayoutParams endParams(View v, View targetView) {
347+
final ConstraintLayout.LayoutParams layoutParams =
348+
(ConstraintLayout.LayoutParams) v.getLayoutParams();
349+
350+
final int marginLeft = v.getContext().getResources()
351+
.getDimensionPixelOffset(R.dimen.spacing_medium);
352+
353+
layoutParams.setMargins(marginLeft, 0, 0, 0);
354+
layoutParams.topToTop = targetView.getId();
355+
layoutParams.startToEnd = targetView.getId();
356+
layoutParams.bottomToBottom = targetView.getId();
357+
358+
return layoutParams;
359+
}
360+
}
356361
}

app/src/main/res/layout/layout_form_order_step1.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
</data>
1313

1414
<android.support.constraint.ConstraintLayout
15-
1615
android:layout_width="match_parent"
1716
android:layout_height="wrap_content"
1817
tools:ignore="HardcodedText"

0 commit comments

Comments
 (0)