1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.constraintlayout.core.state.helpers;
18 
19 import static androidx.constraintlayout.core.widgets.ConstraintWidget.UNKNOWN;
20 
21 import androidx.constraintlayout.core.state.ConstraintReference;
22 import androidx.constraintlayout.core.state.State;
23 import androidx.constraintlayout.core.widgets.ConstraintWidget;
24 
25 public class VerticalChainReference extends ChainReference {
26 
VerticalChainReference(State state)27     public VerticalChainReference(State state) {
28         super(state, State.Helper.VERTICAL_CHAIN);
29     }
30 
31     // @TODO: add description
32     @Override
apply()33     public void apply() {
34         ConstraintReference first = null;
35         ConstraintReference previous = null;
36         for (Object key : mReferences) {
37             ConstraintReference reference = mHelperState.constraints(key);
38             reference.clearVertical();
39         }
40 
41         for (Object key : mReferences) {
42             ConstraintReference reference = mHelperState.constraints(key);
43             if (first == null) {
44                 first = reference;
45                 if (mTopToTop != null) {
46                     first.topToTop(mTopToTop).margin(mMarginTop).marginGone(mMarginTopGone);
47                 } else if (mTopToBottom != null) {
48                     first.topToBottom(mTopToBottom).margin(mMarginTop).marginGone(mMarginTopGone);
49                 } else {
50                     // No constraint declared, default to Parent.
51                     String refKey = reference.getKey().toString();
52                     first.topToTop(State.PARENT).margin(getPreMargin(refKey)).marginGone(
53                             getPreGoneMargin(refKey));
54                 }
55             }
56             if (previous != null) {
57                 String preKey = previous.getKey().toString();
58                 String refKey = reference.getKey().toString();
59                 previous.bottomToTop(reference.getKey()).margin(getPostMargin(preKey)).marginGone(
60                         getPostGoneMargin(preKey));
61                 reference.topToBottom(previous.getKey()).margin(getPreMargin(refKey)).marginGone(
62                         getPreGoneMargin(refKey));
63             }
64             float weight = getWeight(key.toString());
65             if (weight != UNKNOWN) {
66                 reference.setVerticalChainWeight(weight);
67             }
68             previous = reference;
69         }
70 
71         if (previous != null) {
72             if (mBottomToTop != null) {
73                 previous.bottomToTop(mBottomToTop)
74                         .margin(mMarginBottom)
75                         .marginGone(mMarginBottomGone);
76             } else if (mBottomToBottom != null) {
77                 previous.bottomToBottom(mBottomToBottom)
78                         .margin(mMarginBottom)
79                         .marginGone(mMarginBottomGone);
80             } else {
81                 // No constraint declared, default to Parent.
82                 String preKey = previous.getKey().toString();
83                 previous.bottomToBottom(State.PARENT).margin(getPostMargin(preKey)).marginGone(
84                         getPostGoneMargin(preKey));
85             }
86         }
87 
88         if (first == null) {
89             return;
90         }
91 
92         if (mBias != 0.5f) {
93             first.verticalBias(mBias);
94         }
95 
96         switch (mStyle) {
97             case SPREAD: {
98                 first.setVerticalChainStyle(ConstraintWidget.CHAIN_SPREAD);
99             }
100             break;
101             case SPREAD_INSIDE: {
102                 first.setVerticalChainStyle(ConstraintWidget.CHAIN_SPREAD_INSIDE);
103             }
104             break;
105             case PACKED: {
106                 first.setVerticalChainStyle(ConstraintWidget.CHAIN_PACKED);
107             }
108         }
109     }
110 }
111