|
1 | 1 | package com.stop.ui.route
|
2 | 2 |
|
3 |
| -import android.graphics.Rect |
4 |
| -import android.view.View |
| 3 | +import android.graphics.Color |
| 4 | +import androidx.core.content.ContextCompat |
5 | 5 | import androidx.recyclerview.widget.RecyclerView
|
| 6 | +import com.stop.R |
6 | 7 | import com.stop.databinding.RouteItemBinding
|
7 |
| -import com.stop.domain.model.route.tmap.custom.Itinerary |
| 8 | +import com.stop.domain.model.route.tmap.custom.* |
| 9 | +import com.stop.model.route.RouteItem |
| 10 | +import com.stop.model.route.RouteItemType |
8 | 11 |
|
9 | 12 | class RouteViewHolder(
|
10 | 13 | private val binding: RouteItemBinding
|
11 | 14 | ) : RecyclerView.ViewHolder(binding.root) {
|
12 | 15 |
|
13 |
| - private val adapter = TimeLineAdapter() |
| 16 | + private val adapter = RouteDetailAdapter() |
| 17 | + var routeItemColor = 0 |
14 | 18 |
|
15 | 19 | init {
|
16 | 20 | binding.recyclerviewTimeLine.adapter = adapter
|
17 | 21 | binding.recyclerviewTimeLine.setHasFixedSize(true)
|
18 |
| - binding.recyclerviewTimeLine.addItemDecoration(object: RecyclerView.ItemDecoration() { |
19 |
| - override fun getItemOffsets( |
20 |
| - outRect: Rect, |
21 |
| - view: View, |
22 |
| - parent: RecyclerView, |
23 |
| - state: RecyclerView.State |
24 |
| - ) { |
25 |
| - if (parent.getChildAdapterPosition(view) != RecyclerView.NO_POSITION) { |
26 |
| - outRect.set(0, 0, -10, 0) |
27 |
| - } |
28 |
| - } |
29 |
| - }) |
30 | 22 | }
|
31 | 23 |
|
32 | 24 | fun bind(itinerary: Itinerary) {
|
33 | 25 | binding.textViewExpectedRequiredTime.text = secondToHourAndMinute(itinerary.totalTime)
|
| 26 | + val routeItems = mutableListOf<RouteItem>() |
| 27 | + |
| 28 | + itinerary.routes.drop(1).forEachIndexed { index, route -> |
| 29 | + if (route.mode == MoveType.TRANSFER) { |
| 30 | + return@forEachIndexed |
| 31 | + } |
| 32 | + val (typeName, mode) = if (index == itinerary.routes.size - 2) { |
| 33 | + Pair("하차", R.drawable.ic_star_white) |
| 34 | + } else { |
| 35 | + Pair(getTypeName(route), getRouteItemMode(route)) |
| 36 | + } |
| 37 | + |
| 38 | + routeItems.add( |
| 39 | + RouteItem( |
| 40 | + name = route.start.name, |
| 41 | + coordinate = route.start.coordinate, |
| 42 | + mode = mode, |
| 43 | + distance = getRouteItemDistance(route), |
| 44 | + travelTime = route.sectionTime.toInt(), |
| 45 | + lastTime = "", |
| 46 | + beforeColor = getRouteItemColor(route, false), |
| 47 | + currentColor = getRouteItemColor(route, true), |
| 48 | + type = RouteItemType.PATH, |
| 49 | + typeName = typeName, |
| 50 | + ) |
| 51 | + ) |
| 52 | + } |
| 53 | + adapter.submitList(routeItems) |
| 54 | + binding.timeLineContainer.post { |
| 55 | + binding.timeLineContainer.submitList(itinerary.routes) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private fun getRouteItemDistance(route: Route): Double { |
| 60 | + return if (route.mode == MoveType.TRANSFER) { |
| 61 | + val startPoint = android.location.Location("Start") |
| 62 | + val endPoint = android.location.Location("End") |
| 63 | + |
| 64 | + startPoint.latitude = route.start.coordinate.latitude.toDouble() |
| 65 | + startPoint.longitude = route.start.coordinate.longitude.toDouble() |
| 66 | + endPoint.latitude = route.end.coordinate.latitude.toDouble() |
| 67 | + endPoint.longitude = route.end.coordinate.longitude.toDouble() |
| 68 | + startPoint.distanceTo(endPoint).toDouble() |
| 69 | + } else { |
| 70 | + route.distance |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private fun getTypeName(route: Route): String { |
| 75 | + return when (route) { |
| 76 | + is WalkRoute -> "도보" |
| 77 | + is TransportRoute -> getSubwayTypeName(route) |
| 78 | + else -> "" |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private fun getSubwayTypeName(route: TransportRoute): String { |
| 83 | + return when (route.mode) { |
| 84 | + MoveType.SUBWAY -> route.routeInfo.replace("수도권", "") |
| 85 | + MoveType.BUS -> route.routeInfo.split(":")[1] |
| 86 | + else -> route.routeInfo |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private fun getRouteItemColor(route: Route, isCurrent: Boolean): Int { |
| 91 | + return if (isCurrent) { |
| 92 | + when (route) { |
| 93 | + is TransportRoute -> Color.parseColor("#${route.routeColor}") |
| 94 | + is WalkRoute -> ContextCompat.getColor(binding.root.context, R.color.main_yellow) |
| 95 | + else -> ContextCompat.getColor(binding.root.context, R.color.main_lighter_grey) |
| 96 | + } |
| 97 | + } else { |
| 98 | + if (routeItemColor != 0) { |
| 99 | + routeItemColor |
| 100 | + } else { |
| 101 | + getRouteItemColor(route, true) |
| 102 | + } |
| 103 | + } |
| 104 | + } |
34 | 105 |
|
35 |
| - adapter.submitList(itinerary.routes) |
| 106 | + private fun getRouteItemMode(route: Route): Int { |
| 107 | + return when (route.mode) { |
| 108 | + MoveType.WALK, MoveType.TRANSFER -> R.drawable.ic_walk_white |
| 109 | + MoveType.BUS -> R.drawable.ic_bus_white |
| 110 | + MoveType.SUBWAY -> R.drawable.ic_subway_white |
| 111 | + else -> R.drawable.ic_star_white |
| 112 | + } |
36 | 113 | }
|
37 | 114 |
|
38 | 115 | private fun secondToHourAndMinute(second: Int): String {
|
|
0 commit comments