Skip to content

Commit

Permalink
Add network_security_config.xml.
Browse files Browse the repository at this point in the history
Replace enums with sealed class.
  • Loading branch information
kanch231004 committed Nov 14, 2021
1 parent 09914d2 commit 321c711
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 36 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ allOpen {
}

android {
compileSdkVersion 29
compileSdkVersion 30

dataBinding {
enabled = true
Expand All @@ -21,7 +21,7 @@ android {
defaultConfig {
applicationId "com.kanchanpal.newsfeed"
minSdkVersion 21
targetSdkVersion 29
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "com.kanchanpal.newsfeed.util.AppTestRunner"
Expand Down
14 changes: 7 additions & 7 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kanchanpal.newsfeed">


<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
android:name="com.kanchanpal.newsfeed.NewsApp"
android:name=".NewsApp"
android:allowBackup="true"
android:networkSecurityConfig="@xml/network_security_config"
android:hardwareAccelerated="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:largeHeap="true"
android:hardwareAccelerated="false"
android:theme="@style/AppTheme">
<activity android:name="com.kanchanpal.newsfeed.MainActivity">
<intent-filter>
Expand All @@ -25,4 +25,4 @@
</activity>
</application>

</manifest>
</manifest>
19 changes: 4 additions & 15 deletions app/src/main/java/com/kanchanpal/newsfeed/api/Status.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
package com.kanchanpal.newsfeed.api

enum class Status {
RUNNING,
SUCCESS,
FAILED
}

@Suppress("DataClassPrivateConstructor")
data class NetworkState private constructor(
val status: Status,
val msg: String? = null) {
companion object {
val LOADED = NetworkState(Status.SUCCESS)
val LOADING = NetworkState(Status.RUNNING)
fun error(msg: String?) = NetworkState(Status.FAILED, msg)
}
sealed class NetworkState {
object LOADED : NetworkState()
object LOADING : NetworkState()
data class ERROR(val msg: String): NetworkState()
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.kanchanpal.newsfeed.data.newsSet

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.paging.PageKeyedDataSource
import com.kanchanpal.newsfeed.api.NetworkState
Expand All @@ -10,7 +11,6 @@ import com.kanchanpal.newsfeed.data.dao.NewsDao
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import timber.log.Timber
import javax.inject.Inject

class NewsPageDataSource @Inject constructor(
Expand Down Expand Up @@ -49,7 +49,7 @@ class NewsPageDataSource @Inject constructor(
coroutineScope.launch(getJobErrorHandler()) {
when (val response = remoteDataSource.fetchNewsList(apiKey, page, pageSize)) {
is Result.Error -> {
networkState.postValue(NetworkState.error(response.message ?: "Unknown error"))
networkState.postValue(NetworkState.ERROR(response.message ?: "Unknown error"))
postError(response.message)
}
is Result.Success -> {
Expand All @@ -67,6 +67,6 @@ class NewsPageDataSource @Inject constructor(
}

private fun postError(message: String?) {
Timber.e("An error happened: $message")
Log.e("NewsPageDataSource","An error happened: $message")
}
}
3 changes: 1 addition & 2 deletions app/src/main/java/com/kanchanpal/newsfeed/di/AppInjector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ object AppInjector {

}

override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle?) {

override fun onActivitySaveInstanceState(p0: Activity, p1: Bundle) {
}

override fun onActivityDestroyed(activity: Activity) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.kanchanpal.newsfeed.api.Status
import com.kanchanpal.newsfeed.api.NetworkState
import com.kanchanpal.newsfeed.commonUtil.ConnectivityUtil
import com.kanchanpal.newsfeed.databinding.FragmentNewsListBinding
import com.kanchanpal.newsfeed.di.Injectable
Expand Down Expand Up @@ -51,15 +51,15 @@ class NewsListFragment : Fragment(), Injectable {
private fun subscribeUI(adapter: NewsAdapter) {
val data = viewModel.newsList(isConnected)
data?.networkState?.observe(viewLifecycleOwner, Observer {
when(it.status) {
Status.RUNNING -> {
when(it) {
is NetworkState.LOADING -> {
progressBar.visibility = View.VISIBLE
}
Status.FAILED -> {
is NetworkState.ERROR -> {
progressBar.visibility = View.GONE
// Handle fail state
}
Status.SUCCESS -> {
is NetworkState.LOADED -> {
progressBar.visibility = View.GONE
}
}
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/xml/network_security_config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<debug-overrides>
<trust-anchors>
<!-- Trust user added CAs while debuggable only -->
<certificates src="user" />
</trust-anchors>
</debug-overrides>
</network-security-config>
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
buildscript {
ext {
// Sdk and tools
compileSdkVersion = 28
compileSdkVersion = 30
minSdkVersion = 21
targetSdkVersion = 28
targetSdkVersion = 30

// App dependencies
appCompatVersion = '1.1.0-beta01'
Expand Down

0 comments on commit 321c711

Please sign in to comment.