Skip to content

Commit a128f48

Browse files
committed
Fix issue where requests were being duplicated.
1 parent 0e81847 commit a128f48

File tree

14 files changed

+154
-102
lines changed

14 files changed

+154
-102
lines changed

app/src/androidTest/java/com/codingwithmitch/cleannotes/framework/datasource/cache/NoteDaoServiceTests.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,8 @@ class NoteDaoServiceTests: BaseTest(){
235235
noteDaoService.updateNote(
236236
primaryKey = newNote.id,
237237
title = newTitle,
238-
body = newBody
238+
body = newBody,
239+
timestamp = null
239240
)
240241

241242
val notes = noteDaoService.searchNotes()

app/src/main/java/com/codingwithmitch/cleannotes/business/domain/model/Note.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,25 @@ data class Note(
1212
val created_at: String
1313
) : Parcelable{
1414

15+
override fun equals(other: Any?): Boolean {
16+
if (this === other) return true
17+
if (javaClass != other?.javaClass) return false
1518

19+
other as Note
20+
21+
if (id != other.id) return false
22+
if (title != other.title) return false
23+
if (body != other.body) return false
24+
if (created_at != other.created_at) return false
25+
26+
return true
27+
}
28+
29+
override fun hashCode(): Int {
30+
var result = id.hashCode()
31+
result = 31 * result + title.hashCode()
32+
result = 31 * result + body.hashCode()
33+
result = 31 * result + created_at.hashCode()
34+
return result
35+
}
1636
}

app/src/main/java/com/codingwithmitch/cleannotes/business/domain/state/DataChannelManager.kt

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ abstract class DataChannelManager<ViewState> {
4949
private fun offerToDataChannel(dataState: DataState<ViewState>){
5050
dataChannel.let {
5151
if(!it.isClosedForSend){
52+
printLogD("DCM", "offer to channel!")
5253
it.offer(dataState)
5354
}
5455
}
@@ -58,7 +59,7 @@ abstract class DataChannelManager<ViewState> {
5859
stateEvent: StateEvent,
5960
jobFunction: Flow<DataState<ViewState>?>
6061
){
61-
if(!isStateEventActive(stateEvent)){
62+
if(canExecuteNewStateEvent(stateEvent)){
6263
printLogD("DCM", "launching job: ${stateEvent.eventName()}")
6364
addStateEvent(stateEvent)
6465
jobFunction
@@ -71,7 +72,19 @@ abstract class DataChannelManager<ViewState> {
7172
}
7273
}
7374

74-
private fun isMessageStackEmpty(): Boolean {
75+
private fun canExecuteNewStateEvent(stateEvent: StateEvent): Boolean{
76+
// If a job is already active, do not allow duplication
77+
if(isJobAlreadyActive(stateEvent)){
78+
return false
79+
}
80+
// if a dialog is showing, do not allow new StateEvents
81+
if(!isMessageStackEmpty()){
82+
return false
83+
}
84+
return true
85+
}
86+
87+
fun isMessageStackEmpty(): Boolean {
7588
return messageStack.isStackEmpty()
7689
}
7790

@@ -83,7 +96,10 @@ abstract class DataChannelManager<ViewState> {
8396
messageStack.add(stateMessage)
8497
}
8598

86-
fun clearStateMessage(index: Int = 0) = messageStack.removeAt(index)
99+
fun clearStateMessage(index: Int = 0){
100+
printLogD("DataChannelManager", "clear state message")
101+
messageStack.removeAt(index)
102+
}
87103

88104
fun clearAllStateMessages() = messageStack.clear()
89105

@@ -99,7 +115,7 @@ abstract class DataChannelManager<ViewState> {
99115
fun clearActiveStateEventCounter()
100116
= stateEventManager.clearActiveStateEventCounter()
101117

102-
private fun addStateEvent(stateEvent: StateEvent)
118+
fun addStateEvent(stateEvent: StateEvent)
103119
= stateEventManager.addStateEvent(stateEvent)
104120

105121
fun removeStateEvent(stateEvent: StateEvent?)

app/src/main/java/com/codingwithmitch/cleannotes/business/domain/state/StateEventManager.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.codingwithmitch.cleannotes.business.domain.state
33
import androidx.lifecycle.LiveData
44
import androidx.lifecycle.MutableLiveData
55
import com.codingwithmitch.cleannotes.util.EspressoIdlingResource
6+
import com.codingwithmitch.cleannotes.util.printLogD
67

78
/**
89
* - Keeps track of active StateEvents in DataChannelManager
@@ -23,6 +24,7 @@ class StateEventManager {
2324
}
2425

2526
fun clearActiveStateEventCounter(){
27+
printLogD("DCM", "Clear active state events")
2628
EspressoIdlingResource.clear()
2729
activeStateEvents.clear()
2830
syncNumActiveStateEvents()
@@ -35,6 +37,7 @@ class StateEventManager {
3537
}
3638

3739
fun removeStateEvent(stateEvent: StateEvent?){
40+
printLogD("DCM sem", "remove state event: ${stateEvent?.eventName()}")
3841
stateEvent?.let {
3942
EspressoIdlingResource.decrement()
4043
}
@@ -43,12 +46,9 @@ class StateEventManager {
4346
}
4447

4548
fun isStateEventActive(stateEvent: StateEvent): Boolean{
46-
for(eventName in activeStateEvents.keys){
47-
if(stateEvent.eventName().equals(eventName)){
48-
return true
49-
}
50-
}
51-
return false
49+
printLogD("DCM sem", "is state event active? " +
50+
"${activeStateEvents.containsKey(stateEvent.eventName())}")
51+
return activeStateEvents.containsKey(stateEvent.eventName())
5252
}
5353

5454
private fun syncNumActiveStateEvents(){

app/src/main/java/com/codingwithmitch/cleannotes/business/interactors/notelist/SearchNotes.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ class SearchNotes(
2020
page: Int,
2121
stateEvent: StateEvent
2222
): Flow<DataState<NoteListViewState>?> = flow {
23-
2423
var updatedPage = page
2524
if(page <= 0){
2625
updatedPage = 1

app/src/main/java/com/codingwithmitch/cleannotes/framework/presentation/common/BaseViewModel.kt

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,34 @@ abstract class BaseViewModel<ViewState> : ViewModel()
7373
jobFunction: Flow<DataState<ViewState>?>
7474
) = dataChannelManager.launchJob(stateEvent, jobFunction)
7575

76-
fun isJobAlreadyActive(stateEvent: StateEvent): Boolean {
77-
printLogD("BaseViewModel", "is job already active? ${dataChannelManager.isJobAlreadyActive(stateEvent)} ")
78-
return dataChannelManager.isJobAlreadyActive(stateEvent)
79-
}
76+
// private fun isJobAlreadyActive(stateEvent: StateEvent): Boolean {
77+
// return dataChannelManager.isJobAlreadyActive(stateEvent)
78+
// }
79+
//
80+
// private fun isMessageStackEmpty() = dataChannelManager.isMessageStackEmpty()
81+
//
82+
// fun canExecuteNewStateEvent(stateEvent: StateEvent): Boolean{
83+
// // If a job is already active, do not allow duplication
84+
// if(dataChannelManager.isJobAlreadyActive(stateEvent)){
85+
// return false
86+
// }
87+
// // if a dialog is showing, do not allow new StateEvents
88+
// if(!isMessageStackEmpty()){
89+
// return false
90+
// }
91+
// return true
92+
// }
8093

8194
fun getCurrentViewStateOrNew(): ViewState{
82-
val value = viewState.value?.let{
83-
it
84-
}?: initNewViewState()
85-
return value
95+
return viewState.value ?: initNewViewState()
8696
}
8797

8898
fun setViewState(viewState: ViewState){
8999
_viewState.value = viewState
90100
}
91101

92102
fun clearStateMessage(index: Int = 0){
103+
printLogD("BaseViewModel", "clearStateMessage")
93104
dataChannelManager.clearStateMessage(index)
94105
}
95106

app/src/main/java/com/codingwithmitch/cleannotes/framework/presentation/notedetail/NoteDetailViewModel.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ constructor(
4545

4646
override fun setStateEvent(stateEvent: StateEvent) {
4747

48-
if(!isJobAlreadyActive(stateEvent)){
48+
// if(canExecuteNewStateEvent(stateEvent)){
4949
val job: Flow<DataState<NoteDetailViewState>?> = when(stateEvent){
5050

5151
is UpdateNoteEvent -> {
@@ -88,7 +88,7 @@ constructor(
8888
}
8989
}
9090
launchJob(stateEvent, job)
91-
}
91+
// }
9292
}
9393

9494
fun beginPendingDelete(note: Note){

app/src/main/java/com/codingwithmitch/cleannotes/framework/presentation/notelist/NoteListFragment.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import com.codingwithmitch.cleannotes.util.TodoCallback
4343
import com.codingwithmitch.cleannotes.util.printLogD
4444
import kotlinx.android.synthetic.main.fragment_note_list.*
4545
import kotlinx.coroutines.*
46+
import kotlinx.coroutines.Dispatchers.Main
4647
import javax.inject.Inject
4748

4849

@@ -479,6 +480,7 @@ constructor(
479480
}
480481

481482
private fun startNewSearch(){
483+
printLogD("DCM", "start new search")
482484
viewModel.clearList()
483485
viewModel.loadFirstPage()
484486
}

app/src/main/java/com/codingwithmitch/cleannotes/framework/presentation/notelist/NoteListViewModel.kt

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ constructor(
8989

9090
override fun setStateEvent(stateEvent: StateEvent) {
9191

92-
if(!isJobAlreadyActive(stateEvent)){
92+
// if(canExecuteNewStateEvent(stateEvent)){
93+
// dataChannelManager.addStateEvent(stateEvent)
9394
val job: Flow<DataState<NoteListViewState>?> = when(stateEvent){
9495

9596
is InsertNewNoteEvent -> {
@@ -157,7 +158,7 @@ constructor(
157158
}
158159
}
159160
launchJob(stateEvent, job)
160-
}
161+
// }
161162
}
162163

163164
private fun removeSelectedNotesFromList(){
@@ -394,19 +395,20 @@ constructor(
394395
}
395396

396397
fun loadFirstPage() {
397-
if(!isJobAlreadyActive(SearchNotesEvent())){
398+
// if(canExecuteNewStateEvent(SearchNotesEvent())){
398399
setQueryExhausted(false)
399400
resetPage()
400401
setStateEvent(SearchNotesEvent())
401402
printLogD("NoteListViewModel",
402403
"loadFirstPage: ${getCurrentViewStateOrNew().searchQuery}")
403-
}
404+
// }
404405
}
405406

406407
fun nextPage(){
407-
if(!isJobAlreadyActive(SearchNotesEvent())
408-
&& !isQueryExhausted()
409-
){
408+
// if(canExecuteNewStateEvent(SearchNotesEvent())
409+
// && !isQueryExhausted()
410+
// ){
411+
if(!isQueryExhausted()){
410412
printLogD("NoteListViewModel", "attempting to load next page...")
411413
clearLayoutManagerState()
412414
incrementPageNumber()
@@ -422,16 +424,19 @@ constructor(
422424
}
423425

424426
fun retrieveNumNotesInCache(){
425-
if(!isJobAlreadyActive(GetNumNotesInCacheEvent())){
426-
setStateEvent(GetNumNotesInCacheEvent())
427-
}
427+
// if(canExecuteNewStateEvent(GetNumNotesInCacheEvent())){
428+
// setStateEvent(GetNumNotesInCacheEvent())
429+
// }
430+
setStateEvent(GetNumNotesInCacheEvent())
428431
}
429432

430433
fun refreshSearchQuery(){
431-
if(!isJobAlreadyActive(SearchNotesEvent())){
432-
setQueryExhausted(false)
433-
setStateEvent(SearchNotesEvent(false))
434-
}
434+
// if(canExecuteNewStateEvent(SearchNotesEvent())){
435+
// setQueryExhausted(false)
436+
// setStateEvent(SearchNotesEvent(false))
437+
// }
438+
setQueryExhausted(false)
439+
setStateEvent(SearchNotesEvent(false))
435440
}
436441

437442
fun getLayoutManagerState(): Parcelable? {
Binary file not shown.
Binary file not shown.

buildSrc/build/libs/buildSrc.jar

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)