Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,19 @@ android {

dependencies {
def lifecycle_version = "2.3.1"
def paging_version = "3.0.0"

implementation 'androidx.preference:preference-ktx:1.1.1'

implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.5.0'
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.5.10'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'com.google.android.material:material:1.3.0'

// Android KTX
implementation "androidx.core:core-ktx:1.5.0"
debugImplementation "androidx.fragment:fragment-testing:1.3.1"
debugImplementation "androidx.fragment:fragment-testing:1.3.4"

// Timber
implementation 'com.jakewharton.timber:timber:4.7.1'
Expand All @@ -88,11 +89,9 @@ dependencies {
androidTestImplementation 'com.google.dagger:hilt-android-testing:2.35.1'
// Make Hilt generate code in the androidTest folder
kaptAndroidTest 'com.google.dagger:hilt-android-compiler:2.35.1'

// For instrumentation tests
androidTestImplementation 'com.google.dagger:hilt-android-testing:2.35.1'
kaptAndroidTest 'com.google.dagger:hilt-android-compiler:2.35.1'

// For local unit tests
testImplementation 'com.google.dagger:hilt-android-testing:2.35.1'
kaptTest 'com.google.dagger:hilt-android-compiler:2.35.1'
Expand All @@ -118,14 +117,21 @@ dependencies {
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"

// Paging
implementation "androidx.paging:paging-runtime-ktx:$paging_version"
// alternatively - without Android dependencies for tests
testImplementation "androidx.paging:paging-common-ktx:$paging_version"
// optional - Jetpack Compose integration
implementation "androidx.paging:paging-compose:1.0.0-alpha09"

// Feature module Support
implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"

// Testing Navigation
androidTestImplementation "androidx.navigation:navigation-testing:$nav_version"

// Jetpack Compose Integration
implementation "androidx.navigation:navigation-compose:1.0.0-alpha09"
implementation "androidx.navigation:navigation-compose:2.4.0-alpha01"

// Color Picker Library
implementation 'com.jaredrummler:colorpicker:1.1.0'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fazemeright.myinventorytracker.domain.database.offline.room.dao

import androidx.lifecycle.LiveData
import androidx.paging.PagingSource
import androidx.room.Dao
import androidx.room.Query
import androidx.room.Transaction
Expand Down Expand Up @@ -41,13 +42,13 @@ interface InventoryItemDao : BaseDao<InventoryItem> {

@Transaction
@Query("SELECT * FROM my_inventory_table")
fun getItemsWithBagLive(): LiveData<List<ItemWithBag>>
fun getItemsWithBagLive(): PagingSource<Int, ItemWithBag>/*LiveData<List<ItemWithBag>>*/

@Transaction
@Query("SELECT * FROM my_inventory_table")
fun getItemsWithBag(): List<ItemWithBag>

@Transaction
@Query("SELECT * FROM my_inventory_table WHERE itemName LIKE '%' || :searchString || '%' ORDER BY itemName")
fun searchItems(searchString: String): List<ItemWithBag>
fun searchItems(searchString: String): PagingSource<Int, ItemWithBag>
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.fazemeright.myinventorytracker.repository

import androidx.lifecycle.LiveData
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.liveData
import com.fazemeright.myinventorytracker.domain.authentication.UserAuthentication
import com.fazemeright.myinventorytracker.domain.authentication.firebase.FireBaseUserAuthentication
import com.fazemeright.myinventorytracker.domain.database.offline.room.dao.BagItemDao
Expand Down Expand Up @@ -110,14 +114,18 @@ class InventoryRepository @Inject constructor(
}
}

override fun getItemsWithBagLive(): LiveData<List<ItemWithBag>> {
return inventoryItemDao.getItemsWithBagLive()
override fun getItemsWithBagLive(): LiveData<PagingData<ItemWithBag>> {
return Pager(
config = pagingConfig,
pagingSourceFactory = { inventoryItemDao.getItemsWithBagLive() }
).liveData
}

override suspend fun searchInventoryItems(searchText: String): List<ItemWithBag> {
return withContext(Dispatchers.IO) {
inventoryItemDao.searchItems(searchText)
}
override fun searchInventoryItems(searchText: String): LiveData<PagingData<ItemWithBag>> {
return Pager(
config = pagingConfig,
pagingSourceFactory = { inventoryItemDao.searchItems(searchText) }
).liveData
}

override suspend fun getInventoryItemById(itemId: Int): InventoryItem? {
Expand Down Expand Up @@ -201,4 +209,13 @@ class InventoryRepository @Inject constructor(
}
// TODO: Clear Shared Preferences
}

companion object {
val pagingConfig = PagingConfig(
pageSize = 20,
prefetchDistance = 10,
enablePlaceholders = false,
initialLoadSize = 20
)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.fazemeright.myinventorytracker.repository

import androidx.lifecycle.LiveData
import androidx.paging.PagedList
import androidx.paging.PagingData
import com.fazemeright.myinventorytracker.domain.models.BagItem
import com.fazemeright.myinventorytracker.domain.models.InventoryItem
import com.fazemeright.myinventorytracker.domain.models.ItemWithBag
Expand Down Expand Up @@ -66,12 +68,12 @@ interface Repository {
/**
* Get items with bag data through livedata
*/
fun getItemsWithBagLive(): LiveData<List<ItemWithBag>>
fun getItemsWithBagLive(): LiveData<PagingData<ItemWithBag>>

/**
* Search for inventory items with given search string
*/
suspend fun searchInventoryItems(searchText: String): List<ItemWithBag>
fun searchInventoryItems(searchText: String): LiveData<PagingData<ItemWithBag>>

/**
* Get inventory item with given id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@ package com.fazemeright.myinventorytracker.ui.itemlist

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.fazemeright.myinventorytracker.databinding.ListInventoryItemBinding
import com.fazemeright.myinventorytracker.domain.models.ItemWithBag

class ItemListAdapter(private val clickListener: ItemListener) :
ListAdapter<ItemWithBag,
ItemListAdapter.ViewHolder>(ItemListDiffCallback()) {
PagingDataAdapter<ItemWithBag,
ItemListAdapter.ViewHolder>(ItemListDiffCallback()) {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder.from(parent)
}

fun updateList(list: List<ItemWithBag>?) {
/*fun updateList(list: List<ItemWithBag>?) {
submitList(list)
}
}*/

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
getItem(position).also {
getItem(position)?.let {
holder.bind(it, clickListener)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ import com.fazemeright.myinventorytracker.ui.base.BaseFragment
import com.fazemeright.myinventorytracker.ui.settings.SettingsActivity
import com.google.android.material.snackbar.Snackbar
import dagger.hilt.android.AndroidEntryPoint
import timber.log.Timber
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

@AndroidEntryPoint
class ItemListFragment : BaseFragment<FragmentItemListBinding>() {
Expand Down Expand Up @@ -51,9 +53,10 @@ class ItemListFragment : BaseFragment<FragmentItemListBinding>() {
viewLifecycleOwner,
{
it?.let {
adapter.updateList(it)
CoroutineScope(Dispatchers.IO).launch {
adapter.submitData(it)
}
}
Timber.i("received in onCreate: ${it.size}")
}
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.fazemeright.myinventorytracker.ui.itemlist

import android.app.Application
import androidx.lifecycle.*
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.switchMap
import androidx.lifecycle.viewModelScope
import androidx.paging.PagingData
import com.fazemeright.myinventorytracker.domain.models.InventoryItem
import com.fazemeright.myinventorytracker.domain.models.ItemWithBag
import com.fazemeright.myinventorytracker.repository.InventoryRepository
Expand Down Expand Up @@ -37,14 +41,11 @@ class ItemListViewModel @Inject constructor(
_searchString.value = ""
}

val items = _searchString.switchMap {
val items: LiveData<PagingData<ItemWithBag>> = _searchString.switchMap {
if (it.isEmpty()) repository.getItemsWithBagLive()
else
liveData {
emit(
repository.searchInventoryItems(it)
)
}
else {
repository.searchInventoryItems(it)
}
}

fun onSearchClicked(searchText: String) {
Expand Down