Skip to content

Commit

Permalink
use kotlin viewbinding and barriers within constraintLayouts
Browse files Browse the repository at this point in the history
  • Loading branch information
ialokim authored and grote committed Nov 1, 2019
1 parent e124af1 commit 4a1f997
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 112 deletions.
Expand Up @@ -23,15 +23,13 @@ import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProviders
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.appcompat.widget.Toolbar
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast

import javax.inject.Inject
Expand All @@ -52,6 +50,7 @@ import de.grobox.transportr.trips.detail.TripDetailViewModel.SheetState.*
import de.grobox.transportr.trips.detail.TripUtils.intoCalendar
import de.grobox.transportr.trips.detail.TripUtils.share
import de.grobox.transportr.utils.DateUtils.*
import kotlinx.android.synthetic.main.fragment_trip_detail.*

class TripDetailFragment : TransportrFragment(), Toolbar.OnMenuItemClickListener {

Expand All @@ -63,39 +62,17 @@ class TripDetailFragment : TransportrFragment(), Toolbar.OnMenuItemClickListener
lateinit var viewModelFactory: ViewModelProvider.Factory

private lateinit var viewModel: TripDetailViewModel
private lateinit var toolbar: Toolbar
private lateinit var list: RecyclerView
private lateinit var bottomBar: View
private lateinit var fromTime: TextView
private lateinit var from: TextView
private lateinit var toTime: TextView
private lateinit var to: TextView
private lateinit var duration: TextView
private lateinit var price: TextView
private lateinit var topBar: View
private lateinit var fromTimeRel: TextView
private lateinit var durationTop: TextView
private lateinit var priceTop: TextView

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val v = inflater.inflate(R.layout.fragment_trip_detail, container, false)
return inflater.inflate(R.layout.fragment_trip_detail, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

setHasOptionsMenu(true)
component.inject(this)

toolbar = v.findViewById(R.id.toolbar)
list = v.findViewById(R.id.list)
bottomBar = v.findViewById(R.id.bottomBar)
fromTime = bottomBar.findViewById(R.id.fromTime)
from = bottomBar.findViewById(R.id.from)
toTime = bottomBar.findViewById(R.id.toTime)
to = bottomBar.findViewById(R.id.to)
duration = bottomBar.findViewById(R.id.duration)
price = bottomBar.findViewById(R.id.price)
topBar = v.findViewById(R.id.topBar)
fromTimeRel = topBar.findViewById(R.id.fromTimeRel)
durationTop = topBar.findViewById(R.id.durationTop)
priceTop = topBar.findViewById(R.id.priceTop)

toolbar.setNavigationOnClickListener { _ -> onToolbarClose() }
toolbar.setOnMenuItemClickListener(this)
list.layoutManager = LinearLayoutManager(context)
Expand All @@ -104,8 +81,6 @@ class TripDetailFragment : TransportrFragment(), Toolbar.OnMenuItemClickListener
viewModel = ViewModelProviders.of(activity!!, viewModelFactory).get(TripDetailViewModel::class.java)
viewModel.getTrip().observe(this, Observer<Trip> { this.onTripChanged(it) })
viewModel.sheetState.observe(this, Observer<SheetState> { this.onSheetStateChanged(it) })

return v
}

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
Expand Down
80 changes: 34 additions & 46 deletions app/src/main/res/layout/fragment_trip_detail.xml
Expand Up @@ -25,6 +25,7 @@
android:id="@+id/bottomBar"
android:layout_width="match_parent"
android:layout_height="@dimen/mapPadding"
android:padding="8dp"
android:layout_marginTop="0dp"
android:background="@color/primary"
android:clickable="true"
Expand All @@ -40,9 +41,6 @@
android:id="@+id/fromTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:textColor="?android:textColorPrimary"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
Expand All @@ -54,23 +52,19 @@
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ellipsize="middle"
android:singleLine="true"
android:textColor="?android:textColorPrimary"
app:layout_constraintEnd_toStartOf="@+id/durationPrice"
app:layout_constraintEnd_toStartOf="@+id/barrierRight"
app:layout_constraintStart_toEndOf="@+id/fromTime"
app:layout_constraintBaseline_toBaselineOf="@+id/fromTime"
app:layout_constraintTop_toTopOf="parent"
tools:text="From Station"/>

<TextView
android:id="@+id/toTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:textColor="?android:textColorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
Expand All @@ -81,51 +75,47 @@
android:id="@+id/to"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ellipsize="middle"
android:singleLine="true"
android:textColor="?android:textColorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/durationPrice"
app:layout_constraintEnd_toStartOf="@+id/barrierRight"
app:layout_constraintStart_toEndOf="@+id/toTime"
app:layout_constraintTop_toBottomOf="@+id/from"
app:layout_constraintBaseline_toBaselineOf="@+id/toTime"
tools:text="To Station"/>

<LinearLayout
android:id="@+id/durationPrice"
<TextView
android:id="@+id/duration"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:gravity="center_vertical"
app:layout_constraintEnd_toEndOf="parent"
android:layout_height="wrap_content"
android:textColor="?android:textColorSecondary"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent">

<TextView
android:id="@+id/duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:textColor="?android:textColorSecondary"
tools:text="0:42"/>

<TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:textColor="?android:textColorSecondary"
tools:visibility="gone"
tools:text="1,50 €"/>

</LinearLayout>
app:layout_constraintBottom_toTopOf="@+id/price"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintVertical_chainStyle="packed"
tools:text="0:42"/>

<TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:textColor="?android:textColorSecondary"
app:layout_constraintTop_toBottomOf="@+id/duration"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
tools:visibility="visible"
tools:text="1,50 €"/>

<androidx.constraintlayout.widget.Barrier
android:id="@+id/barrierRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="left"
app:constraint_referenced_ids="duration,price"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Expand All @@ -150,9 +140,7 @@
android:textColor="?android:textColorPrimary"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintRight_toLeftOf="@+id/durationTop"
tools:text="in 20min"
tools:visibility="visible"/>

Expand All @@ -165,8 +153,8 @@
android:textColor="?android:textColorSecondary"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:text="Gesamtdauer: 0:42"/>

<TextView
Expand Down
77 changes: 43 additions & 34 deletions app/src/main/res/layout/list_item_trip.xml
Expand Up @@ -26,17 +26,16 @@
android:textStyle="normal|bold"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="in 20min"
tools:text="in 20 min"
tools:visibility="visible"/>

<TextView
android:id="@+id/fromTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:textColor="?android:textColorPrimary"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/fromTimeRel"
app:layout_constraintBaseline_toBaselineOf="@+id/fromLocation"
app:layout_goneMarginTop="0dp"
tools:text="12:22"/>

Expand All @@ -58,12 +57,15 @@
android:id="@+id/fromLocation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginRight="8dp"
android:layout_marginEnd="8dp"
android:textColor="?android:textColorPrimary"
app:layout_constraintLeft_toRightOf="@+id/fromTime"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@+id/fromTime"
app:layout_constraintTop_toBottomOf="@+id/fromTimeRel"
app:layout_constraintLeft_toRightOf="@+id/barrierLeft"
app:layout_constraintRight_toLeftOf="@+id/barrierRight"
tools:text="From Location"/>

<ImageView
Expand All @@ -83,53 +85,46 @@
android:id="@+id/lines"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="4dp"
app:flexWrap="wrap"
app:layout_constraintEnd_toStartOf="@+id/durationPrice"
app:layout_constraintStart_toStartOf="@+id/fromLocation"
app:layout_constraintEnd_toEndOf="@+id/fromLocation"
app:layout_constraintTop_toBottomOf="@+id/fromLocation">

<include layout="@layout/list_item_line"/>

</com.google.android.flexbox.FlexboxLayout>

<LinearLayout
android:id="@+id/durationPrice"
<TextView
android:id="@+id/duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_vertical"
android:textColor="?android:textColorSecondary"
app:layout_constraintBottom_toTopOf="@+id/price"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/lines"
app:layout_constraintBottom_toBottomOf="@+id/lines">

<TextView
android:id="@+id/duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:textColor="?android:textColorSecondary"
tools:text="0:42"/>

<TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:textColor="?android:textColorSecondary"
tools:text="1,50 €"/>

</LinearLayout>
app:layout_constraintVertical_chainStyle="packed"
tools:text="0:42"/>

<TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:textColor="?android:textColorSecondary"
app:layout_constraintTop_toBottomOf="@+id/duration"
app:layout_constraintBottom_toBottomOf="@+id/lines"
app:layout_constraintEnd_toEndOf="parent"
tools:visibility="visible"
tools:text="1,50 €"/>

<TextView
android:id="@+id/toTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="?android:textColorPrimary"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="@+id/toLocation"
app:layout_constraintBaseline_toBaselineOf="@+id/toLocation"
tools:text="23:42"/>

<TextView
Expand All @@ -152,10 +147,24 @@
android:layout_marginTop="4dp"
android:textColor="?android:textColorPrimary"
app:layout_constraintLeft_toLeftOf="@+id/fromLocation"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintRight_toRightOf="@+id/fromLocation"
app:layout_constraintTop_toBottomOf="@+id/lines"
tools:text="To Location"/>

<androidx.constraintlayout.widget.Barrier
android:id="@+id/barrierLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="right"
app:constraint_referenced_ids="toDelay,toTime,fromTime,fromDelay,warning"/>

<androidx.constraintlayout.widget.Barrier
android:id="@+id/barrierRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="left"
app:constraint_referenced_ids="duration,price"/>

</androidx.constraintlayout.widget.ConstraintLayout>

</androidx.cardview.widget.CardView>

0 comments on commit 4a1f997

Please sign in to comment.