Skip to content

Commit

Permalink
Error handling, splashScreen and app icon
Browse files Browse the repository at this point in the history
  • Loading branch information
a7asoft committed Nov 24, 2022
1 parent 016e1c6 commit b142e1a
Show file tree
Hide file tree
Showing 65 changed files with 513 additions and 71 deletions.
4 changes: 2 additions & 2 deletions .idea/assetWizardSettings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions .idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ dependencies {

implementation "com.airbnb.android:lottie:5.0.3"

// Alerts
implementation 'com.github.tapadoo:alerter:7.2.4'

//splashScreen
implementation 'androidx.core:core-splashscreen:1.0.0'

testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
Expand Down
7 changes: 3 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

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

<application
Expand All @@ -13,13 +14,11 @@
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.RandomJoke"
android:theme="@style/Theme.AppSplash"
tools:targetApi="31">
<activity
android:name=".presentation.main.activities.MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.RandomJoke">
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
16 changes: 15 additions & 1 deletion app/src/main/java/com/awto/randomjoke/MyApplication.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
package com.awto.randomjoke

import android.app.Application
import android.util.Log
import com.awto.randomjoke.util.NetworkMonitoringUtil
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
class MyApplication : Application(){
class MyApplication : Application() {

val TAG: String = MyApplication::class.java.simpleName
var mNetworkMonitoringUtil: NetworkMonitoringUtil? = null

override fun onCreate() {
super.onCreate()

Log.d(
TAG,
"onCreate() called"
)

mNetworkMonitoringUtil = NetworkMonitoringUtil(applicationContext)
mNetworkMonitoringUtil!!.checkNetworkState()
mNetworkMonitoringUtil!!.registerNetworkCallbackEvents()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.awto.randomjoke.data.local;

import android.os.Looper;
import android.util.Log;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

public class NetworkStateManager {
public static final String TAG = NetworkStateManager.class.getSimpleName();

private static NetworkStateManager INSTANCE;
private static final MutableLiveData<Boolean> activeNetworkStatusMLD = new MutableLiveData<>();

private NetworkStateManager() {}

public static synchronized NetworkStateManager getInstance() {
if (INSTANCE == null) {
Log.d(TAG, "getInstance() called: Creating new instance");
INSTANCE = new NetworkStateManager();
}
return INSTANCE;
}

/**
* Updates the active network status live-data
*/
public void setNetworkConnectivityStatus(boolean connectivityStatus) {
Log.d(TAG, "setNetworkConnectivityStatus() called with: connectivityStatus = [" + connectivityStatus + "]");

if (Looper.myLooper() == Looper.getMainLooper()) {
activeNetworkStatusMLD.setValue(connectivityStatus);
} else {
activeNetworkStatusMLD.postValue(connectivityStatus);
}
}

/**
* Returns the current network status
*/
public LiveData<Boolean> getNetworkConnectivityStatus() {
Log.d(TAG, "getNetworkConnectivityStatus() called");
return activeNetworkStatusMLD;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.awto.randomjoke.data.repository

import android.util.Log
import com.awto.randomjoke.data.remote.JokeApi
import com.awto.randomjoke.data.remote.model.ErrorModel
import com.awto.randomjoke.data.remote.model.JokeResponseModel
Expand All @@ -12,19 +13,23 @@ import kotlinx.coroutines.flow.flow
import javax.inject.Inject

class JokeRepositoryImpl @Inject constructor(private val jokeApi: JokeApi) : JokeRepository {
override suspend fun getJoke(): Flow<BaseResult<JokeResponseModel, ErrorModel>> {
override suspend fun getJoke(): Flow<BaseResult<JokeResponseModel, ErrorModel, Exception>> {
return flow {
val response = jokeApi.getRandomJoke()
if (response.isSuccessful && response.code() == 200) {
val body = response.body()!!
emit(BaseResult.Success(body))
} else {
val type = object : TypeToken<ErrorModel>() {}.type
val err = Gson().fromJson<ErrorModel>(
response.errorBody()!!.charStream(),
type
)!!
emit(BaseResult.Error(err))
try {
val response = jokeApi.getRandomJoke()
if (response.isSuccessful && response.code() == 200) {
val body = response.body()!!
emit(BaseResult.Success(body))
} else {
val type = object : TypeToken<ErrorModel>() {}.type
val err = Gson().fromJson<ErrorModel>(
response.errorBody()!!.charStream(),
type
)!!
emit(BaseResult.Error(err))
}
} catch (e: Exception) {
emit(BaseResult.Exception(e))
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/com/awto/randomjoke/di/AppModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ object AppModule {
val interceptor = HttpLoggingInterceptor()
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY)
return OkHttpClient.Builder().apply {
connectTimeout(60, TimeUnit.SECONDS)
readTimeout(60, TimeUnit.SECONDS)
writeTimeout(60, TimeUnit.SECONDS)
connectTimeout(10, TimeUnit.SECONDS)
readTimeout(10, TimeUnit.SECONDS)
writeTimeout(10, TimeUnit.SECONDS)
addInterceptor(loggingInterceptor)
}.build()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import com.awto.randomjoke.util.BaseResult
import kotlinx.coroutines.flow.Flow

interface JokeRepository {
suspend fun getJoke(): Flow<BaseResult<JokeResponseModel, ErrorModel>>
suspend fun getJoke(): Flow<BaseResult<JokeResponseModel, ErrorModel, Exception>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import kotlinx.coroutines.flow.Flow
import javax.inject.Inject

class GetRandomJokeUseCase @Inject constructor(private val jokeRepository: JokeRepository){
suspend fun invoke(): Flow<BaseResult<JokeResponseModel, ErrorModel>> {
suspend fun invoke(): Flow<BaseResult<JokeResponseModel, ErrorModel, Exception>> {
return jokeRepository.getJoke()
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.awto.randomjoke.presentation.main.activities

import android.os.Bundle
import com.google.android.material.snackbar.Snackbar
import androidx.appcompat.app.AppCompatActivity
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.core.view.WindowCompat
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
Expand All @@ -22,10 +22,13 @@ class MainActivity : AppCompatActivity() {
WindowCompat.setDecorFitsSystemWindows(window, false)
super.onCreate(savedInstanceState)

installSplashScreen()

binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

setSupportActionBar(binding.toolbar)
supportActionBar?.setDisplayShowTitleEnabled(false)

val navController = findNavController(R.id.nav_host_fragment_content_main)
appBarConfiguration = AppBarConfiguration(navController.graph)
Expand Down
Loading

0 comments on commit b142e1a

Please sign in to comment.