ref: 2b948f4e23bba5facf231aae48cb1185adacd702
app/src/main/java/ml/adamsprogs/bimba/activities/DashActivity.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 android.content.* import android.os.* import android.support.design.widget.Snackbar import android.support.v7.app.* import android.text.Html import android.view.View import com.arlib.floatingsearchview.FloatingSearchView import com.arlib.floatingsearchview.suggestions.model.SearchSuggestion import ml.adamsprogs.bimba.models.* import kotlin.concurrent.thread import android.app.Activity import android.support.v4.widget.* import android.support.v7.widget.* import android.util.Log import android.view.Menu import android.view.MenuItem import android.view.inputmethod.InputMethodManager import ml.adamsprogs.bimba.* //todo refresh every 15s class DashActivity : AppCompatActivity(), MessageReceiver.OnTimetableDownloadListener, SwipeRefreshLayout.OnRefreshListener, FavouritesAdapter.OnMenuItemClickListener { val context: Context = this val receiver = MessageReceiver() lateinit var timetable: Timetable var stops: ArrayList<StopSuggestion>? = null lateinit var swipeRefreshLayout: SwipeRefreshLayout lateinit var favouritesList: RecyclerView lateinit var searchView: FloatingSearchView lateinit var favourites: FavouriteStorage override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_dash) AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO) val toolbar = findViewById(R.id.toolbar) as Toolbar setSupportActionBar(toolbar) supportActionBar?.title = getString(R.string.merge_favourites) prepareSwipeLayout() prepareOnDownloadListener() startDownloaderService() getStops() prepareFavourites() searchView = findViewById(R.id.search_view) as FloatingSearchView searchView.setOnFocusChangeListener(object : FloatingSearchView.OnFocusChangeListener { override fun onFocus() { swipeRefreshLayout.isEnabled = false favouritesList.visibility = View.GONE thread { val newStops = stops!!.filter { deAccent(it.body.split("\n")[0]).contains(deAccent(searchView.query), true) } runOnUiThread { searchView.swapSuggestions(newStops) } } } override fun onFocusCleared() { swipeRefreshLayout.isEnabled = true favouritesList.visibility = View.VISIBLE } }) searchView.setOnQueryChangeListener({ oldQuery, newQuery -> if (oldQuery != "" && newQuery == "") searchView.clearSuggestions() thread { val newStops = stops!!.filter { deAccent(it.body.split("\n")[0]).contains(deAccent(newQuery), true) } runOnUiThread { searchView.swapSuggestions(newStops) } } }) searchView.setOnSearchListener(object : FloatingSearchView.OnSearchListener { override fun onSuggestionClicked(searchSuggestion: SearchSuggestion) { val imm = context.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager var view = (context as DashActivity).currentFocus if (view == null) { view = View(context) } imm.hideSoftInputFromWindow(view.windowToken, 0) intent = Intent(context, StopActivity::class.java) searchSuggestion as StopSuggestion intent.putExtra("stopId", searchSuggestion.id) intent.putExtra("stopSymbol", searchSuggestion.symbol) startActivity(intent) } override fun onSearchAction(query: String) { } }) searchView.setOnBindSuggestionCallback { _, _, textView, item, _ -> val suggestion = item as StopSuggestion val text = suggestion.body.split("\n") val t = "<small><font color=\"#a0a0a0\">" + text[1] + "</font></small>" if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { textView.text = Html.fromHtml(text[0] + "<br/>" + t, Html.FROM_HTML_MODE_LEGACY) } else { @Suppress("DEPRECATION") textView.text = Html.fromHtml(text[0] + "<br/>" + t) } } //todo searchView.attachNavigationDrawerToMenuButton(mDrawerLayout) } private fun prepareFavourites() { favourites = FavouriteStorage.getFavouriteStorage(context) val layoutManager = LinearLayoutManager(context) favouritesList = findViewById(R.id.favouritesList) as RecyclerView favouritesList.adapter = FavouritesAdapter(context, favourites.favouritesList, this) favouritesList.layoutManager = layoutManager } private fun getStops() { timetable = Timetable.getTimetable(this) stops = timetable.getStops() } private fun prepareOnDownloadListener() { val filter = IntentFilter("ml.adamsprogs.bimba.timetableDownloaded") filter.addCategory(Intent.CATEGORY_DEFAULT) registerReceiver(receiver, filter) receiver.addOnTimetableDownloadListener(context as MessageReceiver.OnTimetableDownloadListener) } private fun startDownloaderService() { startService(Intent(context, TimetableDownloader::class.java)) } private fun prepareSwipeLayout() { swipeRefreshLayout = findViewById(R.id.swipeRefreshLayout) as SwipeRefreshLayout swipeRefreshLayout.isEnabled = true swipeRefreshLayout.setOnRefreshListener(this) swipeRefreshLayout.setColorSchemeResources(R.color.colorAccent, R.color.colorPrimary) } override fun onRefresh() { swipeRefreshLayout.isRefreshing = true startDownloaderService() } override fun onBackPressed() { if (!searchView.setSearchFocused(false)) { super.onBackPressed() } } override fun onResume() { super.onResume() (favouritesList.adapter as FavouritesAdapter).favourites = favourites.favouritesList favouritesList.adapter.notifyDataSetChanged() } override fun onDestroy() { super.onDestroy() receiver.removeOnTimetableDownloadListener(context as MessageReceiver.OnTimetableDownloadListener) unregisterReceiver(receiver) } override fun onCreateOptionsMenu(menu: Menu): Boolean { menuInflater.inflate(R.menu.menu_favourite_merge, menu) return true } override fun onOptionsItemSelected(item: MenuItem): Boolean { val id = item.itemId if (id == R.id.action_merge) { val names = (favouritesList.adapter as FavouritesAdapter).selectedNames favourites.merge(names) (favouritesList.adapter as FavouritesAdapter).favourites = favourites.favouritesList favouritesList.adapter.notifyDataSetChanged() (favouritesList.adapter as FavouritesAdapter).stopSelecting(names[0]) } return super.onOptionsItemSelected(item) } fun deAccent(str: String): String { var result = str.replace('ę', 'e') result = result.replace('ó', 'o') result = result.replace('ą', 'a') result = result.replace('ś', 's') result = result.replace('ł', 'l') result = result.replace('ż', 'ż') result = result.replace('ź', 'ź') result = result.replace('ć', 'ć') result = result.replace('ń', 'n') return result } override fun onTimetableDownload(result: String?) { Log.i("Refresh", "downloaded: $result") val message: String when (result) { "downloaded" -> message = getString(R.string.timetable_downloaded) "no connectivity" -> message = getString(R.string.no_connectivity) "up-to-date" -> message = getString(R.string.timetable_up_to_date) "validity failed" -> message = getString(R.string.validity_failed) else -> message = getString(R.string.error_try_later) } timetable.refresh() stops = timetable.getStops() Snackbar.make(swipeRefreshLayout, message, Snackbar.LENGTH_LONG).show() swipeRefreshLayout.isRefreshing = false } override fun edit(name: String): Boolean { val intent = Intent(this, EditFavouriteActivity::class.java) intent.putExtra("favourite", favourites.favourites[name]) startActivity(intent) (favouritesList.adapter as FavouritesAdapter).favourites = favourites.favouritesList favouritesList.adapter.notifyDataSetChanged() return true } override fun delete(name: String): Boolean { favourites.delete(name) (favouritesList.adapter as FavouritesAdapter).favourites = favourites.favouritesList favouritesList.adapter.notifyDataSetChanged() return true } } |