ref: ccd2377d3e1f2491dccc56069fadf0f8ca163f95
app/src/main/java/ml/adamsprogs/bimba/activities/StopActivity.kt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
package ml.adamsprogs.bimba.activities import java.util.* import kotlin.collections.* import android.content.* import android.os.Bundle import android.view.* import android.support.design.widget.* import android.support.v7.app.AppCompatActivity import android.support.v7.widget.* import android.support.v4.app.* import android.support.v4.view.* import android.support.v4.content.res.ResourcesCompat import ml.adamsprogs.bimba.models.* import ml.adamsprogs.bimba.* import kotlin.concurrent.thread import kotlinx.android.synthetic.main.activity_stop.* import ml.adamsprogs.bimba.datasources.TimetableDownloader import ml.adamsprogs.bimba.datasources.VmClient import ml.adamsprogs.bimba.gtfs.AgencyAndId import android.support.v4.view.ViewPager class StopActivity : AppCompatActivity(), MessageReceiver.OnTimetableDownloadListener, MessageReceiver.OnVmListener, Favourite.OnVmPreparedListener { companion object { const val EXTRA_STOP_ID = "stopId" const val EXTRA_FAVOURITE = "favourite" const val SOURCE_TYPE = "sourceType" const val SOURCE_TYPE_STOP = "stop" const val SOURCE_TYPE_FAV = "favourite" } private var stopSegment: StopSegment? = null private var favourite: Favourite? = null private var timetableType = "departure" private var sectionsPagerAdapter: SectionsPagerAdapter? = null private var viewPager: ViewPager? = null private lateinit var timetable: Timetable private lateinit var tabLayout: TabLayout private val context = this private val receiver = MessageReceiver.getMessageReceiver() private val vmDepartures = HashMap<Plate.ID, Set<Departure>>() private var hasDepartures = false private lateinit var sourceType: String override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_stop) timetable = Timetable.getTimetable() sourceType = intent.getStringExtra(SOURCE_TYPE) val toolbar = findViewById<Toolbar>(R.id.toolbar) setSupportActionBar(toolbar) when (sourceType) { SOURCE_TYPE_STOP -> { stopSegment = StopSegment(intent.getSerializableExtra(EXTRA_STOP_ID) as AgencyAndId, null) stopSegment!!.fillPlates() supportActionBar?.title = timetable.getStopName(stopSegment!!.stop) } SOURCE_TYPE_FAV -> { favourite = intent.getParcelableExtra(EXTRA_FAVOURITE) supportActionBar?.title = favourite!!.name favourite!!.addOnVmPreparedListener(this) } } prepareOnDownloadListener() viewPager = container tabLayout = tabs sectionsPagerAdapter = SectionsPagerAdapter(supportFragmentManager, HashMap<AgencyAndId, ArrayList<Departure>>()) /*thread { if (sourceType == SOURCE_TYPE_STOP) { sectionsPagerAdapter!!.departures = Departure.createDepartures(stopSegment!!.stop) } else { sectionsPagerAdapter!!.departures = favourite!!.allDepartures() } runOnUiThread { sectionsPagerAdapter?.notifyDataSetChanged() } }*/ viewPager!!.adapter = sectionsPagerAdapter viewPager!!.addOnPageChangeListener(TabLayout.TabLayoutOnPageChangeListener(tabLayout)) tabLayout.addOnTabSelectedListener(TabLayout.ViewPagerOnTabSelectedListener(viewPager)) selectTodayPage() showFab() } private fun getFavouriteDepartures() { thread { refreshAdapter(favourite!!.allDepartures()) } } private fun refreshAdapter(departures: Map<AgencyAndId, List<Departure>>) { tabLayout.removeAllTabs() sectionsPagerAdapter?.notifyDataSetChanged() departures.keys.sortedBy { timetable.calendarToMode(AgencyAndId(it.id)).sorted()[0] }.forEach { val tab = tabLayout.newTab() tab.text = timetable.calendarToMode(it) .joinToString { resources.getStringArray(R.array.daysOfWeekShort)[it] } tabLayout.addTab(tab) sectionsPagerAdapter?.notifyDataSetChanged() } println("refreshing:") departures[AgencyAndId("4")]?.forEach { println("${it.lineText} -> ${it.direction} @ ${it.time}") } sectionsPagerAdapter?.departures = departures try { sectionsPagerAdapter?.notifyDataSetChanged() } catch (e: Exception) { runOnUiThread { sectionsPagerAdapter?.notifyDataSetChanged() } } selectTodayPage() } override fun onVmPrepared() { getFavouriteDepartures() } private fun showFab() { if (sourceType == SOURCE_TYPE_FAV) return val stopSymbol = timetable.getStopCode(stopSegment!!.stop) val favourites = FavouriteStorage.getFavouriteStorage(context) if (!favourites.has(stopSymbol)) { fab.setImageDrawable(ResourcesCompat.getDrawable(context.resources, R.drawable.ic_favourite_empty, this.theme)) } fab.setOnClickListener { if (!favourites.has(stopSymbol)) { val items = HashSet<Plate>() timetable.getTripsForStop(stopSegment!!.stop).values.forEach { val o = Plate(Plate.ID(it.routeId, stopSegment!!.stop, it.headsign), null) items.add(o) } favourites.add(stopSymbol, items) fab.setImageDrawable(ResourcesCompat.getDrawable(context.resources, R.drawable.ic_favourite, this.theme)) } else { Snackbar.make(it, getString(R.string.stop_already_fav), Snackbar.LENGTH_LONG) .setAction("Action", null).show() } } } private fun prepareOnDownloadListener() { val filter = IntentFilter(TimetableDownloader.ACTION_DOWNLOADED) filter.addAction(VmClient.ACTION_READY) filter.addCategory(Intent.CATEGORY_DEFAULT) registerReceiver(receiver, filter) receiver.addOnTimetableDownloadListener(context) if (sourceType == SOURCE_TYPE_STOP) { receiver.addOnVmListener(context) val intent = Intent(this, VmClient::class.java) intent.putExtra("stop", stopSegment) intent.action = "request" startService(intent) } else favourite!!.registerOnVm(receiver, context) } override fun onVm(vmDepartures: Set<Departure>?, plateId: Plate.ID) { if (vmDepartures == null && this.vmDepartures.isEmpty() && hasDepartures) { return } if (timetableType == "departure" && stopSegment!!.contains(plateId)) { if (vmDepartures != null) this.vmDepartures[plateId] = vmDepartures else this.vmDepartures.remove(plateId) val departures = HashMap<AgencyAndId, List<Departure>>() if (this.vmDepartures.isNotEmpty()) { departures[timetable.getServiceForToday()] = this.vmDepartures.flatMap { it.value }.sortedBy { it.timeTill() } refreshAdapter(departures) } else { refreshAdapter(Departure.createDepartures(stopSegment!!.stop)) hasDepartures = true } } } override fun onTimetableDownload(result: String?) { val message: String = when (result) { TimetableDownloader.RESULT_DOWNLOADED -> getString(R.string.refreshing_cache) TimetableDownloader.RESULT_NO_CONNECTIVITY -> getString(R.string.no_connectivity) TimetableDownloader.RESULT_UP_TO_DATE -> getString(R.string.timetable_up_to_date) TimetableDownloader.RESULT_FINISHED -> getString(R.string.timetable_downloaded) else -> getString(R.string.error_try_later) } try { Snackbar.make(findViewById(R.id.drawer_layout), message, Snackbar.LENGTH_LONG).show() } catch (e: IllegalArgumentException) { } //todo refresh } private fun selectTodayPage() { //fixme does not work val today = (Calendar.getInstance().get(Calendar.DAY_OF_WEEK) - 1) % 7 tabLayout.getTabAt(sectionsPagerAdapter!!.todayTab(today)) } override fun onCreateOptionsMenu(menu: Menu): Boolean { menuInflater.inflate(R.menu.menu_stop, menu) return true } override fun onOptionsItemSelected(item: MenuItem): Boolean { val id = item.itemId if (id == R.id.action_change_type) { if (timetableType == "departure") { timetableType = "full" item.icon = (ResourcesCompat.getDrawable(resources, R.drawable.ic_timetable_departure, this.theme)) sectionsPagerAdapter?.relativeTime = false if (sourceType == SOURCE_TYPE_STOP) refreshAdapter(timetable.getStopDepartures(stopSegment!!.stop)) else refreshAdapter(favourite!!.fullTimetable()) } else { timetableType = "departure" item.icon = (ResourcesCompat.getDrawable(resources, R.drawable.ic_timetable_full, this.theme)) sectionsPagerAdapter?.relativeTime = true if (sourceType == SOURCE_TYPE_STOP) refreshAdapter(Departure.createDepartures(stopSegment!!.stop)) else refreshAdapter(favourite!!.allDepartures()) } return true } return super.onOptionsItemSelected(item) } override fun onDestroy() { super.onDestroy() receiver.removeOnTimetableDownloadListener(context) if (sourceType == SOURCE_TYPE_STOP) { receiver.removeOnVmListener(context) val intent = Intent(this, VmClient::class.java) intent.putExtra("stop", stopSegment) intent.action = "remove" startService(intent) } else favourite!!.deregisterOnVm(receiver, context) unregisterReceiver(receiver) } class PlaceholderFragment : Fragment() { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { val rootView = inflater.inflate(R.layout.fragment_stop, container, false) val layoutManager = LinearLayoutManager(activity) val departuresList: RecyclerView = rootView.findViewById(R.id.departuresList) departuresList.addItemDecoration(DividerItemDecoration(departuresList.context, layoutManager.orientation)) val departures = arguments?.getStringArrayList("departures")!!.map { Departure.fromString(it) } departuresList.adapter = DeparturesAdapter(activity as Context, departures, arguments?.get("relativeTime") as Boolean) departuresList.layoutManager = layoutManager return rootView } companion object { private const val ARG_SECTION_NUMBER = "section_number" fun newInstance(sectionNumber: Int, departures: List<Departure>, relativeTime: Boolean): PlaceholderFragment { val fragment = PlaceholderFragment() val args = Bundle() args.putInt(ARG_SECTION_NUMBER, sectionNumber) println("newInstance:") departures.forEach { println("${it.lineText} -> ${it.direction} @ ${it.time}") } if (departures.isEmpty()) { val d = ArrayList<String>() departures.mapTo(d) { it.toString() } args.putStringArrayList("departures", d) } else args.putStringArrayList("departures", ArrayList<String>()) args.putBoolean("relativeTime", relativeTime) fragment.arguments = args return fragment } } } inner class SectionsPagerAdapter(fm: FragmentManager, departures: Map<AgencyAndId, List<Departure>>) : FragmentStatePagerAdapter(fm) { //todo swipe var departures: Map<AgencyAndId, List<Departure>> = departures set(value) { println("setting:") value[AgencyAndId("4")]?.forEach { println("${it.lineText} -> ${it.direction} @ ${it.time}") } field = value println("set:") this.departures[AgencyAndId("4")]?.forEach { println("${it.lineText} -> ${it.direction} @ ${it.time}") } } private var modes = ArrayList<AgencyAndId>() init { val tab = tabLayout.newTab() tab.text = getString(R.string.today) tabLayout.addTab(tab) sectionsPagerAdapter?.notifyDataSetChanged() } var relativeTime = true fun todayTab(today: Int): Int { if (modes.isEmpty()) return 0 return modes.indexOf(modes.filter { timetable.calendarToMode(it).contains(today) }[0]) } override fun getItemPosition(obj: Any): Int { return PagerAdapter.POSITION_NONE } override fun getItem(position: Int): Fragment { //fixme doesn't refresh after getting departures/switching. Thinks `departures` is empty. May be connected with swipe println("adapter:") departures[AgencyAndId("4")]?.forEach { println("${it.lineText} -> ${it.direction} @ ${it.time}") } val list = if (departures.isEmpty()) ArrayList() else departures[modes[position]]!! return PlaceholderFragment.newInstance(position + 1, list, relativeTime) } override fun getCount() = if (departures.isEmpty()) 1 else modes.size } } |