Skip to content

Commit

Permalink
removed context from the use case func param and supply via injection
Browse files Browse the repository at this point in the history
  • Loading branch information
jd-alexander committed Jun 25, 2020
1 parent fa613d3 commit 9049ef0
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ private void startObserving() {
return null;
}));
mEditPostRepository.getPostChanged().observe(this, postEvent -> postEvent.applyIfNotHandled(post -> {
mViewModel.savePostToDb(this, mEditPostRepository, mSite);
mViewModel.savePostToDb(mEditPostRepository, mSite);
return null;
}));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ class PostListMainViewModel @Inject constructor(

editPostRepository.run {
postChanged.observe(this@PostListMainViewModel, Observer {
savePostToDbUseCase.savePostToDb(context, editPostRepository, site)
savePostToDbUseCase.savePostToDb(editPostRepository, site)
})
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ class SavePostToDbUseCase
private val uploadUtils: UploadUtilsWrapper,
private val dateTimeUtils: DateTimeUtilsWrapper,
private val dispatcher: Dispatcher,
private val pendingDraftsNotificationsUtils: PendingDraftsNotificationsUtilsWrapper
private val pendingDraftsNotificationsUtils: PendingDraftsNotificationsUtilsWrapper,
private val context: Context
) {
fun savePostToDb(
context: Context,
postRepository: EditPostRepository,
site: SiteModel
) {
Expand All @@ -43,14 +43,13 @@ class SavePostToDbUseCase
post.setIsLocallyChanged(true)
}
post.setDateLocallyChanged(dateTimeUtils.currentTimeInIso8601())
handlePendingDraftNotifications(context, postRepository)
handlePendingDraftNotifications(postRepository)
postRepository.savePostSnapshot()
dispatcher.dispatch(PostActionBuilder.newUpdatePostAction(post))
}
}

private fun handlePendingDraftNotifications(
context: Context,
editPostRepository: EditPostRepository
) {
if (editPostRepository.status == PostStatus.DRAFT) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class StorePostViewModel
editPostRepository: EditPostRepository,
site: SiteModel
): ActivityFinishState {
savePostToDbUseCase.savePostToDb(context, editPostRepository, site)
savePostToDbUseCase.savePostToDb(editPostRepository, site)
return if (networkUtils.isNetworkAvailable()) {
postUtils.trackSavePostAnalytics(
editPostRepository.getPost(),
Expand All @@ -79,11 +79,10 @@ class StorePostViewModel
}

fun savePostToDb(
context: Context,
postRepository: EditPostRepository,
site: SiteModel
) {
savePostToDbUseCase.savePostToDb(context, postRepository, site)
savePostToDbUseCase.savePostToDb(postRepository, site)
}

fun updatePostObjectWithUIAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,6 @@ class PostListMainViewModelTest : BaseUnitTest() {
editPostRepository.updateAsync(action, null)

// assert
verify(savePostToDbUseCase, times(1)).savePostToDb(any(), any(), any())
verify(savePostToDbUseCase, times(1)).savePostToDb(any(), any())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@ class SavePostToDbUseCaseTest {
private lateinit var actionCaptor: KArgumentCaptor<Action<PostModel>>
private val currentTime = "2019-08-09T10:01:03+00:00"
private val postId = 1

@Before
fun setUp() {
savePostToDbUseCase = SavePostToDbUseCase(
uploadUtils,
dateTimeUtils,
dispatcher,
pendingDraftsNotificationsUtils
pendingDraftsNotificationsUtils,
context
)
actionCaptor = argumentCaptor()
whenever(dateTimeUtils.currentTimeInIso8601()).thenReturn(currentTime)
Expand All @@ -63,7 +65,7 @@ class SavePostToDbUseCaseTest {
// Given
setupPost(postHasChanges = true)
// When
savePostToDbUseCase.savePostToDb(context, postRepository, siteModel)
savePostToDbUseCase.savePostToDb(postRepository, siteModel)
// Then
verify(postRepository).savePostSnapshot()
assertThat(actionCaptor.firstValue).isNotNull
Expand All @@ -76,7 +78,7 @@ class SavePostToDbUseCaseTest {
// Given
setupPost(postHasChanges = false)
// When
savePostToDbUseCase.savePostToDb(context, postRepository, siteModel)
savePostToDbUseCase.savePostToDb(postRepository, siteModel)
// Then
verify(postRepository, never()).savePostSnapshot()
assertThat(actionCaptor.allValues).isEmpty()
Expand All @@ -95,7 +97,7 @@ class SavePostToDbUseCaseTest {
// Given
setupPost(userCanPublish = false, postStatus = postStatus)
// When
savePostToDbUseCase.savePostToDb(context, postRepository, siteModel)
savePostToDbUseCase.savePostToDb(postRepository, siteModel)
// Then
assertThat(postModel.status).isEqualTo(PENDING.toString())
}
Expand All @@ -111,7 +113,7 @@ class SavePostToDbUseCaseTest {
// Given
setupPost(userCanPublish = false, postStatus = postStatus)
// When
savePostToDbUseCase.savePostToDb(context, postRepository, siteModel)
savePostToDbUseCase.savePostToDb(postRepository, siteModel)
// Then
assertThat(postModel.status).isEqualTo(postStatus.toString())
}
Expand All @@ -131,7 +133,7 @@ class SavePostToDbUseCaseTest {
// Given
setupPost(userCanPublish = true, postStatus = postStatus)
// When
savePostToDbUseCase.savePostToDb(context, postRepository, siteModel)
savePostToDbUseCase.savePostToDb(postRepository, siteModel)
// Then
assertThat(postModel.status).isEqualTo(postStatus.toString())
}
Expand All @@ -142,7 +144,7 @@ class SavePostToDbUseCaseTest {
// Given
setupPost(postStatus = PUBLISHED)
// When
savePostToDbUseCase.savePostToDb(context, postRepository, siteModel)
savePostToDbUseCase.savePostToDb(postRepository, siteModel)
// Then
verify(pendingDraftsNotificationsUtils).cancelPendingDraftAlarms(context, postId)
}
Expand All @@ -153,7 +155,7 @@ class SavePostToDbUseCaseTest {
setupPost(postStatus = DRAFT)
whenever(postRepository.dateLocallyChanged).thenReturn(currentTime)
// When
savePostToDbUseCase.savePostToDb(context, postRepository, siteModel)
savePostToDbUseCase.savePostToDb(postRepository, siteModel)
// Then
verify(pendingDraftsNotificationsUtils).scheduleNextNotifications(
context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ class StorePostViewModelTest : BaseUnitTest() {

@Test
fun `saves post to DB`() {
viewModel.savePostToDb(context, postRepository, site)
viewModel.savePostToDb(postRepository, site)

verify(savePostToDbUseCase).savePostToDb(context, postRepository, site)
verify(savePostToDbUseCase).savePostToDb(postRepository, site)
}

@Test
Expand Down Expand Up @@ -206,7 +206,7 @@ class StorePostViewModelTest : BaseUnitTest() {
site
)

verify(savePostToDbUseCase).savePostToDb(context, postRepository, site)
verify(savePostToDbUseCase).savePostToDb(postRepository, site)
assertThat(result).isEqualTo(SAVED_LOCALLY)
verifyZeroInteractions(postUtils)
verifyZeroInteractions(uploadService)
Expand All @@ -224,7 +224,7 @@ class StorePostViewModelTest : BaseUnitTest() {
site
)

verify(savePostToDbUseCase).savePostToDb(context, postRepository, site)
verify(savePostToDbUseCase).savePostToDb(postRepository, site)
assertThat(result).isEqualTo(SAVED_ONLINE)
verify(postUtils).trackSavePostAnalytics(postModel, site)
verify(uploadService).uploadPost(context, postId, isFirstTimePublish)
Expand Down

0 comments on commit 9049ef0

Please sign in to comment.