-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainActivity.kt
82 lines (61 loc) · 2.67 KB
/
MainActivity.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
package friendlyrobot.nyc.timetrials
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.SearchView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.squareup.moshi.Moshi
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.item_search_response.view.*
class MainActivity : AppCompatActivity() {
private lateinit var searchResponseAdapter:SearchResponseAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
searchResponseAdapter = SearchResponseAdapter()
booksRV.layoutManager = LinearLayoutManager(this)
booksRV.adapter = searchResponseAdapter
//load fake data for now
val moshi = Moshi.Builder().build()
val sample = moshi.adapter(SearchResponse::class.java).fromJson(SAMPLE_DATA)
sample?.docs?.toList()?.let{searchResponseAdapter.add(it)}
searchView.setOnQueryTextListener( object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(p0: String?): Boolean {
Log.e("main act", "onQueryTextSubmit: ${p0}")
val sample2 = moshi.adapter(SearchResponse::class.java).fromJson(SAMPLE_DATA2)
sample2?.docs?.toList()?.let{searchResponseAdapter.add(it)}
return false
}
override fun onQueryTextChange(p0: String?): Boolean {
Log.e("main act", "onQueryTextChange: ${p0}")
return false
}
})
}
}
class SearchResponseAdapter : RecyclerView.Adapter<SearchResponseItem>() {
private val bookDocs = mutableListOf<BookDoc>()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SearchResponseItem {
val inflater = LayoutInflater.from(parent.context)
return SearchResponseItem(inflater.inflate(R.layout.item_search_response, parent, false))
}
override fun getItemCount() = bookDocs.size
override fun onBindViewHolder(holder: SearchResponseItem, position: Int) {
bookDocs.getOrNull(position)?.let { holder.bind(it) }
}
fun add(items : List<BookDoc>) {
bookDocs.clear()
bookDocs.addAll(items)
notifyDataSetChanged()
}
}
class SearchResponseItem(searchItem: View) : RecyclerView.ViewHolder(searchItem) {
fun bind(bookDoc: BookDoc) {
itemView.titleText.text = bookDoc.title_suggest
}
}