ref: dd2ee96d47a98c46ac7ca5a5631e01b8b40a4bea
app/src/main/java/ml/adamsprogs/bimba/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 |
package ml.adamsprogs.bimba import android.support.design.widget.* import android.support.v7.app.AppCompatActivity import android.support.v7.widget.Toolbar import android.support.v4.app.* import android.support.v4.view.ViewPager import android.os.Bundle import android.support.v4.content.res.ResourcesCompat import android.support.v4.view.PagerAdapter import android.view.* import ml.adamsprogs.bimba.models.* import android.support.v7.widget.* import java.util.* import kotlin.collections.ArrayList import kotlin.collections.HashMap class StopActivity : AppCompatActivity() { //todo refresh private lateinit var stopId: String private var timetableType = "departure" private var sectionsPagerAdapter: SectionsPagerAdapter? = null private var viewPager: ViewPager? = null private lateinit var timetable: Timetable override fun onCreate(savedInstanceState: Bundle?) { //todo select current mode super.onCreate(savedInstanceState) setContentView(R.layout.activity_stop) stopId = intent.getStringExtra("stop") val toolbar = findViewById(R.id.toolbar) as Toolbar setSupportActionBar(toolbar) /*todo when Internet connection exists -> download vm else -> timetable */ timetable = Timetable(this) supportActionBar?.title = timetable.getStopName(stopId) ?: "Stop" viewPager = findViewById(R.id.container) as ViewPager val tabLayout = findViewById(R.id.tabs) as TabLayout sectionsPagerAdapter = SectionsPagerAdapter(supportFragmentManager, createDepartures()) viewPager!!.adapter = sectionsPagerAdapter viewPager!!.addOnPageChangeListener(TabLayout.TabLayoutOnPageChangeListener(tabLayout)) tabLayout.addOnTabSelectedListener(TabLayout.ViewPagerOnTabSelectedListener(viewPager)) val fab = findViewById(R.id.fab) as FloatingActionButton fab.setOnClickListener { view -> Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show() //todo favourites } } fun createDepartures(): HashMap<String, ArrayList<Departure>> { val departures = timetable.getStopDepartures(stopId) val moreDepartures = timetable.getStopDepartures(stopId) val rolledDepartures = HashMap<String, ArrayList<Departure>>() for ((_, tomorrowDepartures) in moreDepartures!!) { tomorrowDepartures.forEach{it.tomorrow = true} } for ((mode, _) in departures!!) { rolledDepartures[mode] = (departures[mode] as ArrayList<Departure> + moreDepartures[mode] as ArrayList<Departure>) as ArrayList<Departure> rolledDepartures[mode] = filterDepartures(rolledDepartures[mode]) } return rolledDepartures } fun filterDepartures(departures: List<Departure>?): ArrayList<Departure> { val filtered = ArrayList<Departure>() val lines = HashMap<String, Int>() val now = Calendar.getInstance() for (departure in departures!!) { val time = Calendar.getInstance() time.set(Calendar.HOUR_OF_DAY, Integer.parseInt(departure.time.split(":")[0])) time.set(Calendar.MINUTE, Integer.parseInt(departure.time.split(":")[1])) time.set(Calendar.SECOND, 0) time.set(Calendar.MILLISECOND, 0) if (departure.tomorrow) time.add(Calendar.DAY_OF_MONTH, 1) var lineExistedTimes = lines[departure.line] if (now.before(time) && lineExistedTimes ?: 0 < 3) { lineExistedTimes = (lineExistedTimes ?: 0) + 1 lines[departure.line] = lineExistedTimes filtered.add(departure) } } return filtered } 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?.changeDepartures(timetable.getStopDepartures(stopId)!!) sectionsPagerAdapter?.notifyDataSetChanged() } else { timetableType = "departure" item.icon = (ResourcesCompat.getDrawable(resources, R.drawable.ic_timetable_full, this.theme)) sectionsPagerAdapter?.changeDepartures(createDepartures()) sectionsPagerAdapter?.notifyDataSetChanged() } return true } return super.onOptionsItemSelected(item) } 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) as RecyclerView val dividerItemDecoration = DividerItemDecoration(departuresList.context, layoutManager.orientation) departuresList.addItemDecoration(dividerItemDecoration) val adapter = DeparturesAdapter(activity, arguments.getStringArrayList("departures").map { fromString(it) }) departuresList.adapter = adapter departuresList.layoutManager = layoutManager return rootView } companion object { private val ARG_SECTION_NUMBER = "section_number" fun newInstance(sectionNumber: Int, stopId: String, departures: ArrayList<Departure>?): PlaceholderFragment { val fragment = PlaceholderFragment() val args = Bundle() args.putInt(ARG_SECTION_NUMBER, sectionNumber) args.putString("stop", stopId) val d = ArrayList<String>() departures?.mapTo(d) { it.toString() } args.putStringArrayList("departures", d) fragment.arguments = args return fragment } } } inner class SectionsPagerAdapter(fm: FragmentManager, var departures: HashMap<String, ArrayList<Departure>>) : FragmentStatePagerAdapter(fm) { override fun getItemPosition(obj: Any?): Int { return PagerAdapter.POSITION_NONE } override fun getItem(position: Int): Fragment { var mode: String? = null when (position) { 0 -> mode = "workdays" 1 -> mode = "saturdays" 2 -> mode = "sundays" } return PlaceholderFragment.newInstance(position + 1, stopId, departures[mode]) } fun changeDepartures(departures: HashMap<String, ArrayList<Departure>>) { this.departures = departures } override fun getCount() = 3 } } |