Skip to content

Commit 0199f08

Browse files
committed
Fix bug: BlogPost information not correct after updating.
1 parent 2c496d8 commit 0199f08

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

app/src/main/java/com/codingwithmitch/openapi/presentation/main/blog/detail/ViewBlogEvents.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ sealed class ViewBlogEvents {
99

1010
data class GetBlog(val pk: Int): ViewBlogEvents()
1111

12+
object Refresh: ViewBlogEvents()
13+
1214
data class ConfirmBlogExistsOnServer(
1315
val pk: Int,
1416
val slug: String

app/src/main/java/com/codingwithmitch/openapi/presentation/main/blog/detail/ViewBlogFragment.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ class ViewBlogFragment : BaseBlogFragment()
4444
binding.deleteButton.setOnClickListener {
4545
viewModel.onTriggerEvent(ViewBlogEvents.DeleteBlog)
4646
}
47+
48+
// If an update occurred from UpdateBlogFragment, refresh the BlogPost
49+
findNavController().currentBackStackEntry?.savedStateHandle?.getLiveData<Boolean>(SHOULD_REFRESH)?.observe(viewLifecycleOwner) { shouldRefresh ->
50+
shouldRefresh?.run {
51+
viewModel.onTriggerEvent(ViewBlogEvents.Refresh)
52+
findNavController().currentBackStackEntry?.savedStateHandle?.set(SHOULD_REFRESH, null)
53+
}
54+
}
4755
}
4856

4957
private fun subscribeObservers(){

app/src/main/java/com/codingwithmitch/openapi/presentation/main/blog/detail/ViewBlogViewModel.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import kotlinx.coroutines.flow.launchIn
1919
import kotlinx.coroutines.flow.onEach
2020
import javax.inject.Inject
2121

22+
const val SHOULD_REFRESH = "should_refresh"
23+
2224
@HiltViewModel
2325
class ViewBlogViewModel
2426
@Inject
@@ -72,6 +74,9 @@ constructor(
7274
}
7375
)
7476
}
77+
is ViewBlogEvents.Refresh ->{
78+
refresh()
79+
}
7580
is ViewBlogEvents.IsAuthor -> {
7681
isAuthor(event.slug)
7782
}
@@ -120,6 +125,21 @@ constructor(
120125
}
121126
}
122127

128+
private fun refresh(){
129+
state.value?.let { state ->
130+
state.blogPost?.let { blogPost ->
131+
getBlog(
132+
pk = blogPost.pk,
133+
callback = object: OnCompleteCallback{
134+
override fun done() {
135+
// do nothing
136+
}
137+
}
138+
)
139+
}
140+
}
141+
}
142+
123143
private fun confirmBlogExistsOnServer(pk: Int, slug: String, callback: OnCompleteCallback){
124144
state.value?.let { state ->
125145
confirmBlogExistsOnServer.execute(

app/src/main/java/com/codingwithmitch/openapi/presentation/main/blog/update/UpdateBlogFragment.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import com.codingwithmitch.openapi.business.domain.util.Constants.Companion.GALL
1717
import com.codingwithmitch.openapi.business.domain.util.ErrorHandling.Companion.SOMETHING_WRONG_WITH_IMAGE
1818
import com.codingwithmitch.openapi.databinding.FragmentUpdateBlogBinding
1919
import com.codingwithmitch.openapi.presentation.main.blog.BaseBlogFragment
20+
import com.codingwithmitch.openapi.presentation.main.blog.detail.SHOULD_REFRESH
2021
import com.codingwithmitch.openapi.presentation.util.processQueue
2122
import com.theartofdev.edmodo.cropper.CropImage
2223
import com.theartofdev.edmodo.cropper.CropImageView
@@ -135,6 +136,7 @@ class UpdateBlogFragment : BaseBlogFragment()
135136
)
136137
}
137138
if(state.isUpdateComplete){
139+
findNavController().previousBackStackEntry?.savedStateHandle?.set(SHOULD_REFRESH, true)
138140
findNavController().popBackStack(R.id.viewBlogFragment, false)
139141
}
140142
})

0 commit comments

Comments
 (0)