Skip to content

Commit 60140db

Browse files
Kishan Kumar MauryaKishan Kumar Maurya
authored andcommitted
Caching of data for 1 day & error handling
1 parent 63ca4e6 commit 60140db

File tree

10 files changed

+107
-17
lines changed

10 files changed

+107
-17
lines changed

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ android {
2828
sourceCompatibility JavaVersion.VERSION_1_8
2929
targetCompatibility JavaVersion.VERSION_1_8
3030
}
31+
// For Kotlin projects
32+
kotlinOptions {
33+
jvmTarget = "1.8"
34+
}
3135
}
3236

3337
dependencies {

app/src/main/java/com/example/githubfirebaseissue/api/ApiConstant.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@ class ApiConstant {
44

55
companion object{
66
const val GITHUB_BASE_URL = "https://api.github.com/repos/firebase/firebase-ios-sdk/"
7+
const val HEADER_CACHE_CONTROL = "Cache-Control"
8+
const val HEADER_PRAGMA = "Pragma"
79
}
810
}

app/src/main/java/com/example/githubfirebaseissue/base/BaseFragment.kt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import android.widget.Toast
1111
import androidx.fragment.app.Fragment
1212
import com.example.githubfirebaseissue.R
1313
import dagger.android.support.AndroidSupportInjection
14+
import okio.IOException
15+
import retrofit2.HttpException
1416

1517

1618
abstract class BaseFragment : Fragment() {
@@ -55,6 +57,19 @@ abstract class BaseFragment : Fragment() {
5557
dialog.show()
5658
}
5759

60+
protected fun handleError(th: Throwable) {
61+
val error = when (th) {
62+
is HttpException -> when (th.code()) {
63+
401 -> getString(R.string.error_unauthorized)
64+
500 -> getString(R.string.error_server)
65+
else -> getString(R.string.error_server)
66+
}
67+
is IOException -> getString(R.string.internet_error)
68+
else -> getString(R.string.error_server)
69+
}
70+
onError(error)
71+
}
72+
5873
/**
5974
* This method is called after view has been created.
6075
* This method should be used to initialize all views that are needed to be created (and recreated after fragment is reattached)
@@ -64,6 +79,6 @@ abstract class BaseFragment : Fragment() {
6479

6580
abstract fun getLayoutRes(): Int
6681
abstract fun showLoadingState(loading: Boolean)
67-
abstract fun onError(th: Throwable)
82+
abstract fun onError(message: String)
6883

6984
}
Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,95 @@
11
package com.example.githubfirebaseissue.di.module
22

3+
import android.util.Log
4+
import com.example.githubfirebaseissue.GithubApplication
5+
import com.example.githubfirebaseissue.api.ApiConstant.Companion.HEADER_CACHE_CONTROL
6+
import com.example.githubfirebaseissue.api.ApiConstant.Companion.HEADER_PRAGMA
37
import com.example.githubfirebaseissue.di.scope.AppScope
48
import dagger.Module
59
import dagger.Provides
6-
import okhttp3.OkHttpClient
10+
import okhttp3.*
711
import okhttp3.logging.HttpLoggingInterceptor
12+
import java.io.File
13+
import okhttp3.CacheControl
14+
import com.example.githubfirebaseissue.helper.ApplicationUtil
15+
import java.util.concurrent.TimeUnit
16+
817

918
@Module
1019
class InterceptorModule {
1120

1221
@Provides
1322
@AppScope
14-
fun provideOkHttpClient(interceptor: HttpLoggingInterceptor): OkHttpClient {
23+
fun provideOkHttpClient(
24+
context: GithubApplication,
25+
interceptor: HttpLoggingInterceptor
26+
): OkHttpClient {
1527
return OkHttpClient.Builder()
1628
.addInterceptor(interceptor)
29+
.addInterceptor { provideOfflineCacheInterceptor(context, it) }
30+
.addNetworkInterceptor { provideCacheInterceptor(context, it) }
31+
.cache(provideCache(context))
1732
.build()
1833
}
1934

35+
2036
@Provides
2137
@AppScope
2238
fun provideLoggingInterceptor(): HttpLoggingInterceptor {
2339
val httpLoggingInterceptor = HttpLoggingInterceptor()
2440
httpLoggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
2541
return httpLoggingInterceptor
2642
}
43+
44+
45+
private fun provideCache(context: GithubApplication): Cache? {
46+
var cache: Cache? = null
47+
try {
48+
cache = Cache(File(context.cacheDir, "http-cache"), 10 * 1024 * 1024)
49+
} catch (e: Exception) {
50+
Log.e("Cache", "Could not create Cache!")
51+
}
52+
return cache
53+
}
54+
55+
private fun provideOfflineCacheInterceptor(
56+
context: GithubApplication,
57+
chain: Interceptor.Chain
58+
): Response {
59+
var request = chain.request()
60+
61+
if (!ApplicationUtil.hasNetwork(context)) {
62+
val cacheControl = CacheControl.Builder()
63+
.maxStale(1, TimeUnit.DAYS)
64+
.build()
65+
66+
request = request.newBuilder()
67+
.removeHeader(HEADER_PRAGMA)
68+
.removeHeader(HEADER_CACHE_CONTROL)
69+
.cacheControl(cacheControl)
70+
.build()
71+
}
72+
73+
return chain.proceed(request)
74+
}
75+
76+
private fun provideCacheInterceptor(
77+
context: GithubApplication,
78+
chain: Interceptor.Chain
79+
): Response {
80+
val response = chain.proceed(chain.request())
81+
val cacheControl: CacheControl = if (ApplicationUtil.hasNetwork(context)) {
82+
CacheControl.Builder().maxAge(0, TimeUnit.SECONDS).build()
83+
} else {
84+
CacheControl.Builder()
85+
.maxStale(1, TimeUnit.DAYS)
86+
.build()
87+
}
88+
89+
return response.newBuilder()
90+
.removeHeader(HEADER_PRAGMA)
91+
.removeHeader(HEADER_CACHE_CONTROL)
92+
.header(HEADER_CACHE_CONTROL, cacheControl.toString())
93+
.build()
94+
}
2795
}

app/src/main/java/com/example/githubfirebaseissue/ui/fragment/AbstractCommentsFragment.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ abstract class AbstractCommentsFragment : BaseFragment() {
3737

3838
private fun observeDataChange() {
3939
viewModel.loadingState.observe(viewLifecycleOwner, Observer { showLoadingState(it) })
40-
viewModel.apiError.observe(viewLifecycleOwner, EventObserver { onError(it) })
40+
viewModel.apiError.observe(viewLifecycleOwner, EventObserver { handleError(it) })
4141
viewModel.commentsLiveData.observe(viewLifecycleOwner, EventObserver {
4242
if (it.isNotEmpty())
4343
setCommentsData(it)

app/src/main/java/com/example/githubfirebaseissue/ui/fragment/AbstractIssueFragment.kt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@ abstract class AbstractIssueFragment : BaseFragment() {
2424

2525
override fun onCreate(savedInstanceState: Bundle?) {
2626
super.onCreate(savedInstanceState)
27-
if (ApplicationUtil.hasNetwork(context!!))
28-
viewModel.fetchFireBaseIosIssueList()
29-
else
30-
showNetworkError()
27+
viewModel.fetchFireBaseIosIssueList()
3128
}
3229

3330
override fun viewInitialization(view: View) {
@@ -37,14 +34,13 @@ abstract class AbstractIssueFragment : BaseFragment() {
3734

3835
private fun observeDataChange() {
3936
viewModel.loadingState.observe(viewLifecycleOwner, Observer { showLoadingState(it) })
40-
viewModel.apiError.observe(viewLifecycleOwner, EventObserver { onError(it) })
37+
viewModel.apiError.observe(viewLifecycleOwner, EventObserver { handleError(it) })
4138
viewModel.issueLiveData.observe(viewLifecycleOwner, EventObserver {
4239
setIssuesData(it)
4340
})
4441
}
4542

4643

4744
abstract fun setIssuesData(list: List<Issue>)
48-
abstract fun showNetworkError()
4945

5046
}

app/src/main/java/com/example/githubfirebaseissue/ui/fragment/CommentsFragment.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ class CommentsFragment : AbstractCommentsFragment() {
6060
progress.visibility = View.GONE
6161
}
6262

63-
override fun onError(th: Throwable) {
64-
th.message?.let { showToast(it) }
63+
override fun onError(message: String) {
64+
showToast(message)
6565
}
6666

6767
}

app/src/main/java/com/example/githubfirebaseissue/ui/fragment/IssuesFragment.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ class IssuesFragment : AbstractIssueFragment(), IAdapterCallback {
5858
progress.visibility = View.GONE
5959
}
6060

61-
override fun onError(th: Throwable) {
62-
th.message?.let { showToast(it) }
61+
override fun onError(message: String) {
62+
showToast(message)
6363
}
6464

6565

@@ -70,7 +70,4 @@ class IssuesFragment : AbstractIssueFragment(), IAdapterCallback {
7070
)
7171
}
7272

73-
override fun showNetworkError() {
74-
showErrorDialog(getString(R.string.internet_error),getString(R.string.close))
75-
}
7673
}

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@
44
<string name="go_back">Go Back</string>
55
<string name="internet_error">Please check Internet</string>
66
<string name="close">Close</string>
7+
<string name="error_unauthorized">User Not Authorised</string>
8+
<string name="error_server">Something went wrong. Please try later</string>
79
</resources>

0 commit comments

Comments
 (0)