Skip to content

Commit

Permalink
Merge pull request #233 from myofficework000/lesson_19
Browse files Browse the repository at this point in the history
added lesson 19
  • Loading branch information
myofficework000 authored Jan 26, 2024
2 parents 036bde8 + 3fec622 commit 5d25594
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.jetpack_compose_all_in_one.lessons.lesson_19

import retrofit2.http.GET

interface DogApiService {
@GET("api/breeds/image/random")
suspend fun getDogImage(): DogResponse
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.example.jetpack_compose_all_in_one.lessons.lesson_19

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import coil.compose.rememberImagePainter

@Composable
fun DogImageScreen(viewModel: DogViewModel = hiltViewModel()) {
val dogUrl by viewModel.dogImage.observeAsState()

Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = androidx.compose.ui.Alignment.CenterHorizontally
) {
dogUrl?.let { url ->
Image(
modifier = Modifier.height(300.dp).fillMaxWidth(),
painter = rememberImagePainter(data = dogUrl),
contentDescription = "Dog Image"
)
}

Spacer(modifier = Modifier.height(16.dp))

Button(onClick = { viewModel.getRandomDogImage() }) {
Text("Fetch Dog Image")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.example.jetpack_compose_all_in_one.lessons.lesson_19

data class DogResponse(val message: String, val status: String)
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.example.jetpack_compose_all_in_one.lessons.lesson_19

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.liveData
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class DogViewModel @Inject constructor(private val dogApiService: DogApiService) : ViewModel() {

val dogImage = MutableLiveData<String>()

init {
getRandomDogImage()
}

fun getRandomDogImage() {
viewModelScope.launch {
try {
val response = dogApiService.getDogImage()
if (response.status == "success") {
dogImage.value = response.message
} else {
Log.d("TAG", "getRandomDogImage: ${response.status}")
}
} catch (e: Exception) {
e.stackTrace
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.example.jetpack_compose_all_in_one.lessons.lesson_19

import android.content.Context
import android.net.ConnectivityManager
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import okhttp3.Cache
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object NetworkModule {

@Provides
@Singleton
fun provideDogApiService(@ApplicationContext context: Context): DogApiService {
val cacheSize = (10 * 1024 * 1024).toLong()
val myCache = Cache(context.cacheDir, cacheSize)
val okHttpClient = OkHttpClient.Builder()
.cache(myCache)
.addInterceptor { chain ->
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val activeNetwork = connectivityManager.activeNetworkInfo
val isNetworkAvailable = activeNetwork != null && activeNetwork.isConnected
var request = chain.request()
request = if (isNetworkAvailable) {
request.newBuilder().header("Cache-Control", "public, max-age=" + 5).build()
} else {
request.newBuilder().header("Cache-Control", "public, only-if-cached, max-stale=" + 60 * 60 * 24 * 7).build()
}
chain.proceed(request)
}
.build()

return Retrofit.Builder()
.baseUrl("https://dog.ceo/")
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(DogApiService::class.java)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ import com.example.jetpack_compose_all_in_one.lessons.lesson_15.Lesson_15
import com.example.jetpack_compose_all_in_one.lessons.lesson_16.Lesson_16
import com.example.jetpack_compose_all_in_one.lessons.lesson_17.Lesson_17
import com.example.jetpack_compose_all_in_one.lessons.lesson_18.Lesson_18
import com.example.jetpack_compose_all_in_one.lessons.lesson_19.DogImageScreen
import com.example.jetpack_compose_all_in_one.lessons.lesson_2.Lesson_2_Chapter_2_Screen
import com.example.jetpack_compose_all_in_one.lessons.lesson_2.Lesson_2_Chapter_4_Image
import com.example.jetpack_compose_all_in_one.lessons.lesson_2.Lesson_2_Chapter_5_Progressbar
Expand Down Expand Up @@ -434,6 +435,10 @@ fun MainContainerOfApp(
Lesson_18()
}

composable(NavDes.Lesson19.route()){
DogImageScreen()
}

composable(NavDes.L6Chapter1.route()) {
Lesson_6_ThemeChange()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ object NavConstants {
const val LESSON_18 = "LESSON_18"
const val LESSON_18_ABOUT = "Lesson 18: Compose UI Testing"

const val LESSON_19 = "LESSON_19"
const val LESSON_19_ABOUT = "Lesson 19: Caching with OkHttp Interceptor"

const val QUOTE_2 = "quote2"
const val QUOTE_2_ABOUT = "Swipe Quotes"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ import com.example.jetpack_compose_all_in_one.utils.navigation.NavConstants.LESS
import com.example.jetpack_compose_all_in_one.utils.navigation.NavConstants.LESSON_17_ABOUT
import com.example.jetpack_compose_all_in_one.utils.navigation.NavConstants.LESSON_18
import com.example.jetpack_compose_all_in_one.utils.navigation.NavConstants.LESSON_18_ABOUT
import com.example.jetpack_compose_all_in_one.utils.navigation.NavConstants.LESSON_19
import com.example.jetpack_compose_all_in_one.utils.navigation.NavConstants.LESSON_19_ABOUT
import com.example.jetpack_compose_all_in_one.utils.navigation.NavConstants.LESSON_1_ABOUT
import com.example.jetpack_compose_all_in_one.utils.navigation.NavConstants.LESSON_2_CHAPTER_1
import com.example.jetpack_compose_all_in_one.utils.navigation.NavConstants.LESSON_2_CHAPTER_1_ABOUT
Expand Down Expand Up @@ -303,6 +305,9 @@ sealed class NavDes(val data: INavigationDrawerItem, val customAppBarStringId: I
object Lesson18 :
NavDes(NavigationDrawerData(LESSON_18, LESSON_18_ABOUT))

object Lesson19 :
NavDes(NavigationDrawerData(LESSON_19, LESSON_19_ABOUT))

object Lesson14DropDownMenu :
NavDes(NavigationDrawerData(LESSON_14_DROP_DOWN_MENU, LESSON_14_DROP_DOWN_MENU_ABOUT))

Expand Down Expand Up @@ -512,7 +517,8 @@ sealed class NavDes(val data: INavigationDrawerItem, val customAppBarStringId: I
Lesson15,
Lesson16,
Lesson17,
Lesson18
Lesson18,
Lesson19
), LESSONS
)
)
Expand Down

0 comments on commit 5d25594

Please sign in to comment.