File tree Expand file tree Collapse file tree 4 files changed +32
-0
lines changed
app/src/main/java/com/codingwithmitch/openapi/presentation/main/blog Expand file tree Collapse file tree 4 files changed +32
-0
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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 (){
Original file line number Diff line number Diff line change @@ -19,6 +19,8 @@ import kotlinx.coroutines.flow.launchIn
1919import kotlinx.coroutines.flow.onEach
2020import javax.inject.Inject
2121
22+ const val SHOULD_REFRESH = " should_refresh"
23+
2224@HiltViewModel
2325class 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(
Original file line number Diff line number Diff line change @@ -17,6 +17,7 @@ import com.codingwithmitch.openapi.business.domain.util.Constants.Companion.GALL
1717import com.codingwithmitch.openapi.business.domain.util.ErrorHandling.Companion.SOMETHING_WRONG_WITH_IMAGE
1818import com.codingwithmitch.openapi.databinding.FragmentUpdateBlogBinding
1919import com.codingwithmitch.openapi.presentation.main.blog.BaseBlogFragment
20+ import com.codingwithmitch.openapi.presentation.main.blog.detail.SHOULD_REFRESH
2021import com.codingwithmitch.openapi.presentation.util.processQueue
2122import com.theartofdev.edmodo.cropper.CropImage
2223import 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 })
You can’t perform that action at this time.
0 commit comments