Skip to content

Commit

Permalink
load data from room
Browse files Browse the repository at this point in the history
  • Loading branch information
YoungTr committed Jun 11, 2021
1 parent 8ea0dfe commit e3e4156
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ import androidx.room.*
interface SearchDao {

@Query("SELECT * FROM search_history")
suspend fun getAll(): List<SearchHistory>
suspend fun loadAll(): List<SearchHistory>

@Query("SELECT * FROM search_history WHERE id IN (:searchIds)")
suspend fun loadAllByIds(searchIds: IntArray): List<SearchHistory>

@Query("SELECT * FROM search_history WHERE value = (:value)")
suspend fun getSearchHistory(value: String): SearchHistory?

@Insert
suspend fun insertAll(vararg searches: SearchHistory)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ interface SearchRepo {

suspend fun insertSearchHistories(vararg searchHistory: SearchHistory)

suspend fun insertSearchHistory(value: String)
suspend fun insertSearchHistory(value: String): SearchHistory?

suspend fun getSearchHistory(value: String): SearchHistory?

suspend fun deleteAll()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,29 @@ import javax.inject.Inject
class SearchRepository @Inject constructor(private val searchDao: SearchDao) : SearchRepo {

override suspend fun loadSearchHistories(): List<SearchHistory> {
return searchDao.getAll()
return searchDao.loadAll()
}

override suspend fun insertSearchHistories(vararg searchHistory: SearchHistory) {
searchDao.insertAll(*searchHistory)
}

override suspend fun insertSearchHistory(value: String) {
val search = SearchHistory(value = value, time = System.currentTimeMillis())
insertSearchHistories(search)
override suspend fun insertSearchHistory(value: String): SearchHistory? {
val searchHistory = getSearchHistory(value)
return if (searchHistory == null) {
val search = SearchHistory(value = value, time = System.currentTimeMillis())
insertSearchHistories(search)
search
} else {
null
}
}

override suspend fun deleteAll() {
searchDao.deleteAll()
}

override suspend fun getSearchHistory(value: String): SearchHistory? {
return searchDao.getSearchHistory(value)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ import com.eyepertizer.androidx.util.GlobalUtil
class HotSearchAdapter(val fragment: SearchFragment, var dataList: MutableList<String>) :
RecyclerView.Adapter<RecyclerView.ViewHolder>() {

private val histories: List<SearchHistory> = mutableListOf()
private val histories: MutableList<SearchHistory> = mutableListOf()

override fun getItemCount() = dataList.size + 2
override fun getItemCount() = dataList.size + histories.size + 2

override fun getItemViewType(position: Int) = when (position) {
0 -> Const.ItemViewType.CUSTOM_HEADER
Expand All @@ -66,10 +66,16 @@ class HotSearchAdapter(val fragment: SearchFragment, var dataList: MutableList<S
holder.tvTitle.text = text
}
is HotSearchViewHolder -> {
val item = dataList[position - 1]
holder.tvKeywords.text = item
if (position <= dataList.size) {
val item = dataList[position - 1]
holder.tvKeywords.text = item
} else {
val gap = dataList.size + 2
val item = histories[position - gap]
holder.tvKeywords.text = item.value
}
holder.itemView.setOnClickListener {
"${item},${GlobalUtil.getString(R.string.currently_not_supported)}".showToast()
"${holder.tvKeywords.text}${GlobalUtil.getString(R.string.currently_not_supported)}".showToast()
}
}
else -> {
Expand All @@ -87,6 +93,12 @@ class HotSearchAdapter(val fragment: SearchFragment, var dataList: MutableList<S
}
}

fun addHistories(histories: List<SearchHistory>) {
if (!histories.isNullOrEmpty()) {
this.histories.addAll(histories)
notifyDataSetChanged()
}
}

inner class HeaderViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val tvTitle = view.findViewById<TextView>(R.id.tvTitle)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,14 @@ class SearchFragment : BaseFragment() {
binding.etQuery.showSoftKeyboard()
adapter.setData(result.data as MutableList<String>)
})
viewModel.historyLiveData.observe(this, Observer { histories ->
adapter.addHistories(histories)
})
}

override fun lazyInit() {
viewModel.onRefresh()
viewModel.loadHistories()
}

override fun onDestroyView() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.eyepertizer.androidx.ui.search

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.eyepertizer.androidx.base.viewmodel.BaseRefreshViewModel
import com.eyepertizer.androidx.data.IDataManager
import com.eyepertizer.androidx.data.db.dao.repository.search.SearchHistory
import com.eyepertizer.androidx.data.db.dao.repository.search.SearchRepo
import com.eyepertizer.androidx.data.network.api.MainPageApis
import com.eyepertizer.androidx.util.logD
Expand All @@ -16,18 +18,22 @@ class SearchHotViewModel @Inject constructor(

BaseRefreshViewModel<List<String>>(dataManager) {

val historyLiveData = MutableLiveData<List<SearchHistory>>()

override fun getURL(): String {
return MainPageApis.PUSHMESSAGE_URL
}


override suspend fun fetchData(_url: String): List<String>? {

fun loadHistories() {
viewModelScope.launch {
val loadSearchHistories = searchRepoHelper.loadSearchHistories()
logD("SearchViewModel", loadSearchHistories.joinToString())
logD(TAG, loadSearchHistories.joinToString())
historyLiveData.postValue(loadSearchHistories)
}
}


override suspend fun fetchData(_url: String): List<String>? {
return getDataManager().getHotSearch()
}

Expand All @@ -37,9 +43,17 @@ class SearchHotViewModel @Inject constructor(

fun insert(value: String) {
viewModelScope.launch {
searchRepoHelper.insertSearchHistory(value)
val insertSearchHistory = searchRepoHelper.insertSearchHistory(value)
if (insertSearchHistory != null) {
historyLiveData.postValue(listOf(insertSearchHistory))
} else {
logD(TAG, "$value exit")
}
}
}

companion object {
const val TAG = "SearchHotViewModel"
}

}
4 changes: 2 additions & 2 deletions buildInstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ echo "------------------start build------------------"
./gradlew :app:assembleDebug
echo "------------------build finish------------------"
echo "------------------start install------------------"
adb install -r ./app/build/outputs/apk/debug/app-debug.apk
adb -s 344621240204 install -r ./app/build/outputs/apk/debug/app-debug.apk
echo "------------------start app------------------"
adb shell am start -n com.eyepertizer.androidx/com.eyepertizer.androidx.ui.splash.view.SplashActivity
adb -s 344621240204 shell am start -n com.eyepertizer.androidx/com.eyepertizer.androidx.ui.splash.view.SplashActivity

0 comments on commit e3e4156

Please sign in to comment.