|
@@ -5,11 +5,9 @@ import android.app.Activity
|
|
|
import android.content.*
|
|
|
import android.os.*
|
|
|
import android.preference.PreferenceManager.getDefaultSharedPreferences
|
|
|
-import android.support.design.widget.*
|
|
|
-import android.support.v4.widget.*
|
|
|
-import android.support.v7.widget.*
|
|
|
-import android.support.v7.app.*
|
|
|
-import android.text.Html
|
|
|
+import android.text.Editable
|
|
|
+import androidx.appcompat.app.*
|
|
|
+import android.text.TextWatcher
|
|
|
import android.view.*
|
|
|
import android.view.inputmethod.InputMethodManager
|
|
|
import kotlin.collections.ArrayList
|
|
@@ -24,27 +22,30 @@ import ml.adamsprogs.bimba.models.suggestions.*
|
|
|
import ml.adamsprogs.bimba.models.adapters.*
|
|
|
import ml.adamsprogs.bimba.collections.*
|
|
|
|
|
|
-import com.arlib.floatingsearchview.FloatingSearchView
|
|
|
-import com.arlib.floatingsearchview.suggestions.model.SearchSuggestion
|
|
|
+import com.google.android.material.navigation.NavigationView
|
|
|
+import com.google.android.material.snackbar.Snackbar
|
|
|
+import com.mancj.materialsearchbar.MaterialSearchBar
|
|
|
+import com.mancj.materialsearchbar.MaterialSearchBar.BUTTON_BACK
|
|
|
+import com.mancj.materialsearchbar.MaterialSearchBar.BUTTON_NAVIGATION
|
|
|
|
|
|
-//todo<p:1> searchView integration
|
|
|
class DashActivity : AppCompatActivity(), MessageReceiver.OnTimetableDownloadListener,
|
|
|
- FavouritesAdapter.OnMenuItemClickListener, FavouritesAdapter.ViewHolder.OnClickListener, ProviderProxy.OnDeparturesReadyListener {
|
|
|
+ FavouritesAdapter.OnMenuItemClickListener, FavouritesAdapter.ViewHolder.OnClickListener, ProviderProxy.OnDeparturesReadyListener, SuggestionsAdapter.OnSuggestionClickListener {
|
|
|
|
|
|
val context: Context = this
|
|
|
private val receiver = MessageReceiver.getMessageReceiver()
|
|
|
private lateinit var timetable: Timetable
|
|
|
private var suggestions: List<GtfsSuggestion>? = null
|
|
|
- private lateinit var drawerLayout: DrawerLayout
|
|
|
+ private lateinit var drawerLayout: androidx.drawerlayout.widget.DrawerLayout
|
|
|
private lateinit var drawerView: NavigationView
|
|
|
- lateinit var favouritesList: RecyclerView
|
|
|
- lateinit var searchView: FloatingSearchView
|
|
|
+ lateinit var favouritesList: androidx.recyclerview.widget.RecyclerView
|
|
|
+ lateinit var searchView: MaterialSearchBar
|
|
|
private lateinit var favourites: FavouriteStorage
|
|
|
private lateinit var adapter: FavouritesAdapter
|
|
|
private val actionModeCallback = ActionModeCallback()
|
|
|
private var actionMode: ActionMode? = null
|
|
|
private var isWarned = false
|
|
|
private lateinit var providerProxy: ProviderProxy
|
|
|
+ private lateinit var suggestionsAdapter: SuggestionsAdapter
|
|
|
|
|
|
companion object {
|
|
|
const val REQUEST_EDIT_FAVOURITE = 1
|
|
@@ -90,63 +91,75 @@ class DashActivity : AppCompatActivity(), MessageReceiver.OnTimetableDownloadLis
|
|
|
showValidityInDrawer()
|
|
|
|
|
|
searchView = search_view
|
|
|
-
|
|
|
- searchView.setOnFocusChangeListener(object : FloatingSearchView.OnFocusChangeListener {
|
|
|
- override fun onFocus() {
|
|
|
- favouritesList.visibility = View.GONE
|
|
|
- providerProxy.getSuggestions(searchView.query) {
|
|
|
- searchView.swapSuggestions(it)
|
|
|
+ suggestionsAdapter = SuggestionsAdapter(layoutInflater, this, this)
|
|
|
+ searchView.setCustomSuggestionAdapter(suggestionsAdapter)
|
|
|
+
|
|
|
+ searchView.addTextChangeListener(object : TextWatcher {
|
|
|
+ override fun afterTextChanged(s: Editable?) {
|
|
|
+ if (searchView.isSearchEnabled) {
|
|
|
+ searchView.clearSuggestions()
|
|
|
+ searchView.updateLastSuggestions(listOf<GtfsSuggestion>())
|
|
|
+ providerProxy.getSuggestions(s.toString()) { suggestions ->
|
|
|
+ suggestions.forEach {
|
|
|
+ if (it.name.contains(s as CharSequence, true)) {
|
|
|
+ suggestionsAdapter.addSuggestion(it)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ searchView.showSuggestionsList()
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- override fun onFocusCleared() {
|
|
|
- favouritesList.visibility = View.VISIBLE
|
|
|
+ override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
|
|
|
}
|
|
|
- })
|
|
|
|
|
|
- searchView.setOnQueryChangeListener { oldQuery, newQuery ->
|
|
|
- if (oldQuery != "" && newQuery == "")
|
|
|
- searchView.clearSuggestions()
|
|
|
- providerProxy.getSuggestions(newQuery) {
|
|
|
- searchView.swapSuggestions(it)
|
|
|
+ override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- 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)
|
|
|
- if (searchSuggestion is StopSuggestion) {
|
|
|
- val intent = Intent(context, StopSpecifyActivity::class.java)
|
|
|
- intent.putExtra(StopSpecifyActivity.EXTRA_STOP_NAME, searchSuggestion.name)
|
|
|
- startActivity(intent)
|
|
|
- } else if (searchSuggestion is LineSuggestion) {
|
|
|
- val intent = Intent(context, LineSpecifyActivity::class.java)
|
|
|
- intent.putExtra(LineSpecifyActivity.EXTRA_LINE_ID, searchSuggestion.name)
|
|
|
- startActivity(intent)
|
|
|
+ })
|
|
|
+
|
|
|
+ searchView.setOnSearchActionListener(object : MaterialSearchBar.OnSearchActionListener {
|
|
|
+ override fun onButtonClicked(buttonCode: Int) {
|
|
|
+ when (buttonCode) {
|
|
|
+ BUTTON_NAVIGATION -> {
|
|
|
+ if (drawerLayout.isDrawerOpen(drawerView))
|
|
|
+ drawerLayout.closeDrawer(drawerView)
|
|
|
+ else
|
|
|
+ drawerLayout.openDrawer(drawerView)
|
|
|
+ }
|
|
|
+ BUTTON_BACK -> {
|
|
|
+ searchView.disableSearch()
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- override fun onSearchAction(query: String) {
|
|
|
+ override fun onSearchStateChanged(enabled: Boolean) {
|
|
|
}
|
|
|
- })
|
|
|
|
|
|
- searchView.setOnBindSuggestionCallback { _, iconView, textView, item, _ ->
|
|
|
- val suggestion = item as GtfsSuggestion
|
|
|
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
|
|
- textView.text = Html.fromHtml(item.body, Html.FROM_HTML_MODE_LEGACY)
|
|
|
- } else {
|
|
|
- @Suppress("DEPRECATION")
|
|
|
- textView.text = Html.fromHtml(item.body)
|
|
|
+ override fun onSearchConfirmed(text: CharSequence?) {
|
|
|
+ // todo re-search
|
|
|
+ println("OnSearchConfirmed")
|
|
|
}
|
|
|
- iconView.setImageDrawable(getDrawable(suggestion.getIcon(), context))
|
|
|
- }
|
|
|
|
|
|
- searchView.attachNavigationDrawerToMenuButton(drawer_layout as DrawerLayout)
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onSuggestionClickListener(suggestion: GtfsSuggestion) {
|
|
|
+ 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)
|
|
|
+ if (suggestion is StopSuggestion) {
|
|
|
+ val intent = Intent(context, StopSpecifyActivity::class.java)
|
|
|
+ intent.putExtra(StopSpecifyActivity.EXTRA_STOP_NAME, suggestion.name)
|
|
|
+ startActivity(intent)
|
|
|
+ } else if (suggestion is LineSuggestion) {
|
|
|
+ val intent = Intent(context, LineSpecifyActivity::class.java)
|
|
|
+ intent.putExtra(LineSpecifyActivity.EXTRA_LINE_ID, suggestion.name)
|
|
|
+ startActivity(intent)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
override fun onRestart() {
|
|
@@ -243,22 +256,24 @@ class DashActivity : AppCompatActivity(), MessageReceiver.OnTimetableDownloadLis
|
|
|
favourites.forEach {
|
|
|
it.subscribeForDepartures(this, this)
|
|
|
}
|
|
|
- val layoutManager = LinearLayoutManager(context)
|
|
|
+ val layoutManager = androidx.recyclerview.widget.LinearLayoutManager(context)
|
|
|
favouritesList = favourites_list
|
|
|
adapter = FavouritesAdapter(context, favourites, this, this)
|
|
|
favouritesList.adapter = adapter
|
|
|
- favouritesList.itemAnimator = DefaultItemAnimator()
|
|
|
+ favouritesList.itemAnimator = androidx.recyclerview.widget.DefaultItemAnimator()
|
|
|
favouritesList.layoutManager = layoutManager
|
|
|
}
|
|
|
|
|
|
override fun onDeparturesReady(departures: List<Departure>, plateId: Plate.ID?, code: Int) {
|
|
|
- favouritesList.adapter.notifyDataSetChanged()
|
|
|
+ favouritesList.adapter!!.notifyDataSetChanged()
|
|
|
showError(drawer_layout, code, this)
|
|
|
}
|
|
|
|
|
|
private fun getSuggestions() {
|
|
|
- providerProxy.getSuggestions {
|
|
|
- searchView.swapSuggestions(it)
|
|
|
+ providerProxy.getSuggestions { suggestions ->
|
|
|
+ suggestions.forEach {
|
|
|
+ suggestionsAdapter.addSuggestion(it)
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -275,12 +290,14 @@ class DashActivity : AppCompatActivity(), MessageReceiver.OnTimetableDownloadLis
|
|
|
startService(Intent(context, TimetableDownloader::class.java))
|
|
|
}
|
|
|
|
|
|
- override fun onBackPressed() { //fixme
|
|
|
+ override fun onBackPressed() {
|
|
|
if (drawerLayout.isDrawerOpen(drawerView)) {
|
|
|
drawerLayout.closeDrawer(drawerView)
|
|
|
return
|
|
|
}
|
|
|
- if (!searchView.setSearchFocused(false)) {
|
|
|
+ if (searchView.isSearchEnabled) {
|
|
|
+ searchView.disableSearch()
|
|
|
+ } else {
|
|
|
super.onBackPressed()
|
|
|
}
|
|
|
}
|
|
@@ -288,7 +305,7 @@ class DashActivity : AppCompatActivity(), MessageReceiver.OnTimetableDownloadLis
|
|
|
override fun onResume() {
|
|
|
super.onResume()
|
|
|
adapter.favourites = favourites
|
|
|
- favouritesList.adapter.notifyDataSetChanged()
|
|
|
+ favouritesList.adapter!!.notifyDataSetChanged()
|
|
|
}
|
|
|
|
|
|
override fun onDestroy() {
|
|
@@ -321,18 +338,18 @@ class DashActivity : AppCompatActivity(), MessageReceiver.OnTimetableDownloadLis
|
|
|
return true
|
|
|
}
|
|
|
|
|
|
- override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
|
|
|
+ override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
|
|
if (requestCode == REQUEST_EDIT_FAVOURITE) {
|
|
|
if (resultCode == Activity.RESULT_OK) {
|
|
|
- val name = data.getStringExtra(EditFavouriteActivity.EXTRA_NEW_NAME)
|
|
|
+ val name = data!!.getStringExtra(EditFavouriteActivity.EXTRA_NEW_NAME)
|
|
|
val positionBefore = data.getIntExtra(EditFavouriteActivity.EXTRA_POSITION_BEFORE, -1)
|
|
|
//adapter.favourites = favourites.favouritesList
|
|
|
if (positionBefore == -1)
|
|
|
- favouritesList.adapter.notifyDataSetChanged()
|
|
|
+ favouritesList.adapter!!.notifyDataSetChanged()
|
|
|
else {
|
|
|
val positionAfter = favourites.indexOf(name)
|
|
|
- favouritesList.adapter.notifyItemChanged(positionBefore)
|
|
|
- favouritesList.adapter.notifyItemMoved(positionBefore, positionAfter)
|
|
|
+ favouritesList.adapter!!.notifyItemChanged(positionBefore)
|
|
|
+ favouritesList.adapter!!.notifyItemMoved(positionBefore, positionAfter)
|
|
|
}
|
|
|
adapter[name]?.let {
|
|
|
it.unsubscribeFromDepartures(context)
|
|
@@ -345,7 +362,7 @@ class DashActivity : AppCompatActivity(), MessageReceiver.OnTimetableDownloadLis
|
|
|
override fun delete(name: String): Boolean {
|
|
|
favourites.delete(name)
|
|
|
//adapter.favourites = favourites.favouritesList
|
|
|
- favouritesList.adapter.notifyItemRemoved(favourites.indexOf(name))
|
|
|
+ favouritesList.adapter!!.notifyItemRemoved(favourites.indexOf(name))
|
|
|
return true
|
|
|
}
|
|
|
|