-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #233 from myofficework000/lesson_19
added lesson 19
- Loading branch information
Showing
8 changed files
with
152 additions
and
1 deletion.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
app/src/main/java/com/example/jetpack_compose_all_in_one/lessons/lesson_19/DogApiService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
43 changes: 43 additions & 0 deletions
43
app/src/main/java/com/example/jetpack_compose_all_in_one/lessons/lesson_19/DogImageScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
app/src/main/java/com/example/jetpack_compose_all_in_one/lessons/lesson_19/DogResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
35 changes: 35 additions & 0 deletions
35
app/src/main/java/com/example/jetpack_compose_all_in_one/lessons/lesson_19/DogViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
app/src/main/java/com/example/jetpack_compose_all_in_one/lessons/lesson_19/NetworkModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters