Skip to content

Commit

Permalink
Rearrange directions form
Browse files Browse the repository at this point in the history
  • Loading branch information
ialokim committed Feb 20, 2020
1 parent b4d77cf commit 046f499
Show file tree
Hide file tree
Showing 32 changed files with 325 additions and 327 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
return true;
case R.id.action_time:
if (calendar == null) calendar = Calendar.getInstance();
TimeDateFragment fragment = TimeDateFragment.newInstance(calendar);
TimeDateFragment fragment = TimeDateFragment.newInstance(calendar, null);
fragment.setTimeDateListener(this);
fragment.show(getSupportFragmentManager(), TimeDateFragment.TAG);
return true;
Expand All @@ -215,6 +215,10 @@ public void onTimeAndDateSet(Calendar calendar) {
getSupportLoaderManager().restartLoader(LOADER_DEPARTURES, args, this).forceLoad();
}

@Override
public void onDepartureOrArrivalSet(boolean departure) {
}

private synchronized void loadMoreDepartures(boolean later) {
Date date = new Date();
int maxDepartures = MAX_DEPARTURES;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ import android.view.View.*
import android.view.animation.Animation
import android.view.animation.Animation.RELATIVE_TO_SELF
import android.view.animation.TranslateAnimation
import androidx.appcompat.widget.TooltipCompat
import androidx.core.content.ContextCompat
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProviders
import de.grobox.transportr.R
import de.grobox.transportr.TransportrFragment
import de.grobox.transportr.data.locations.FavoriteLocation.FavLocationType
Expand All @@ -44,6 +44,7 @@ import de.grobox.transportr.utils.Constants.DEPARTURE
import de.grobox.transportr.utils.Constants.EXPANDED
import de.grobox.transportr.utils.DateUtils
import de.grobox.transportr.utils.DateUtils.*
import de.schildbach.pte.dto.Product
import kotlinx.android.synthetic.main.fragment_directions_form.*
import java.util.*
import javax.annotation.ParametersAreNonnullByDefault
Expand All @@ -58,15 +59,12 @@ class DirectionsFragment : TransportrFragment() {
internal lateinit var viewModelFactory: ViewModelProvider.Factory

private lateinit var viewModel: DirectionsViewModel
private var expandItem: MenuItem? = null

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val v = inflater.inflate(R.layout.fragment_directions_form, container, false)
component.inject(this)

setHasOptionsMenu(true)

viewModel = ViewModelProviders.of(activity!!, viewModelFactory).get(DirectionsViewModel::class.java)
viewModel = ViewModelProvider(activity!!, viewModelFactory).get(DirectionsViewModel::class.java)

return v
}
Expand All @@ -89,64 +87,59 @@ class DirectionsFragment : TransportrFragment() {
viaLocation.setLocationViewListener(viewModel)
toLocation.setLocationViewListener(viewModel)

viewModel.home.observe(this, Observer { homeLocation ->
fromLocation.setHomeLocation(homeLocation)
viaLocation.setHomeLocation(homeLocation)
toLocation.setHomeLocation(homeLocation)
viewModel.home.observe(viewLifecycleOwner, Observer {
fromLocation.setHomeLocation(it)
viaLocation.setHomeLocation(it)
toLocation.setHomeLocation(it)
})
viewModel.work.observe(this, Observer { workLocation ->
fromLocation.setWorkLocation(workLocation)
viaLocation.setWorkLocation(workLocation)
toLocation.setWorkLocation(workLocation)
viewModel.work.observe(viewLifecycleOwner, Observer {
fromLocation.setWorkLocation(it)
viaLocation.setWorkLocation(it)
toLocation.setWorkLocation(it)
})
viewModel.locations.observe(this, Observer { favoriteLocations ->
if (favoriteLocations == null) return@Observer
fromLocation.setFavoriteLocations(favoriteLocations)
viaLocation.setFavoriteLocations(favoriteLocations)
toLocation.setFavoriteLocations(favoriteLocations)
viewModel.locations.observe(viewLifecycleOwner, Observer {
if (it == null) return@Observer
fromLocation.setFavoriteLocations(it)
viaLocation.setFavoriteLocations(it)
toLocation.setFavoriteLocations(it)
})
viewModel.fromLocation.observe(this, Observer { location ->
fromLocation.setLocation(location)
if (location != null) toLocation.requestFocus()
viewModel.fromLocation.observe(viewLifecycleOwner, Observer {
fromLocation.setLocation(it)
if (it != null) toLocation.requestFocus()
})
viewModel.viaLocation.observe(this, Observer { location -> viaLocation.setLocation(location) })
viewModel.toLocation.observe(this, Observer { location -> toLocation.setLocation(location) })
viewModel.isDeparture.observe(this, Observer<Boolean> { this.onIsDepartureChanged(it) })
viewModel.isExpanded.observe(this, Observer<Boolean> { this.onViaVisibleChanged(it) })
viewModel.calendar.observe(this, Observer { this.onCalendarUpdated(it) })
viewModel.findGpsLocation.observe(this, Observer { this.onFindGpsLocation(it) })
viewModel.isFavTrip.observe(this, Observer { this.onFavStatusChanged(it) })

favIcon.visibility = VISIBLE
favIcon.setOnClickListener { viewModel.toggleFavTrip() }
viewModel.viaLocation.observe(viewLifecycleOwner, Observer { viaLocation.setLocation(it) })
viewModel.toLocation.observe(viewLifecycleOwner, Observer { toLocation.setLocation(it) })
viewModel.isDeparture.observe(viewLifecycleOwner, Observer<Boolean> { this.onIsDepartureChanged(it) })
viewModel.isExpanded.observe(viewLifecycleOwner, Observer<Boolean> { this.onViaVisibleChanged(it) })
viewModel.calendar.observe(viewLifecycleOwner, Observer { this.onCalendarUpdated(it) })
viewModel.findGpsLocation.observe(viewLifecycleOwner, Observer { this.onFindGpsLocation(it) })
viewModel.isFavTrip.observe(viewLifecycleOwner, Observer { this.onFavStatusChanged(it) })
viewModel.products.observe(viewLifecycleOwner, Observer { this.onProductsChanged(it) })

departureIcon.setOnClickListener { viewModel.toggleDeparture() }
favIcon.setOnClickListener { viewModel.toggleFavTrip() }

val onTimeClickListener = OnClickListener {
timeBackground.setOnClickListener {
if (viewModel.calendar.value == null) throw IllegalStateException()
val fragment = TimeDateFragment.newInstance(viewModel.calendar.value!!)
val fragment = TimeDateFragment.newInstance(viewModel.calendar.value!!, viewModel.isDeparture.value!!)
fragment.setTimeDateListener(viewModel)
fragment.show(activity!!.supportFragmentManager, TimeDateFragment.TAG)
}
val onTimeLongClickListener = OnLongClickListener {
timeBackground.setOnLongClickListener {
viewModel.resetCalender()
true
}

timeIcon.setOnClickListener(onTimeClickListener)
date.setOnClickListener(onTimeClickListener)
time.setOnClickListener(onTimeClickListener)

timeIcon.setOnLongClickListener(onTimeLongClickListener)
date.setOnLongClickListener(onTimeLongClickListener)
time.setOnLongClickListener(onTimeLongClickListener)

productsIcon.setOnClickListener {
activity?.let { a ->
ProductDialogFragment().show(a.supportFragmentManager, ProductDialogFragment.TAG)
}
}
swapIcon.setOnClickListener { swapLocations() }
viaIcon.setOnClickListener { viewModel.toggleIsExpanded() }

TooltipCompat.setTooltipText(productsIcon, getString(R.string.action_choose_products))
TooltipCompat.setTooltipText(swapIcon, getString(R.string.action_switch_locations))
TooltipCompat.setTooltipText(viaIcon, getString(R.string.action_navigation_expand))
}

override fun onSaveInstanceState(outState: Bundle) {
Expand All @@ -166,22 +159,7 @@ class DirectionsFragment : TransportrFragment() {
viewModel.setToLocation(toLocation.getLocation())
viewModel.onTimeAndDateSet(savedInstanceState.getSerializable(DATE) as Calendar)
}
}

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.directions, menu)
expandItem = menu.findItem(R.id.action_navigation_expand)
super.onCreateOptionsMenu(menu, inflater)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.action_navigation_expand -> {
viewModel.setIsExpanded(!item.isChecked)
true
}
else -> super.onOptionsItemSelected(item)
}
(activity!!.supportFragmentManager.findFragmentByTag(TimeDateFragment.TAG) as? TimeDateFragment)?.setTimeDateListener(viewModel)
}

private fun onCalendarUpdated(calendar: Calendar?) {
Expand Down Expand Up @@ -251,12 +229,11 @@ class DirectionsFragment : TransportrFragment() {
}

private fun onIsDepartureChanged(isDeparture: Boolean) {
departureIcon.setImageResource(if (isDeparture) R.drawable.ic_trip_departure else R.drawable.ic_trip_arrival)
departure.text = getString(if (isDeparture) R.string.trip_dep else R.string.trip_arr)
}

private fun onViaVisibleChanged(viaVisible: Boolean) {
expandItem?.isChecked = viaVisible
expandItem?.setIcon(if (viaVisible) R.drawable.ic_action_navigation_unfold_less_white else R.drawable.ic_action_navigation_unfold_more_white)
viaIcon?.setImageResource(if (viaVisible) R.drawable.ic_action_navigation_unfold_less_white else R.drawable.ic_action_navigation_unfold_more_white)
viaCard.visibility = if (viaVisible) VISIBLE else GONE
}

Expand All @@ -271,7 +248,7 @@ class DirectionsFragment : TransportrFragment() {
}
fromLocation.setSearching()
toLocation.requestFocus()
viewModel.locationLiveData.observe(this, Observer { location ->
viewModel.locationLiveData.observe(viewLifecycleOwner, Observer { location ->
viewModel.setFromLocation(location)
viewModel.search()
viewModel.locationLiveData.removeObservers(this@DirectionsFragment)
Expand All @@ -283,12 +260,15 @@ class DirectionsFragment : TransportrFragment() {
favIcon.visibility = INVISIBLE
} else {
favIcon.visibility = VISIBLE
if (isFav) {
favIcon.setImageResource(R.drawable.ic_action_star)
} else {
favIcon.setImageResource(R.drawable.ic_action_star_empty)
}
favIcon.setImageResource(if (isFav) R.drawable.ic_action_star else R.drawable.ic_action_star_empty)
val tooltip = getString(if (isFav) R.string.action_unfav_trip else R.string.action_fav_trip)
favIcon.contentDescription = tooltip
TooltipCompat.setTooltipText(favIcon, tooltip)
}
}

private fun onProductsChanged(products: EnumSet<Product>) {
productsMarked.visibility = if (Product.ALL == products) GONE else VISIBLE
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ public void onTimeAndDateSet(Calendar calendar) {
search();
}

@Override
public void onDepartureOrArrivalSet(boolean departure) {
setIsDeparture(departure);
search();
}

void resetCalender() {
now.setValue(true);
search();
Expand Down Expand Up @@ -164,11 +170,6 @@ LiveData<Boolean> getIsDeparture() {
return isDeparture;
}

void toggleDeparture() {
isDeparture.setValue(!(isDeparture.getValue() == null || isDeparture.getValue()));
search();
}

void setIsDeparture(boolean departure) {
isDeparture.setValue(departure);
search();
Expand All @@ -182,6 +183,8 @@ void setIsExpanded(boolean expanded) {
isExpanded.setValue(expanded);
}

void toggleIsExpanded() { isExpanded.setValue(!isExpanded.getValue());}

MutableLiveData<Boolean> isFavTrip() {
return tripsRepository.isFavTrip();
}
Expand Down Expand Up @@ -265,5 +268,4 @@ LiveData<Pair<String,String>> getQueryPTEError() {
LiveData<String> getQueryMoreError() {
return tripsRepository.getQueryMoreError();
}

}
37 changes: 33 additions & 4 deletions app/src/main/java/de/grobox/transportr/ui/TimeDateFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import android.os.Bundle
import android.text.format.DateFormat.getDateFormat
import android.view.LayoutInflater
import android.view.View
import android.view.View.GONE
import android.view.ViewGroup
import android.widget.DatePicker
import android.widget.TimePicker
Expand All @@ -38,19 +39,22 @@ import java.util.Calendar.*
class TimeDateFragment : DialogFragment(), OnDateSetListener, OnTimeChangedListener {

private var listener: TimeDateListener? = null
private var departure: Boolean? = null
private lateinit var calendar: Calendar

companion object {
@JvmField
val TAG: String = TimeDateFragment::class.java.simpleName
private val CALENDAR = "calendar"
private val DEPARTURE = "departure"

@JvmStatic
fun newInstance(calendar: Calendar): TimeDateFragment {
fun newInstance(calendar: Calendar, departure: Boolean? = null): TimeDateFragment {
val f = TimeDateFragment()

val args = Bundle()
args.putSerializable(CALENDAR, calendar)
args.putSerializable(DEPARTURE, departure)
f.arguments = args

return f
Expand All @@ -60,12 +64,14 @@ class TimeDateFragment : DialogFragment(), OnDateSetListener, OnTimeChangedListe
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

calendar = if (savedInstanceState == null) {
if (savedInstanceState == null) {
arguments?.let {
it.getSerializable(CALENDAR) as Calendar
calendar = it.getSerializable(CALENDAR) as Calendar
departure = it.getSerializable(DEPARTURE) as Boolean?
} ?: throw IllegalArgumentException("Arguments missing")
} else {
savedInstanceState.getSerializable(CALENDAR) as Calendar
calendar = savedInstanceState.getSerializable(CALENDAR) as Calendar
departure = savedInstanceState.getSerializable(DEPARTURE) as Boolean?
}
}

Expand Down Expand Up @@ -97,13 +103,34 @@ class TimeDateFragment : DialogFragment(), OnDateSetListener, OnTimeChangedListe
showDate(calendar)
}

// Departure or Arrival
departure?.let {
departureButton.isChecked = it
arrivalButton.isChecked = !it

departureButton.setOnClickListener {
departure = departureButton.isChecked
arrivalButton.isChecked = !departure!!
}

arrivalButton.setOnClickListener {
departure = !arrivalButton.isChecked
departureButton.isChecked = departure!!
}
} ?: run {
departureButton.visibility = GONE
arrivalButton.visibility = GONE
}

// Buttons
okButton.setOnClickListener {
listener?.onTimeAndDateSet(calendar)
departure?.let { listener?.onDepartureOrArrivalSet(it) }
dismiss()
}
nowButton.setOnClickListener {
listener?.onTimeAndDateSet(Calendar.getInstance())
departure?.let { listener?.onDepartureOrArrivalSet(it) }
dismiss()
}
cancelButton.setOnClickListener {
Expand All @@ -114,6 +141,7 @@ class TimeDateFragment : DialogFragment(), OnDateSetListener, OnTimeChangedListe
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putSerializable(CALENDAR, calendar)
outState.putSerializable(DEPARTURE, departure)
}

override fun onTimeChanged(timePicker: TimePicker, hourOfDay: Int, minute: Int) {
Expand Down Expand Up @@ -155,6 +183,7 @@ class TimeDateFragment : DialogFragment(), OnDateSetListener, OnTimeChangedListe

interface TimeDateListener {
fun onTimeAndDateSet(calendar: Calendar)
fun onDepartureOrArrivalSet(departure: Boolean) { }
}

}
7 changes: 7 additions & 0 deletions app/src/main/res/drawable/ic_action_dot.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<size
android:width="8dp"
android:height="8dp"/>
</shape>
9 changes: 0 additions & 9 deletions app/src/main/res/drawable/ic_trip_arrival.xml

This file was deleted.

9 changes: 0 additions & 9 deletions app/src/main/res/drawable/ic_trip_departure.xml

This file was deleted.

Loading

0 comments on commit 046f499

Please sign in to comment.