Skip to content

Commit

Permalink
unsplash api integration to show university images
Browse files Browse the repository at this point in the history
  • Loading branch information
JonibekXolmonov committed Apr 1, 2024
1 parent 4240542 commit 7f753aa
Show file tree
Hide file tree
Showing 13 changed files with 249 additions and 86 deletions.
2 changes: 1 addition & 1 deletion .idea/kotlinc.xml

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

31 changes: 20 additions & 11 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-kapt'
id 'com.google.dagger.hilt.android'
id 'com.google.devtools.ksp'
}

android {
Expand Down Expand Up @@ -55,7 +55,7 @@ android {
buildConfig true
}
composeOptions {
kotlinCompilerExtensionVersion '1.4.7'
kotlinCompilerExtensionVersion '1.5.11'
}
packagingOptions {
resources {
Expand All @@ -73,7 +73,7 @@ dependencies {

implementation 'androidx.core:core-ktx:1.12.0'
implementation platform('org.jetbrains.kotlin:kotlin-bom:1.8.0')
implementation 'androidx.activity:activity-compose:1.8.1'
implementation 'androidx.activity:activity-compose:1.8.2'
implementation platform('androidx.compose:compose-bom:2023.06.01')
implementation 'androidx.compose.ui:ui'
implementation 'androidx.compose.ui:ui-graphics'
Expand All @@ -90,12 +90,12 @@ dependencies {
//Dagger-Hilt
def hilt_version = "2.47"
implementation "com.google.dagger:hilt-android:$hilt_version"
kapt "com.google.dagger:hilt-compiler:$hilt_version"
kapt "androidx.hilt:hilt-compiler:1.1.0"
implementation "androidx.hilt:hilt-navigation-compose:1.1.0"
ksp "com.google.dagger:hilt-compiler:$hilt_version"
ksp "androidx.hilt:hilt-compiler:1.2.0"
implementation "androidx.hilt:hilt-navigation-compose:1.2.0"

// Coroutines
def coroutine_version = "1.5.2"
def coroutine_version = "1.8.0"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutine_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutine_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-play-services:$coroutine_version"
Expand All @@ -109,18 +109,27 @@ dependencies {
//Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.squareup.okhttp3:okhttp:4.11.0'
implementation 'com.squareup.okhttp3:okhttp:4.12.0'

//Coil
implementation "io.coil-kt:coil-compose:2.4.0"
implementation "io.coil-kt:coil-compose:2.6.0"

//Glide
implementation("com.github.bumptech.glide:compose:1.0.0-beta01")

//Extended Icons
implementation "androidx.compose.material:material-icons-extended:1.5.0"
implementation "androidx.compose.material:material-icons-extended:1.6.4"

//Browser View
implementation 'androidx.browser:browser:1.7.0'
implementation 'androidx.browser:browser:1.8.0'

//Permissions
implementation "com.google.accompanist:accompanist-permissions:0.23.1"

// Room
def roomVersion = "2.6.1"
implementation "androidx.room:room-runtime:$roomVersion"
implementation "androidx.room:room-ktx:$roomVersion"
ksp "androidx.room:room-compiler:$roomVersion"

}
16 changes: 16 additions & 0 deletions app/src/main/java/com/bera/josaahelpertool/di/AppModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package com.bera.josaahelpertool.di
import android.content.Context
import com.bera.josaahelpertool.network.CutoffApi
import com.bera.josaahelpertool.network.QuotesApi
import com.bera.josaahelpertool.network.UnsplashApi
import com.bera.josaahelpertool.network.connectivity.ConnectivityObserver
import com.bera.josaahelpertool.network.okhttp.CacheInterceptor
import com.bera.josaahelpertool.network.okhttp.ForceCacheInterceptor
import com.bera.josaahelpertool.repository.CutoffRepository
import com.bera.josaahelpertool.repository.UniversityImageRepository
import com.bera.josaahelpertool.utils.Constants
import dagger.Module
import dagger.Provides
Expand All @@ -28,6 +30,10 @@ object AppModule {
@Provides
fun provideCutoffRepository(api: CutoffApi) = CutoffRepository(api)

@Singleton
@Provides
fun provideUniversityImageRepository(api: UnsplashApi) = UniversityImageRepository(api)

@Singleton
@Provides
fun provideCutoffApi(@ApplicationContext appContext: Context, connectivityObserver: ConnectivityObserver): CutoffApi {
Expand Down Expand Up @@ -56,6 +62,16 @@ object AppModule {
.create(QuotesApi::class.java)
}

@Singleton
@Provides
fun provideUnsplashApi(): UnsplashApi {
return Retrofit.Builder()
.baseUrl(Constants.BASE_URL_UNSPLASH)
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(UnsplashApi::class.java)
}

@Singleton
@Provides
fun provideConnectivityObserver(@ApplicationContext appContext: Context): ConnectivityObserver =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.bera.josaahelpertool.models

import com.bera.josaahelpertool.models.ui.TopHalfItem
import com.google.gson.annotations.SerializedName

data class UniversityImageResponse(
val results: List<UniversityImage>
)

data class UniversityImage(
val description: String?,
@SerializedName("alt_description")
val altDescription: String,
val urls: Urls,
)

data class Urls(
val raw: String,
val full: String,
val regular: String,
val small: String,
val thumb: String,
@SerializedName("small_s3")
val smallS3: String
)

fun UniversityImage.toModel() =
TopHalfItem(imageUrl = urls.regular, name = description ?: altDescription)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.bera.josaahelpertool.models.ui

data class TopHalfItem(
val imageUrl: String,
val name: String
)
19 changes: 19 additions & 0 deletions app/src/main/java/com/bera/josaahelpertool/network/UnsplashApi.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.bera.josaahelpertool.network

import com.bera.josaahelpertool.models.UniversityImageResponse
import com.bera.josaahelpertool.utils.Constants.API_KEY_UNSPLASH
import com.bera.josaahelpertool.utils.Constants.QUERY
import retrofit2.http.GET
import retrofit2.http.Query
import javax.inject.Singleton

@Singleton
interface UnsplashApi {
@GET("search/photos")
suspend fun getUniversityImages(
@Query("page") page: Int = 1,
@Query("client_id") clientId: String = API_KEY_UNSPLASH,
@Query("query") query: String = QUERY,
@Query("per_page") perPage: Int = 30
): UniversityImageResponse
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bera.josaahelpertool.repository

import com.bera.josaahelpertool.models.Quotes
import com.bera.josaahelpertool.models.UniversityImageResponse
import com.bera.josaahelpertool.network.QuotesApi
import javax.inject.Inject

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.bera.josaahelpertool.repository

import android.util.Log
import com.bera.josaahelpertool.models.UniversityImageResponse
import com.bera.josaahelpertool.network.UnsplashApi
import javax.inject.Inject
import kotlin.random.Random

class UniversityImageRepository @Inject constructor(private val unsplashApi: UnsplashApi) {
suspend fun getUniversityImages(): UniversityImageResponse {
val page = Random.nextInt(1, 4)
return unsplashApi.getUniversityImages(page)
}
}
Loading

0 comments on commit 7f753aa

Please sign in to comment.