Skip to content

Commit 2499624

Browse files
committed
Fix offline bugs in blog list
1 parent 4d1b098 commit 2499624

File tree

3 files changed

+102
-60
lines changed

3 files changed

+102
-60
lines changed

app/src/main/java/com/codingwithmitch/openapi/business/interactors/blog/ConfirmBlogExistsOnServer.kt

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package com.codingwithmitch.openapi.business.interactors.blog
22

3+
import android.util.Log
34
import com.codingwithmitch.openapi.api.handleUseCaseException
45
import com.codingwithmitch.openapi.business.datasource.cache.blog.BlogPostDao
56
import com.codingwithmitch.openapi.business.datasource.cache.blog.toBlogPost
67
import com.codingwithmitch.openapi.business.datasource.network.main.OpenApiMainService
78
import com.codingwithmitch.openapi.business.domain.models.AuthToken
89
import com.codingwithmitch.openapi.business.domain.models.BlogPost
910
import com.codingwithmitch.openapi.business.domain.util.*
11+
import com.codingwithmitch.openapi.business.domain.util.ErrorHandling.Companion.UNABLE_TO_RESOLVE_HOST
1012
import com.codingwithmitch.openapi.business.domain.util.SuccessHandling.Companion.SUCCESS_BLOG_DOES_NOT_EXIST_IN_CACHE
1113
import kotlinx.coroutines.flow.Flow
1214
import kotlinx.coroutines.flow.catch
@@ -43,34 +45,51 @@ class ConfirmBlogExistsOnServer(
4345
throw Exception(ErrorHandling.ERROR_AUTH_TOKEN_INVALID)
4446
}
4547
// confirm it exists on server (throws 404 if does not exist)
48+
var isNetworkError = false
4649
val blogPost = try {
4750
service.getBlog(
4851
authorization = "Token ${authToken.token}",
4952
slug = slug,
5053
)
5154
}catch (e1: Exception){
55+
if(e1.message?.contains(UNABLE_TO_RESOLVE_HOST) == true){ // network error
56+
isNetworkError = true
57+
}
5258
e1.printStackTrace()
5359
null
5460
}
55-
// if it exists on server but not in cache. Delete from cache and emit error.
56-
if(blogPost == null){
57-
cache.deleteBlogPost(pk)
58-
emit(DataState.error<Response>(
59-
response = Response(
60-
message = ErrorHandling.ERROR_BLOG_DOES_NOT_EXIST,
61-
uiComponentType = UIComponentType.Dialog(),
62-
messageType = MessageType.Error()
61+
if(isNetworkError){
62+
emit(
63+
DataState.error<Response>(
64+
response = Response(
65+
message = "Network Error.",
66+
uiComponentType = UIComponentType.None(),
67+
messageType = MessageType.Error()
68+
)
6369
)
64-
))
65-
}else{ // if it exists in the cache and on the server. Everything is fine.
66-
emit(DataState.data<Response>(
67-
data = Response(
68-
message = SuccessHandling.SUCCESS_BLOG_EXISTS_ON_SERVER,
69-
uiComponentType = UIComponentType.None(),
70-
messageType = MessageType.Success()
71-
),
72-
response = null,
73-
))
70+
)
71+
}
72+
else{
73+
// if it exists on server but not in cache. Delete from cache and emit error.
74+
if(blogPost == null){
75+
cache.deleteBlogPost(pk)
76+
emit(DataState.error<Response>(
77+
response = Response(
78+
message = ErrorHandling.ERROR_BLOG_DOES_NOT_EXIST,
79+
uiComponentType = UIComponentType.Dialog(),
80+
messageType = MessageType.Error()
81+
)
82+
))
83+
}else{ // if it exists in the cache and on the server. Everything is fine.
84+
emit(DataState.data<Response>(
85+
data = Response(
86+
message = SuccessHandling.SUCCESS_BLOG_EXISTS_ON_SERVER,
87+
uiComponentType = UIComponentType.None(),
88+
messageType = MessageType.Success()
89+
),
90+
response = null,
91+
))
92+
}
7493
}
7594
}
7695
}.catch { e ->

app/src/main/java/com/codingwithmitch/openapi/business/interactors/blog/IsAuthorOfBlogPost.kt

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.codingwithmitch.openapi.business.interactors.blog
22

33
import com.codingwithmitch.openapi.api.handleUseCaseException
4+
import com.codingwithmitch.openapi.business.datasource.cache.blog.toEntity
45
import com.codingwithmitch.openapi.business.datasource.network.main.OpenApiMainService
6+
import com.codingwithmitch.openapi.business.datasource.network.main.toBlogPost
57
import com.codingwithmitch.openapi.business.domain.models.AuthToken
8+
import com.codingwithmitch.openapi.business.domain.models.BlogPost
69
import com.codingwithmitch.openapi.business.domain.util.DataState
710
import com.codingwithmitch.openapi.business.domain.util.ErrorHandling.Companion.ERROR_AUTH_TOKEN_INVALID
811
import com.codingwithmitch.openapi.business.domain.util.ErrorHandling.Companion.ERROR_EDIT_BLOG_NEED_PERMISSION
@@ -29,35 +32,39 @@ class IsAuthorOfBlogPost(
2932
if(authToken == null){
3033
throw Exception(ERROR_AUTH_TOKEN_INVALID)
3134
}
32-
val response = service.isAuthorOfBlogPost(
33-
"Token ${authToken.token}",
34-
slug
35-
)
36-
if(response.response == RESPONSE_HAS_PERMISSION_TO_EDIT){
37-
emit(DataState.data(response = null, true))
38-
}
39-
else if(response.response == GENERIC_ERROR
40-
&& response.errorMessage == RESPONSE_NO_PERMISSION_TO_EDIT){
41-
emit(DataState.data(
42-
response = Response(
43-
message = response.errorMessage,
44-
uiComponentType = UIComponentType.None(),
45-
messageType = MessageType.Success()
46-
),
47-
data = false
48-
))
49-
}
50-
else if(response.response == GENERIC_ERROR){
51-
emit(DataState.data(
52-
response = Response(
53-
message = response.errorMessage,
54-
uiComponentType = UIComponentType.Dialog(),
55-
messageType = MessageType.Error()
56-
),
57-
data = false
58-
))
59-
}
60-
else{
35+
try{ // catch network exception
36+
val response = service.isAuthorOfBlogPost(
37+
"Token ${authToken.token}",
38+
slug
39+
)
40+
if(response.response == RESPONSE_HAS_PERMISSION_TO_EDIT){
41+
emit(DataState.data(response = null, true))
42+
}
43+
else if(response.response == GENERIC_ERROR
44+
&& response.errorMessage == RESPONSE_NO_PERMISSION_TO_EDIT){
45+
emit(DataState.data(
46+
response = Response(
47+
message = response.errorMessage,
48+
uiComponentType = UIComponentType.None(),
49+
messageType = MessageType.Success()
50+
),
51+
data = false
52+
))
53+
}
54+
else if(response.response == GENERIC_ERROR){
55+
emit(DataState.data(
56+
response = Response(
57+
message = response.errorMessage,
58+
uiComponentType = UIComponentType.Dialog(),
59+
messageType = MessageType.Error()
60+
),
61+
data = false
62+
))
63+
}
64+
else{
65+
emit(DataState.data(response = null, false))
66+
}
67+
}catch (e: Exception){
6168
emit(DataState.data(response = null, false))
6269
}
6370
}.catch { e ->

app/src/main/java/com/codingwithmitch/openapi/business/interactors/blog/SearchBlogs.kt

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import com.codingwithmitch.openapi.presentation.main.blog.list.BlogFilterOptions
1010
import com.codingwithmitch.openapi.presentation.main.blog.list.BlogOrderOptions
1111
import com.codingwithmitch.openapi.business.domain.util.DataState
1212
import com.codingwithmitch.openapi.business.domain.util.ErrorHandling.Companion.ERROR_AUTH_TOKEN_INVALID
13+
import com.codingwithmitch.openapi.business.domain.util.MessageType
14+
import com.codingwithmitch.openapi.business.domain.util.Response
15+
import com.codingwithmitch.openapi.business.domain.util.UIComponentType
1316
import kotlinx.coroutines.flow.Flow
1417
import kotlinx.coroutines.flow.catch
1518
import kotlinx.coroutines.flow.flow
@@ -34,20 +37,33 @@ class SearchBlogs(
3437
}
3538
// get Blogs from network
3639
val filterAndOrder = order.value + filter.value // Ex: -date_updated
37-
val blogs = service.searchListBlogPosts(
38-
"Token ${authToken.token}",
39-
query = query,
40-
ordering = filterAndOrder,
41-
page = page
42-
).results.map { it.toBlogPost() }
43-
44-
// Insert into cache
45-
for(blog in blogs){
46-
try{
47-
cache.insert(blog.toEntity())
48-
}catch (e: Exception){
49-
e.printStackTrace()
40+
41+
try{ // catch network exception
42+
val blogs = service.searchListBlogPosts(
43+
"Token ${authToken.token}",
44+
query = query,
45+
ordering = filterAndOrder,
46+
page = page
47+
).results.map { it.toBlogPost() }
48+
49+
// Insert into cache
50+
for(blog in blogs){
51+
try{
52+
cache.insert(blog.toEntity())
53+
}catch (e: Exception){
54+
e.printStackTrace()
55+
}
5056
}
57+
}catch (e: Exception){
58+
emit(
59+
DataState.error<List<BlogPost>>(
60+
response = Response(
61+
message = "Unable to update the cache.",
62+
uiComponentType = UIComponentType.None(),
63+
messageType = MessageType.Error()
64+
)
65+
)
66+
)
5167
}
5268

5369
// emit from cache

0 commit comments

Comments
 (0)