Skip to content

Commit 3147a08

Browse files
committed
Determining if Author of BlogPost
1 parent b2712dd commit 3147a08

File tree

5 files changed

+148
-8
lines changed

5 files changed

+148
-8
lines changed

app/src/main/java/com/codingwithmitch/openapi/api/main/OpenApiMainService.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ interface OpenApiMainService {
4141
): LiveData<GenericApiResponse<BlogListSearchResponse>>
4242

4343

44+
@GET("blog/{slug}/is_author")
45+
fun isAuthorOfBlogPost(
46+
@Header("Authorization") authorization: String,
47+
@Path("slug") slug: String
48+
): LiveData<GenericApiResponse<GenericResponse>>
49+
50+
4451
}
4552

4653

app/src/main/java/com/codingwithmitch/openapi/repository/main/BlogRepository.kt

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.codingwithmitch.openapi.repository.main
33
import android.util.Log
44
import androidx.lifecycle.LiveData
55
import androidx.lifecycle.switchMap
6+
import com.codingwithmitch.openapi.api.GenericResponse
67
import com.codingwithmitch.openapi.api.main.OpenApiMainService
78
import com.codingwithmitch.openapi.api.main.responses.BlogListSearchResponse
89
import com.codingwithmitch.openapi.models.AuthToken
@@ -15,10 +16,14 @@ import com.codingwithmitch.openapi.session.SessionManager
1516
import com.codingwithmitch.openapi.ui.DataState
1617
import com.codingwithmitch.openapi.ui.main.blog.state.BlogViewState
1718
import com.codingwithmitch.openapi.ui.main.blog.state.BlogViewState.*
19+
import com.codingwithmitch.openapi.util.AbsentLiveData
1820
import com.codingwithmitch.openapi.util.ApiSuccessResponse
1921
import com.codingwithmitch.openapi.util.Constants.Companion.PAGINATION_PAGE_SIZE
2022
import com.codingwithmitch.openapi.util.DateUtils
23+
import com.codingwithmitch.openapi.util.ErrorHandling.Companion.ERROR_UNKNOWN
2124
import com.codingwithmitch.openapi.util.GenericApiResponse
25+
import com.codingwithmitch.openapi.util.SuccessHandling.Companion.RESPONSE_HAS_PERMISSION_TO_EDIT
26+
import com.codingwithmitch.openapi.util.SuccessHandling.Companion.RESPONSE_NO_PERMISSION_TO_EDIT
2227
import kotlinx.coroutines.Dispatchers
2328
import kotlinx.coroutines.Dispatchers.IO
2429
import kotlinx.coroutines.Job
@@ -150,6 +155,84 @@ constructor(
150155
}.asLiveData()
151156
}
152157

158+
159+
fun isAuthorOfBlogPost(
160+
authToken: AuthToken,
161+
slug: String
162+
): LiveData<DataState<BlogViewState>> {
163+
return object: NetworkBoundResource<GenericResponse, Any, BlogViewState>(
164+
sessionManager.isConnectedToTheInternet(),
165+
true,
166+
true,
167+
false
168+
){
169+
170+
171+
// not applicable
172+
override suspend fun createCacheRequestAndReturn() {
173+
174+
}
175+
176+
override suspend fun handleApiSuccessResponse(response: ApiSuccessResponse<GenericResponse>) {
177+
withContext(Dispatchers.Main){
178+
179+
Log.d(TAG, "handleApiSuccessResponse: ${response.body.response}")
180+
if(response.body.response.equals(RESPONSE_NO_PERMISSION_TO_EDIT)){
181+
onCompleteJob(
182+
DataState.data(
183+
data = BlogViewState(
184+
viewBlogFields = ViewBlogFields(
185+
isAuthorOfBlogPost = false
186+
)
187+
),
188+
response = null
189+
)
190+
)
191+
}
192+
else if(response.body.response.equals(RESPONSE_HAS_PERMISSION_TO_EDIT)){
193+
onCompleteJob(
194+
DataState.data(
195+
data = BlogViewState(
196+
viewBlogFields = ViewBlogFields(
197+
isAuthorOfBlogPost = true
198+
)
199+
),
200+
response = null
201+
)
202+
)
203+
}
204+
else{
205+
onErrorReturn(ERROR_UNKNOWN, shouldUseDialog = false, shouldUseToast = false)
206+
}
207+
}
208+
}
209+
210+
// not applicable
211+
override fun loadFromCache(): LiveData<BlogViewState> {
212+
return AbsentLiveData.create()
213+
}
214+
215+
// Make an update and change nothing.
216+
// If they are not the author it will return: "You don't have permission to edit that."
217+
override fun createCall(): LiveData<GenericApiResponse<GenericResponse>> {
218+
return openApiMainService.isAuthorOfBlogPost(
219+
"Token ${authToken.token!!}",
220+
slug
221+
)
222+
}
223+
224+
// not applicable
225+
override suspend fun updateLocalDb(cacheObject: Any?) {
226+
227+
}
228+
229+
override fun setJob(job: Job) {
230+
addJob("isAuthorOfBlogPost", job)
231+
}
232+
233+
234+
}.asLiveData()
235+
}
153236
}
154237

155238

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

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import com.codingwithmitch.openapi.R
88
import com.codingwithmitch.openapi.models.BlogPost
99
import com.codingwithmitch.openapi.ui.main.blog.state.BlogStateEvent
1010
import com.codingwithmitch.openapi.ui.main.blog.state.BlogStateEvent.*
11+
import com.codingwithmitch.openapi.ui.main.blog.viewmodel.isAuthorOfBlogPost
12+
import com.codingwithmitch.openapi.ui.main.blog.viewmodel.setIsAuthorOfBlogPost
1113
import com.codingwithmitch.openapi.util.DateUtils
1214
import kotlinx.android.synthetic.main.fragment_view_blog.*
1315

@@ -26,21 +28,44 @@ class ViewBlogFragment : BaseBlogFragment(){
2628
super.onViewCreated(view, savedInstanceState)
2729
setHasOptionsMenu(true)
2830
subscribeObservers()
31+
checkIsAuthorOfBlogPost()
2932
stateChangeListener.expandAppBar()
3033
}
3134

35+
fun checkIsAuthorOfBlogPost(){
36+
viewModel.setIsAuthorOfBlogPost(false) // reset
37+
viewModel.setStateEvent(CheckAuthorOfBlogPost())
38+
}
39+
3240
fun subscribeObservers(){
3341
viewModel.dataState.observe(viewLifecycleOwner, Observer{ dataState ->
3442
stateChangeListener.onDataStateChange(dataState)
43+
44+
dataState.data?.let { data ->
45+
data.data?.getContentIfNotHandled()?.let { viewState ->
46+
viewModel.setIsAuthorOfBlogPost(
47+
viewState.viewBlogFields.isAuthorOfBlogPost
48+
)
49+
}
50+
}
3551
})
3652

3753
viewModel.viewState.observe(viewLifecycleOwner, Observer { viewState ->
3854
viewState.viewBlogFields.blogPost?.let{ blogPost ->
3955
setBlogProperties(blogPost)
4056
}
57+
58+
if(viewState.viewBlogFields.isAuthorOfBlogPost){
59+
adaptViewToAuthorMode()
60+
}
4161
})
4262
}
4363

64+
fun adaptViewToAuthorMode(){
65+
activity?.invalidateOptionsMenu()
66+
delete_button.visibility = View.VISIBLE
67+
}
68+
4469
fun setBlogProperties(blogPost: BlogPost){
4570
requestManager
4671
.load(blogPost.image)
@@ -52,17 +77,13 @@ class ViewBlogFragment : BaseBlogFragment(){
5277
}
5378

5479
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
55-
// TODO("Check if user is author of blog post")
56-
val isAuthorOfBlogPost = true
57-
if(isAuthorOfBlogPost){
80+
if(viewModel.isAuthorOfBlogPost()){
5881
inflater.inflate(R.menu.edit_view_menu, menu)
5982
}
6083
}
6184

6285
override fun onOptionsItemSelected(item: MenuItem): Boolean {
63-
// TODO("Check if user is author of blog post")
64-
val isAuthorOfBlogPost = true
65-
if(isAuthorOfBlogPost){
86+
if(viewModel.isAuthorOfBlogPost()){
6687
when(item.itemId){
6788
R.id.edit -> {
6889
navUpdateBlogFragment()

app/src/main/java/com/codingwithmitch/openapi/ui/main/blog/viewmodel/BlogViewModel.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,12 @@ constructor(
5858
}
5959

6060
is CheckAuthorOfBlogPost -> {
61-
return AbsentLiveData.create()
61+
return sessionManager.cachedToken.value?.let { authToken ->
62+
blogRepository.isAuthorOfBlogPost(
63+
authToken = authToken,
64+
slug = getSlug()
65+
)
66+
}?: AbsentLiveData.create()
6267
}
6368

6469
is None ->{

app/src/main/java/com/codingwithmitch/openapi/ui/main/blog/viewmodel/Getters.kt

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,28 @@ fun BlogViewModel.getPage(): Int{
2222
getCurrentViewStateOrNew().let {
2323
return it.blogFields.page
2424
}
25-
}
25+
}
26+
27+
fun BlogViewModel.getSlug(): String{
28+
getCurrentViewStateOrNew().let {
29+
it.viewBlogFields.blogPost?.let {
30+
return it.slug
31+
}
32+
}
33+
return ""
34+
}
35+
36+
fun BlogViewModel.isAuthorOfBlogPost(): Boolean{
37+
getCurrentViewStateOrNew().let {
38+
return it.viewBlogFields.isAuthorOfBlogPost
39+
}
40+
}
41+
42+
43+
44+
45+
46+
47+
48+
49+

0 commit comments

Comments
 (0)