ref: 2b948f4e23bba5facf231aae48cb1185adacd702
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 |
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 android.util.Log import ml.adamsprogs.bimba.models.* import ml.adamsprogs.bimba.* class StopActivity : AppCompatActivity(), MessageReceiver.OnVmListener { private lateinit var stopId: String private lateinit var stopSymbol: String private var timetableType = "departure" private var sectionsPagerAdapter: SectionsPagerAdapter? = null private var viewPager: ViewPager? = null private lateinit var timetable: Timetable private val today = Calendar.getInstance() private lateinit var tabLayout: TabLayout private var timer = Timer() private lateinit var timerTask: TimerTask private val context = this private val receiver = MessageReceiver() private lateinit var sharedPreferences: SharedPreferences override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_stop) stopId = intent.getStringExtra("stopId") stopSymbol = intent.getStringExtra("stopSymbol") val toolbar = findViewById(R.id.toolbar) as Toolbar setSupportActionBar(toolbar) sharedPreferences = this.getSharedPreferences("ml.adamsprogs.bimba.prefs", Context.MODE_PRIVATE) createTimerTask() prepareOnDownloadListener() timetable = Timetable.getTimetable() supportActionBar?.title = timetable.getStopName(stopId) ?: "Stop" viewPager = findViewById(R.id.container) as ViewPager tabLayout = findViewById(R.id.tabs) as TabLayout sectionsPagerAdapter = SectionsPagerAdapter(supportFragmentManager, createDepartures(stopId)) viewPager!!.adapter = sectionsPagerAdapter viewPager!!.addOnPageChangeListener(TabLayout.TabLayoutOnPageChangeListener(tabLayout)) tabLayout.addOnTabSelectedListener(TabLayout.ViewPagerOnTabSelectedListener(viewPager)) selectTodayPage() scheduleRefresh() val fab = findViewById(R.id.fab) as FloatingActionButton val favourites = FavouriteStorage.getFavouriteStorage(context) if (!favourites.has(stopSymbol)) { fab.setImageDrawable(ResourcesCompat.getDrawable(context.resources, R.drawable.ic_favourite_empty, this.theme)) } fab.setOnClickListener { Log.i("FAB", "Click") if (!favourites.has(stopSymbol)) { Log.i("FAB", "Add") val items = ArrayList<HashMap<String, String>>() timetable.getLines(stopId).forEach { val o = HashMap<String, String>() o["stop"] = stopId o["line"] = it items.add(o) } Log.i("FAB", "Say") favourites.add(stopSymbol, items) Log.i("FAB", "Change") 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 createTimerTask() { timerTask = object : TimerTask() { override fun run() { val vmIntent = Intent(context, VmClient::class.java) vmIntent.putExtra("stopId", stopId) vmIntent.putExtra("stopSymbol", stopSymbol) startService(vmIntent) } } } private fun prepareOnDownloadListener() { val filter = IntentFilter("ml.adamsprogs.bimba.departuresCreated") filter.addCategory(Intent.CATEGORY_DEFAULT) registerReceiver(receiver, filter) receiver.addOnVmListener(context as MessageReceiver.OnVmListener) } override fun onVm(departures: HashMap<String, ArrayList<Departure>>) { sectionsPagerAdapter?.departures = departures sectionsPagerAdapter?.notifyDataSetChanged() } private fun selectTodayPage() { when (today.get(Calendar.DAY_OF_WEEK)) { Calendar.SATURDAY -> tabLayout.getTabAt(1)?.select() Calendar.SUNDAY -> tabLayout.getTabAt(2)?.select() else -> tabLayout.getTabAt(0)?.select() } } private fun scheduleRefresh() { timer.cancel() timer = Timer() createTimerTask() timer.scheduleAtFixedRate(timerTask, 0, 15000) } 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?.departures = timetable.getStopDepartures(stopId)!! sectionsPagerAdapter?.relativeTime = false sectionsPagerAdapter?.notifyDataSetChanged() timer.cancel() } else { timetableType = "departure" item.icon = (ResourcesCompat.getDrawable(resources, R.drawable.ic_timetable_full, this.theme)) sectionsPagerAdapter?.departures = createDepartures(stopId) sectionsPagerAdapter?.relativeTime = true sectionsPagerAdapter?.notifyDataSetChanged() scheduleRefresh() } return true } return super.onOptionsItemSelected(item) } override fun onDestroy() { super.onDestroy() receiver.removeOnVmListener(context as MessageReceiver.OnVmListener) unregisterReceiver(receiver) timer.cancel() } 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) }, arguments["relativeTime"] as Boolean) 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>?, relativeTime: Boolean): 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) args.putBoolean("relativeTime", relativeTime) fragment.arguments = args return fragment } } } inner class SectionsPagerAdapter(fm: FragmentManager, var departures: HashMap<String, ArrayList<Departure>>) : FragmentStatePagerAdapter(fm) { var relativeTime = true 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], relativeTime) } override fun getCount() = 3 } } |