Skip to content

Commit 90d1414

Browse files
fix: UseCase not getting data in same order with insert
1 parent c0bff98 commit 90d1414

7 files changed

Lines changed: 83 additions & 23 deletions

File tree

features/home/src/main/java/com/smarttoolfactory/home/adapter/PropertyListAdapter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class PropertyItemDiffCallback : DiffUtil.ItemCallback<PropertyItem>() {
8282
oldItem: PropertyItem,
8383
newItem: PropertyItem
8484
): Boolean {
85-
return oldItem == newItem
85+
return oldItem === newItem
8686
}
8787

8888
override fun areContentsTheSame(

features/home/src/main/java/com/smarttoolfactory/home/propertylist/PropertyListFlowFragment.kt

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class PropertyListFlowFragment : DynamicNavigationFragment<FragmentPropertyListB
2121
@Inject
2222
lateinit var viewModel: PropertyListViewModelFlow
2323

24+
lateinit var itemListAdapter: PropertyItemListAdapter
25+
2426
/**
2527
* ViewModel for setting sort filter on top menu and property list fragments
2628
*/
@@ -44,13 +46,13 @@ class PropertyListFlowFragment : DynamicNavigationFragment<FragmentPropertyListB
4446
LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false)
4547

4648
// Set RecyclerViewAdapter
47-
this.adapter =
48-
PropertyItemListAdapter(
49-
R.layout.row_property,
50-
viewModel::onClick,
51-
viewModel::onLikeButtonClick
49+
itemListAdapter = PropertyItemListAdapter(
50+
R.layout.row_property,
51+
viewModel::onClick,
52+
viewModel::onLikeButtonClick
5253

53-
)
54+
)
55+
this.adapter = itemListAdapter
5456
}
5557

5658
val swipeRefreshLayout = dataBinding.swipeRefreshLayout
@@ -61,8 +63,6 @@ class PropertyListFlowFragment : DynamicNavigationFragment<FragmentPropertyListB
6163
}
6264

6365
subscribeGoToDetailScreen()
64-
65-
subscribeToolbarSortChange()
6666
}
6767

6868
private fun subscribeToolbarSortChange() {
@@ -100,4 +100,14 @@ class PropertyListFlowFragment : DynamicNavigationFragment<FragmentPropertyListB
100100
)
101101
.inject(this)
102102
}
103+
104+
override fun onResume() {
105+
super.onResume()
106+
subscribeToolbarSortChange()
107+
}
108+
109+
override fun onPause() {
110+
super.onPause()
111+
toolbarVM.queryBySort.removeObservers(viewLifecycleOwner)
112+
}
103113
}

features/home/src/main/java/com/smarttoolfactory/home/propertylist/PropertyListRxjava3Fragment.kt

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import androidx.fragment.app.activityViewModels
66
import androidx.recyclerview.widget.LinearLayoutManager
77
import com.smarttoolfactory.core.di.CoreModuleDependencies
88
import com.smarttoolfactory.core.ui.fragment.DynamicNavigationFragment
9+
import com.smarttoolfactory.core.util.observe
910
import com.smarttoolfactory.home.R
1011
import com.smarttoolfactory.home.adapter.PropertyItemListAdapter
1112
import com.smarttoolfactory.home.databinding.FragmentPropertyListBinding
@@ -20,6 +21,9 @@ class PropertyListRxjava3Fragment : DynamicNavigationFragment<FragmentPropertyLi
2021
@Inject
2122
lateinit var viewModel: PropertyListViewModelRxJava3
2223

24+
lateinit var itemListAdapter:PropertyItemListAdapter
25+
26+
2327
/**
2428
* ViewModel for setting sort filter on top menu and property list fragments
2529
*/
@@ -43,13 +47,14 @@ class PropertyListRxjava3Fragment : DynamicNavigationFragment<FragmentPropertyLi
4347
LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false)
4448

4549
// Set RecyclerViewAdapter
46-
this.adapter =
47-
PropertyItemListAdapter(
48-
R.layout.row_property,
49-
viewModel::onClick,
50-
viewModel::onLikeButtonClick
50+
itemListAdapter = PropertyItemListAdapter(
51+
R.layout.row_property,
52+
viewModel::onClick,
53+
viewModel::onLikeButtonClick
54+
55+
)
56+
this.adapter =itemListAdapter
5157

52-
)
5358
}
5459

5560
val swipeRefreshLayout = dataBinding.swipeRefreshLayout
@@ -60,6 +65,16 @@ class PropertyListRxjava3Fragment : DynamicNavigationFragment<FragmentPropertyLi
6065
}
6166

6267
subscribeGoToDetailScreen()
68+
69+
}
70+
71+
private fun subscribeToolbarSortChange() {
72+
73+
viewLifecycleOwner.observe(toolbarVM.queryBySort) {
74+
it.getContentIfNotHandled()?.let { orderBy ->
75+
viewModel.refreshPropertyList(orderBy)
76+
}
77+
}
6378
}
6479

6580
private fun subscribeGoToDetailScreen() {
@@ -88,4 +103,14 @@ class PropertyListRxjava3Fragment : DynamicNavigationFragment<FragmentPropertyLi
88103
)
89104
.inject(this)
90105
}
106+
107+
override fun onResume() {
108+
super.onResume()
109+
subscribeToolbarSortChange()
110+
}
111+
112+
override fun onPause() {
113+
super.onPause()
114+
toolbarVM.queryBySort.removeObservers(viewLifecycleOwner)
115+
}
91116
}

libraries/data/schemas/com.smarttoolfactory.data.db.PropertyDatabase/1.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"formatVersion": 1,
33
"database": {
44
"version": 1,
5-
"identityHash": "15b2cbc83a5ed4f7c8f30f2cb27bd7d2",
5+
"identityHash": "33b163c8f718e70e8c782b7aa90a25ce",
66
"entities": [
77
{
88
"tableName": "property",
9-
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`insert_order` INTEGER NOT NULL, `id` INTEGER NOT NULL, `update` INTEGER NOT NULL, `category_id` INTEGER NOT NULL, `title` TEXT NOT NULL, `subject` TEXT NOT NULL, `type` TEXT NOT NULL, `type_id` INTEGER NOT NULL, `thumbnail` TEXT, `thumbnail_big` TEXT, `image_count` INTEGER NOT NULL, `price` TEXT NOT NULL, `price_period` TEXT, `price_period_raw` TEXT NOT NULL, `price_label` TEXT, `price_value` TEXT, `price_value_raw` INTEGER NOT NULL, `currency` TEXT NOT NULL, `featured` INTEGER NOT NULL, `location` TEXT NOT NULL, `area` TEXT NOT NULL, `poa` INTEGER NOT NULL, `rera_permit` TEXT, `bathrooms` TEXT NOT NULL, `bedrooms` TEXT NOT NULL, `date_insert` TEXT NOT NULL, `date_update` TEXT NOT NULL, `agent_name` TEXT NOT NULL, `broker_name` TEXT NOT NULL, `agent_license` TEXT, `location_id` INTEGER NOT NULL, `hide_location` INTEGER NOT NULL, `broker` TEXT NOT NULL, `amenities` TEXT NOT NULL, `amenities_keys` TEXT NOT NULL, `latitude` REAL NOT NULL, `longitude` REAL NOT NULL, `premium` INTEGER NOT NULL, `livingrooms` TEXT NOT NULL, `verified` INTEGER NOT NULL, `gallery` TEXT, `phone` TEXT NOT NULL, `lead_email_receivers` TEXT NOT NULL, `reference` TEXT NOT NULL, PRIMARY KEY(`insert_order`, `id`))",
9+
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`insert_order` INTEGER NOT NULL, `id` INTEGER NOT NULL, `update` INTEGER NOT NULL, `category_id` INTEGER NOT NULL, `title` TEXT NOT NULL, `subject` TEXT NOT NULL, `type` TEXT NOT NULL, `type_id` INTEGER NOT NULL, `thumbnail` TEXT, `thumbnail_big` TEXT, `image_count` INTEGER NOT NULL, `price` TEXT NOT NULL, `price_period` TEXT, `price_period_raw` TEXT NOT NULL, `price_label` TEXT, `price_value` TEXT, `price_value_raw` INTEGER NOT NULL, `currency` TEXT NOT NULL, `featured` INTEGER NOT NULL, `location` TEXT NOT NULL, `area` TEXT NOT NULL, `poa` INTEGER NOT NULL, `rera_permit` TEXT, `bathrooms` TEXT NOT NULL, `bedrooms` TEXT NOT NULL, `date_insert` TEXT NOT NULL, `date_update` TEXT NOT NULL, `agent_name` TEXT NOT NULL, `broker_name` TEXT NOT NULL, `agent_license` TEXT, `location_id` INTEGER NOT NULL, `hide_location` INTEGER NOT NULL, `broker` TEXT NOT NULL, `amenities` TEXT NOT NULL, `amenities_keys` TEXT NOT NULL, `latitude` REAL NOT NULL, `longitude` REAL NOT NULL, `premium` INTEGER NOT NULL, `livingrooms` TEXT NOT NULL, `verified` INTEGER NOT NULL, `gallery` TEXT, `phone` TEXT NOT NULL, `lead_email_receivers` TEXT NOT NULL, `reference` TEXT NOT NULL, PRIMARY KEY(`insert_order`))",
1010
"fields": [
1111
{
1212
"fieldPath": "insertOrder",
@@ -275,8 +275,7 @@
275275
],
276276
"primaryKey": {
277277
"columnNames": [
278-
"insert_order",
279-
"id"
278+
"insert_order"
280279
],
281280
"autoGenerate": false
282281
},
@@ -287,7 +286,7 @@
287286
"views": [],
288287
"setupQueries": [
289288
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
290-
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '15b2cbc83a5ed4f7c8f30f2cb27bd7d2')"
289+
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '33b163c8f718e70e8c782b7aa90a25ce')"
291290
]
292291
}
293292
}

libraries/data/src/main/java/com/smarttoolfactory/data/model/local/PropertyEntity.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ import com.smarttoolfactory.data.model.IEntity
1212
* * Implements [IEntity] marker interface for mapping this database items from REST DTOs or
1313
* to UI items
1414
*/
15-
@Entity(tableName = "property")
15+
@Entity(tableName = "property",primaryKeys = ["insert_order"])
1616
data class PropertyEntity(
1717

18-
@PrimaryKey
18+
@ColumnInfo(name = "insert_order")
19+
var insertOrder:Int = 0,
20+
1921
@ColumnInfo(name = "id")
2022
val id: Int,
2123
@ColumnInfo(name = "update")

libraries/domain/src/main/java/com/smarttoolfactory/domain/usecase/GetPropertiesUseCaseFlow.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,15 @@ class GetPropertiesUseCaseFlow @Inject constructor(
5555
throw EmptyDataException("No Data is available in Remote source!")
5656
} else {
5757
repository.run {
58+
5859
deletePropertyEntities()
60+
61+
// 🔥 Add an insert order since we are not using Room's ORDER BY
62+
it.forEachIndexed { index, propertyEntity ->
63+
propertyEntity.insertOrder = index
64+
}
5965
savePropertyEntities(it)
66+
6067
getPropertyEntitiesFromLocal()
6168
}
6269
}
@@ -102,7 +109,12 @@ class GetPropertiesUseCaseFlow @Inject constructor(
102109
repository.run {
103110
val data = fetchEntitiesFromRemote()
104111
deletePropertyEntities()
105-
savePropertyEntities(data)
112+
113+
// 🔥 Add an insert order since we are not using Room's ORDER BY
114+
data.forEachIndexed { index, propertyEntity ->
115+
propertyEntity.insertOrder = index
116+
}
117+
106118
data
107119
}
108120
} else {

libraries/domain/src/main/java/com/smarttoolfactory/domain/usecase/GetPropertiesUseCaseRxJava3.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ class GetPropertiesUseCaseRxJava3 @Inject constructor(
4949
throw EmptyDataException("No Data is available in remote source!")
5050
} else {
5151
repository.deletePropertyEntities()
52+
.andThen {completableObserver->
53+
it.forEachIndexed { index, propertyEntity ->
54+
propertyEntity.insertOrder = index
55+
}
56+
completableObserver.onComplete()
57+
}
5258
.andThen(repository.savePropertyEntities(it))
5359
.andThen(repository.getPropertyEntitiesFromLocal())
5460
}
@@ -86,6 +92,12 @@ class GetPropertiesUseCaseRxJava3 @Inject constructor(
8692
repository.fetchEntitiesFromRemote()
8793
.concatMap {
8894
repository.deletePropertyEntities()
95+
.andThen {completableObserver->
96+
it.forEachIndexed { index, propertyEntity ->
97+
propertyEntity.insertOrder = index
98+
}
99+
completableObserver.onComplete()
100+
}
89101
.andThen(repository.savePropertyEntities(it))
90102
.andThen(Single.just(it))
91103
}

0 commit comments

Comments
 (0)