Skip to content

Commit

Permalink
Add headers in list
Browse files Browse the repository at this point in the history
  • Loading branch information
corenting committed Nov 14, 2018
1 parent 878fd6b commit 7c6d985
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 34 deletions.
1 change: 0 additions & 1 deletion .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions app/src/main/java/fr/corenting/traficparis/models/Enums.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ enum class TransportType {

enum class TrafficState {
OK, TRAFFIC, WORK
}

enum class TitleType {
WORK, TRAFFIC, OK
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package fr.corenting.traficparis.models

data class ListTitle(val title: TitleType)
62 changes: 51 additions & 11 deletions app/src/main/java/fr/corenting/traficparis/ui/main/MainAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,59 @@ import androidx.recyclerview.widget.RecyclerView
import fr.corenting.traficparis.R
import kotlinx.android.synthetic.main.list_item.view.*
import fr.corenting.traficparis.models.ListItem
import fr.corenting.traficparis.models.ListTitle
import fr.corenting.traficparis.models.TitleType
import fr.corenting.traficparis.utils.DrawableUtils
import kotlinx.android.synthetic.main.list_title.view.*


class MainAdapter(private val context: Context) :
androidx.recyclerview.widget.RecyclerView.Adapter<MainAdapter.ResultViewHolder>() {
androidx.recyclerview.widget.RecyclerView.Adapter<RecyclerView.ViewHolder>() {

companion object {
const val TYPE_TITLE = 0
const val TYPE_ITEM = 1
}

private lateinit var recyclerView: RecyclerView
private var dataSet = mutableListOf<ListItem>()
private var dataSet = mutableListOf<Any>()

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ResultViewHolder {
val v = LayoutInflater.from(parent.context)
.inflate(R.layout.list_item, parent, false)
return ResultViewHolder(v)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
if (viewType == TYPE_TITLE) {
val v = LayoutInflater.from(parent.context)
.inflate(R.layout.list_title, parent, false)
return HeaderViewHolder(v)
}
else {
val v = LayoutInflater.from(parent.context)
.inflate(R.layout.list_item, parent, false)
return ItemViewHolder(v)
}
}

override fun getItemCount(): Int {
return dataSet.size
}

override fun onBindViewHolder(holder: ResultViewHolder, position: Int) {
override fun getItemViewType(position: Int): Int {
val currentResult = dataSet[position]
bindHolderContent(holder.itemView, currentResult)
if (currentResult is ListTitle) {
return TYPE_TITLE
}
return TYPE_ITEM
}

private fun bindHolderContent(itemView: View, currentResult: ListItem) {
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val currentResult = dataSet[position]
if (getItemViewType(position) == TYPE_ITEM) {
bindItemHolder(holder.itemView, currentResult as ListItem)
}
else {
bindTitleHolder(holder.itemView, currentResult as ListTitle)
}
}

private fun bindItemHolder(itemView: View, currentResult: ListItem) {
itemView.titleTextView.text = context.getString(R.string.line_title, currentResult.lineName,
currentResult.title)
itemView.subtitleTextView.text = currentResult.stateDescription
Expand All @@ -50,6 +78,15 @@ class MainAdapter(private val context: Context) :
}
}

private fun bindTitleHolder(itemView: View, currentResult: ListTitle) {
val title:String = when {
currentResult.title == TitleType.OK -> context.getString(R.string.normal_traffic)
currentResult.title == TitleType.WORK -> context.getString(R.string.work)
else -> context.getString(R.string.issues)
}
itemView.headerTitleTextView.text = title
}

override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
super.onAttachedToRecyclerView(recyclerView)
this.recyclerView = recyclerView
Expand All @@ -65,7 +102,7 @@ class MainAdapter(private val context: Context) :
}
}

fun addItems(items: List<ListItem>) {
fun addItems(items: List<Any>) {
recyclerView.post {
recyclerView.isEnabled = false
dataSet.addAll(items)
Expand All @@ -74,6 +111,9 @@ class MainAdapter(private val context: Context) :
}
}

class ResultViewHolder(itemView: View) :
class ItemViewHolder(itemView: View) :
androidx.recyclerview.widget.RecyclerView.ViewHolder(itemView)

class HeaderViewHolder(itemView: View) :
androidx.recyclerview.widget.RecyclerView.ViewHolder(itemView)
}
48 changes: 26 additions & 22 deletions app/src/main/java/fr/corenting/traficparis/utils/ResultsSorting.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,51 @@ package fr.corenting.traficparis.utils
import fr.corenting.traficparis.models.*

object ResultsSorting {
fun convertApiResultsToListItems(results: ApiResponseResults): List<ListItem> {
val retList = mutableListOf<ListItem>()
fun convertApiResultsToListItems(results: ApiResponseResults): List<Any> {
val retList = mutableListOf<Any>()

// First, add lines with traffic issues
retList.add(ListTitle(TitleType.TRAFFIC))
retList.addAll(results.metros
.filter { it.slug != "normal" && it.slug != "normal_trav" }
.map { convertFromApiModel(TransportType.METRO, it) })
.filter { it.slug != "normal" && it.slug != "normal_trav" }
.map { convertFromApiModel(TransportType.METRO, it) })
retList.addAll(results.rers
.filter { it.slug != "normal" && it.slug != "normal_trav" }
.map { convertFromApiModel(TransportType.RER, it) })
.filter { it.slug != "normal" && it.slug != "normal_trav" }
.map { convertFromApiModel(TransportType.RER, it) })
retList.addAll(results.tramways
.filter { it.slug != "normal" && it.slug != "normal_trav" }
.map { convertFromApiModel(TransportType.TRAM, it) })
.filter { it.slug != "normal" && it.slug != "normal_trav" }
.map { convertFromApiModel(TransportType.TRAM, it) })

// Then, add lines with work
retList.add(ListTitle(TitleType.WORK))
retList.addAll(results.metros
.filter { it.slug == "normal_trav" }
.map { convertFromApiModel(TransportType.METRO, it) })
.filter { it.slug == "normal_trav" }
.map { convertFromApiModel(TransportType.METRO, it) })
retList.addAll(results.rers
.filter { it.slug == "normal_trav" }
.map { convertFromApiModel(TransportType.RER, it) })
.filter { it.slug == "normal_trav" }
.map { convertFromApiModel(TransportType.RER, it) })
retList.addAll(results.tramways
.filter { it.slug == "normal_trav" }
.map { convertFromApiModel(TransportType.TRAM, it) })
.filter { it.slug == "normal_trav" }
.map { convertFromApiModel(TransportType.TRAM, it) })

// Then, add working lines
retList.add(ListTitle(TitleType.OK))
retList.addAll(results.metros
.filter { it.slug == "normal" }
.map { convertFromApiModel(TransportType.METRO, it) })
.filter { it.slug == "normal" }
.map { convertFromApiModel(TransportType.METRO, it) })
retList.addAll(results.rers
.filter { it.slug == "normal" }
.map { convertFromApiModel(TransportType.RER, it) })
.filter { it.slug == "normal" }
.map { convertFromApiModel(TransportType.RER, it) })
retList.addAll(results.tramways
.filter { it.slug == "normal" }
.map { convertFromApiModel(TransportType.TRAM, it) })
.filter { it.slug == "normal" }
.map { convertFromApiModel(TransportType.TRAM, it) })

return retList
}

private fun convertFromApiModel(type: TransportType,
apiItem: ApiResponseItem
private fun convertFromApiModel(
type: TransportType,
apiItem: ApiResponseItem
): ListItem {

val lineState: TrafficState = when {
Expand Down
24 changes: 24 additions & 0 deletions app/src/main/res/layout/list_title.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/headerTitleTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Title"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
4 changes: 4 additions & 0 deletions app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@
<string name="line_title">Ligne %1$s - %2$s</string>
<string name="about_text"><![CDATA[Application par corenting.<br><a href="http://www.corenting.fr">Site web et informations de contact</a><br><br><b>Cette application n\'est pas officielle et\n n\'est approuvée ou liée à la RATP ou à la SNCF.</b><br><br>Les pictogrammes des lignes de métro et RER appartiennent à la RATP, et sont soumis à la licence RATP : <a href="https://data.ratp.fr/page/cgu_ratp/">https://data.ratp.fr/page/cgu_ratp/</a><br><br>M et T sont des marques de la RATP. Sous réserve de disponibilité, les logotypes de la RATP sont téléchargeables et exploitables gratuitement à partir du site <a href="http://data.ratp.fr">http://data.ratp.fr</a> dans les conditions visées au sein de celui-ci.<br><br>Version :&nbsp;]]></string>
<string name="about">À propos</string>
<string name="download_error">Erreur lors du téléchargement du contenu</string>
<string name="normal_traffic">Trafic normal</string>
<string name="work">Travaux</string>
<string name="issues">Pertubations</string>
</resources>
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@

<string name="about_text"><![CDATA[Application created by corenting.<br><a href="http://www.corenting.fr">Website and contact informations</a><br><br><b>This application is not official and\n is not endorsed or approved by the RATP or the SNCF.</b><br><br>The pictograms of the metro and RER lines belong to the RATP, and are subject to the RATP licence : <a href="https://data.ratp.fr/page/cgu_ratp/">https://data.ratp.fr/page/cgu_ratp/</a><br><br>M et T are RATP brands. Subject to availability, RATP\'s logos can be downloaded and used free of charge from the <a href="http://data.ratp.fr">http://data.ratp.fr</a> website under the conditions referred to therein.<br><br>Version :&nbsp;]]></string>
<string name="download_error">Error while downloading content</string>
<string name="normal_traffic">Normal traffic</string>
<string name="work">Work</string>
<string name="issues">Issues</string>
</resources>

0 comments on commit 7c6d985

Please sign in to comment.