Skip to content

Commit 1779dab

Browse files
fix sort by adding table to db
1 parent 5448329 commit 1779dab

18 files changed

Lines changed: 663 additions & 108 deletions

File tree

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,26 @@ class PropertyListFlowFragment : DynamicNavigationFragment<FragmentPropertyListB
6262
viewModel.refreshPropertyList()
6363
}
6464

65+
subscribeViewModelSortChange()
66+
6567
subscribeGoToDetailScreen()
6668
}
6769

70+
/**
71+
* When sort key is fetched from database change the one belong to Toolbar
72+
*/
73+
private fun subscribeViewModelSortChange() {
74+
viewLifecycleOwner.observe(viewModel.orderKey) {
75+
toolbarVM.currentSortFilter = it
76+
}
77+
}
78+
6879
private fun subscribeToolbarSortChange() {
6980

7081
viewLifecycleOwner.observe(toolbarVM.queryBySort) {
7182
it.getContentIfNotHandled()?.let { orderBy ->
7283
viewModel.refreshPropertyList(orderBy)
84+
toolbarVM.currentSortFilter = orderBy
7385
}
7486
}
7587
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,26 @@ class PropertyListRxjava3Fragment : DynamicNavigationFragment<FragmentPropertyLi
6262
viewModel.refreshPropertyList()
6363
}
6464

65+
subscribeViewModelSortChange()
66+
6567
subscribeGoToDetailScreen()
6668
}
6769

70+
/**
71+
* When sort key is fetched from database change the one belong to Toolbar
72+
*/
73+
private fun subscribeViewModelSortChange() {
74+
viewLifecycleOwner.observe(viewModel.orderKey) {
75+
toolbarVM.currentSortFilter = it
76+
}
77+
}
78+
6879
private fun subscribeToolbarSortChange() {
6980

7081
viewLifecycleOwner.observe(toolbarVM.queryBySort) {
7182
it.getContentIfNotHandled()?.let { orderBy ->
7283
viewModel.refreshPropertyList(orderBy)
84+
toolbarVM.currentSortFilter = orderBy
7385
}
7486
}
7587
}

features/home/src/main/java/com/smarttoolfactory/home/viewmodel/AbstractPropertyListVM.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import androidx.lifecycle.LiveData
44
import androidx.lifecycle.ViewModel
55
import com.smarttoolfactory.core.util.Event
66
import com.smarttoolfactory.core.viewstate.ViewState
7-
import com.smarttoolfactory.domain.ORDER_BY_NONE
87
import com.smarttoolfactory.domain.model.PropertyItem
98

109
/**
@@ -21,9 +20,12 @@ abstract class AbstractPropertyListVM : ViewModel() {
2120

2221
abstract val propertyListViewState: LiveData<ViewState<List<PropertyItem>>>
2322

24-
abstract fun getPropertyList(orderBy: String = ORDER_BY_NONE)
23+
/**
24+
* Used when fragment is just opened
25+
*/
26+
abstract fun getPropertyList()
2527

26-
abstract fun refreshPropertyList(orderBy: String = ORDER_BY_NONE)
28+
abstract fun refreshPropertyList(orderBy: String? = null)
2729

2830
abstract fun onClick(propertyItem: PropertyItem)
2931
}

features/home/src/main/java/com/smarttoolfactory/home/viewmodel/PropertyListViewModelFlow.kt

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.smarttoolfactory.core.util.Event
77
import com.smarttoolfactory.core.util.convertToFlowViewState
88
import com.smarttoolfactory.core.viewstate.Status
99
import com.smarttoolfactory.core.viewstate.ViewState
10+
import com.smarttoolfactory.domain.ORDER_BY_NONE
1011
import com.smarttoolfactory.domain.model.PropertyItem
1112
import com.smarttoolfactory.domain.usecase.GetPropertiesUseCaseFlow
1213
import kotlinx.coroutines.CoroutineScope
@@ -29,6 +30,23 @@ class PropertyListViewModelFlow @ViewModelInject constructor(
2930
override val propertyListViewState: LiveData<ViewState<List<PropertyItem>>>
3031
get() = _propertyViewState
3132

33+
private var _orderByKey = ORDER_BY_NONE
34+
35+
var orderKey = MutableLiveData<String>().apply { value = _orderByKey }
36+
37+
init {
38+
updateOrderByKey()
39+
}
40+
41+
fun updateOrderByKey() {
42+
getPropertiesUseCase.getCurrentSortKey()
43+
.onEach {
44+
_orderByKey = it
45+
orderKey.value = _orderByKey
46+
}
47+
.launchIn(coroutineScope)
48+
}
49+
3250
/**
3351
* Function to retrieve data from repository with offline-first which checks
3452
* local data source first.
@@ -40,9 +58,9 @@ class PropertyListViewModelFlow @ViewModelInject constructor(
4058
* * If both network and db don't have any data throw empty set exception
4159
*
4260
*/
43-
override fun getPropertyList(orderBy: String) {
61+
override fun getPropertyList() {
4462

45-
getPropertiesUseCase.getPropertiesOfflineFirst(orderBy)
63+
getPropertiesUseCase.getPropertiesOfflineFirst(_orderByKey)
4664
.convertToFlowViewState()
4765
.onStart {
4866
_propertyViewState.value = ViewState(status = Status.LOADING)
@@ -53,8 +71,9 @@ class PropertyListViewModelFlow @ViewModelInject constructor(
5371
.launchIn(coroutineScope)
5472
}
5573

56-
override fun refreshPropertyList(orderBy: String) {
57-
getPropertiesUseCase.getPropertiesOfflineLast(orderBy)
74+
override fun refreshPropertyList(orderBy: String?) {
75+
76+
getPropertiesUseCase.getPropertiesOfflineLast(orderBy ?: _orderByKey)
5877
.convertToFlowViewState()
5978
.onStart {
6079
_propertyViewState.value = ViewState(status = Status.LOADING)

features/home/src/main/java/com/smarttoolfactory/home/viewmodel/PropertyListViewModelRxJava3.kt

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import com.smarttoolfactory.core.util.Event
77
import com.smarttoolfactory.core.util.convertFromSingleToObservableViewStateWithLoading
88
import com.smarttoolfactory.core.viewstate.Status
99
import com.smarttoolfactory.core.viewstate.ViewState
10+
import com.smarttoolfactory.domain.ORDER_BY_NONE
1011
import com.smarttoolfactory.domain.model.PropertyItem
1112
import com.smarttoolfactory.domain.usecase.GetPropertiesUseCaseRxJava3
1213
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
14+
import io.reactivex.rxjava3.schedulers.Schedulers
1315

1416
class PropertyListViewModelRxJava3 @ViewModelInject constructor(
1517
private val getPropertiesUseCase: GetPropertiesUseCaseRxJava3
@@ -25,8 +27,32 @@ class PropertyListViewModelRxJava3 @ViewModelInject constructor(
2527
override val propertyListViewState: LiveData<ViewState<List<PropertyItem>>>
2628
get() = _propertyListViewState
2729

28-
override fun getPropertyList(orderBy: String) {
29-
getPropertiesUseCase.getPropertiesOfflineFirst((orderBy))
30+
private var _orderByKey = ORDER_BY_NONE
31+
32+
var orderKey = MutableLiveData<String>().apply { value = _orderByKey }
33+
34+
init {
35+
updateOrderByKey()
36+
}
37+
38+
private fun updateOrderByKey() {
39+
getPropertiesUseCase.getCurrentSortKey()
40+
.subscribeOn(Schedulers.io())
41+
.observeOn(AndroidSchedulers.mainThread())
42+
.subscribe(
43+
{
44+
_orderByKey = it
45+
46+
orderKey.value = _orderByKey
47+
},
48+
{
49+
println("PropertyListViewModelRxJava3 init error: $it")
50+
}
51+
)
52+
}
53+
54+
override fun getPropertyList() {
55+
getPropertiesUseCase.getPropertiesOfflineFirst(_orderByKey)
3056
.convertFromSingleToObservableViewStateWithLoading()
3157
.observeOn(AndroidSchedulers.mainThread())
3258
.subscribe(
@@ -39,8 +65,9 @@ class PropertyListViewModelRxJava3 @ViewModelInject constructor(
3965
)
4066
}
4167

42-
override fun refreshPropertyList(orderBy: String) {
43-
getPropertiesUseCase.getPropertiesOfflineLast(orderBy)
68+
override fun refreshPropertyList(orderBy: String?) {
69+
70+
getPropertiesUseCase.getPropertiesOfflineLast(orderBy ?: _orderByKey)
4471
.convertFromSingleToObservableViewStateWithLoading()
4572
.observeOn(AndroidSchedulers.mainThread())
4673
.subscribe(

features/home/src/main/res/layout/row_property.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
<androidx.constraintlayout.widget.ConstraintLayout
1515
android:layout_width="match_parent"
16-
android:layout_height="150dp"
16+
android:layout_height="140dp"
1717
android:layout_marginStart="8dp"
1818
android:layout_marginEnd="8dp"
1919
android:layout_marginBottom="4dp"
@@ -31,7 +31,7 @@
3131
app:layout_constraintBottom_toBottomOf="parent"
3232
app:layout_constraintStart_toStartOf="parent"
3333
app:layout_constraintTop_toTopOf="parent"
34-
app:layout_constraintWidth_percent=".45">
34+
app:layout_constraintWidth_percent=".4">
3535

3636
<ImageView
3737
imageSrc="@{item.thumbnail}"
@@ -60,10 +60,10 @@
6060
android:paddingTop="1dp"
6161
android:paddingEnd="8dp"
6262
android:paddingBottom="1dp"
63-
android:translationZ="5dp"
64-
android:textSize="10sp"
6563
android:text="VERIFIED"
66-
android:textColor="@color/white" />
64+
android:textColor="@color/white"
65+
android:textSize="10sp"
66+
android:translationZ="5dp" />
6767
</androidx.cardview.widget.CardView>
6868

6969
<com.google.android.material.textview.MaterialTextView
@@ -103,7 +103,7 @@
103103
android:layout_height="40dp"
104104
android:background="#00ffffff"
105105
android:padding="2dp"
106-
android:src="@drawable/ic_baseline_favorite_border_30"
106+
android:src="@drawable/ic_baseline_favorite_border_24"
107107
android:tint="#29B6F6"
108108
app:layout_constraintBottom_toBottomOf="@+id/tvPrice"
109109
app:layout_constraintEnd_toEndOf="parent"

0 commit comments

Comments
 (0)