-
Notifications
You must be signed in to change notification settings - Fork 0
Hilt example - Part 1 #110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fedeturazzini
wants to merge
29
commits into
master
Choose a base branch
from
fedetura/hilt
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
4851fbf
hilt library
fedeturazzini fb0413d
hilt base imple
fedeturazzini 660df80
strings for hilt
fedeturazzini 17e9a9e
adding hilt activity for example
fedeturazzini 006ab63
koin folder
fedeturazzini e948079
imple recipe part 1
fedeturazzini 09bc105
Cleaning code
fedeturazzini 73a3e21
cleaning
fedeturazzini f4839ff
clean
fedeturazzini 7738e25
bye kapt verbose
fedeturazzini 927c10c
cleaning imports
fedeturazzini f1581d2
code smell
fedeturazzini adfca45
code smell
fedeturazzini dd9983f
code smell :)
fedeturazzini 8953937
+ smell code fixes
fedeturazzini 76dcbac
code rev Juli
fedeturazzini 59234a2
code rev juli
fedeturazzini e727425
code rev tomi
fedeturazzini 7f4bca2
code rev tomi
fedeturazzini 3204004
code rev tomi
fedeturazzini 21226ce
ordered in lexicographic order
fedeturazzini 189be0d
clean
fedeturazzini 5eb0a74
code rev tomi
fedeturazzini 25036ae
code rev tomi
fedeturazzini 86dfc0d
code rev tomi
fedeturazzini dfea7cc
code rev juan
fedeturazzini 0ab2c14
cod rev
fedeturazzini b2852a4
clean spaces
fedeturazzini 37586d5
changes
fedeturazzini File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
5 changes: 4 additions & 1 deletion
5
app/src/main/java/com/rocketinsights/android/RocketApplication.kt
This file contains hidden or 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
19 changes: 19 additions & 0 deletions
19
app/src/main/java/com/rocketinsights/android/di/hilt/AppModule.kt
This file contains hidden or 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,19 @@ | ||
| package com.rocketinsights.android.di.hilt | ||
|
|
||
| import android.content.Context | ||
| import com.rocketinsights.android.RocketApplication | ||
| import dagger.Module | ||
| import dagger.Provides | ||
| import dagger.hilt.InstallIn | ||
| import dagger.hilt.android.qualifiers.ApplicationContext | ||
| import dagger.hilt.components.SingletonComponent | ||
| import javax.inject.Singleton | ||
|
|
||
| @Module | ||
| @InstallIn(SingletonComponent::class) | ||
| object AppModule { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need a module just to provide the application context? |
||
| @Singleton | ||
| @Provides | ||
| fun provideApplication(@ApplicationContext app: Context): RocketApplication = | ||
| app as RocketApplication | ||
| } | ||
57 changes: 57 additions & 0 deletions
57
app/src/main/java/com/rocketinsights/android/di/hilt/HiltNetworkModule.kt
This file contains hidden or 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,57 @@ | ||
| package com.rocketinsights.android.di.hilt | ||
|
|
||
| import com.google.gson.GsonBuilder | ||
| import com.rocketinsights.android.models.recipe_hilt.RecipeDtoMapper | ||
| import com.rocketinsights.android.network.hiltexample.RecipeService | ||
| import dagger.Module | ||
| import dagger.Provides | ||
| import dagger.hilt.InstallIn | ||
| import dagger.hilt.components.SingletonComponent | ||
| import okhttp3.Interceptor | ||
| import okhttp3.OkHttpClient | ||
| import retrofit2.Retrofit | ||
| import retrofit2.converter.gson.GsonConverterFactory | ||
| import javax.inject.Singleton | ||
|
|
||
| @Module | ||
| @InstallIn(SingletonComponent::class) | ||
| object HiltNetworkModule { | ||
|
|
||
| private var okHttpClient: OkHttpClient? = null | ||
|
|
||
| @Singleton | ||
| @Provides | ||
| fun providesRecipeMapper(): RecipeDtoMapper = RecipeDtoMapper() | ||
|
|
||
| @Singleton | ||
| @Provides | ||
| fun provideRecipeService(): RecipeService { | ||
| return Retrofit.Builder() | ||
| .baseUrl("https://food2fork.ca/api/recipe/") | ||
| .addConverterFactory(GsonConverterFactory.create(GsonBuilder().create())) | ||
| .client(buildClientHttp()) | ||
| .build() | ||
| .create(RecipeService::class.java) | ||
| } | ||
|
|
||
| private fun buildClientHttp(): OkHttpClient? { | ||
| okHttpClient = OkHttpClient.Builder() | ||
| .addInterceptor(buildInterceptor()) | ||
| .build() | ||
|
|
||
| return okHttpClient | ||
| } | ||
|
|
||
| private fun buildInterceptor() = Interceptor { chain -> | ||
| var request = chain.request() | ||
|
|
||
| request = request.newBuilder() | ||
| .addHeader( | ||
| "Authorization", | ||
| "Token 9c8b06d329136da358c2d00e76946b0111ce2c48" | ||
| ) | ||
| .build() | ||
|
|
||
| chain.proceed(request = request) | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
app/src/main/java/com/rocketinsights/android/di/hilt/RepositoryModule.kt
This file contains hidden or 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,28 @@ | ||
| package com.rocketinsights.android.di.hilt | ||
|
|
||
| import com.rocketinsights.android.models.recipe_hilt.RecipeDtoMapper | ||
| import com.rocketinsights.android.network.hiltexample.RecipeService | ||
| import com.rocketinsights.android.repos.RecipeRepository | ||
| import com.rocketinsights.android.repos.RecipeRepositoryImplementation | ||
| import dagger.Module | ||
| import dagger.Provides | ||
| import dagger.hilt.InstallIn | ||
| import dagger.hilt.components.SingletonComponent | ||
| import javax.inject.Singleton | ||
|
|
||
| @Module | ||
| @InstallIn(SingletonComponent::class) | ||
| object RepositoryModule { | ||
|
|
||
| @Singleton | ||
| @Provides | ||
| fun provideRecipeRepository( | ||
| recipeService: RecipeService, | ||
| recipeMapper: RecipeDtoMapper, | ||
| ): RecipeRepository { | ||
| return RecipeRepositoryImplementation( | ||
| recipeService = recipeService, | ||
| mapper = recipeMapper | ||
| ) | ||
| } | ||
| } |
2 changes: 1 addition & 1 deletion
2
...om/rocketinsights/android/di/KoinUtils.kt → ...cketinsights/android/di/koin/KoinUtils.kt
This file contains hidden or 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
13 changes: 13 additions & 0 deletions
13
app/src/main/java/com/rocketinsights/android/models/recipe_hilt/Recipe.kt
This file contains hidden or 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,13 @@ | ||
| package com.rocketinsights.android.models.recipe_hilt | ||
|
|
||
| data class Recipe( | ||
| val id: Int, | ||
| val title: String, | ||
| val publisher: String, | ||
| val featuredImage: String, | ||
| val rating: Int = 0, | ||
| val sourceUrl: String, | ||
| val ingredients: List<String>, | ||
| val dateAdded: String, | ||
| val dateUpdated: String | ||
| ) |
19 changes: 19 additions & 0 deletions
19
app/src/main/java/com/rocketinsights/android/models/recipe_hilt/RecipeDto.kt
This file contains hidden or 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,19 @@ | ||
| package com.rocketinsights.android.models.recipe_hilt | ||
|
|
||
| import com.google.gson.annotations.SerializedName | ||
|
|
||
| data class RecipeDto( | ||
| var pk: Int, | ||
| var title: String, | ||
| var publisher: String, | ||
| @SerializedName("featured_image") | ||
| var featuredImage: String, | ||
| var rating: Int = 0, | ||
| @SerializedName("source_url") | ||
| var sourceUrl: String, | ||
| var ingredients: List<String> = emptyList(), | ||
| @SerializedName("date_added") | ||
| var dateAdded: String, | ||
| @SerializedName("date_updated") | ||
| var dateUpdated: String, | ||
| ) |
19 changes: 19 additions & 0 deletions
19
app/src/main/java/com/rocketinsights/android/models/recipe_hilt/RecipeDtoMapper.kt
This file contains hidden or 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,19 @@ | ||
| package com.rocketinsights.android.models.recipe_hilt | ||
|
|
||
| class RecipeDtoMapper { | ||
| fun mapToDomainModel(model: RecipeDto): Recipe { | ||
| return Recipe( | ||
| id = model.pk, | ||
| title = model.title, | ||
| featuredImage = model.featuredImage, | ||
| rating = model.rating, | ||
| publisher = model.publisher, | ||
| sourceUrl = model.sourceUrl, | ||
| ingredients = model.ingredients, | ||
| dateAdded = model.dateAdded, | ||
| dateUpdated = model.dateUpdated, | ||
| ) | ||
| } | ||
|
|
||
| fun toDomainList(initial: List<RecipeDto>): List<Recipe> = initial.map { mapToDomainModel(it) } | ||
| } |
8 changes: 8 additions & 0 deletions
8
app/src/main/java/com/rocketinsights/android/network/hiltexample/RecipeSearchResponse.kt
This file contains hidden or 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.rocketinsights.android.network.hiltexample | ||
|
|
||
| import com.rocketinsights.android.models.recipe_hilt.RecipeDto | ||
|
|
||
| data class RecipeSearchResponse( | ||
| val count: Int, | ||
| val results: List<RecipeDto>, | ||
| ) |
13 changes: 13 additions & 0 deletions
13
app/src/main/java/com/rocketinsights/android/network/hiltexample/RecipeService.kt
This file contains hidden or 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,13 @@ | ||
| package com.rocketinsights.android.network.hiltexample | ||
|
|
||
| import retrofit2.http.GET | ||
| import retrofit2.http.Query | ||
|
|
||
| interface RecipeService { | ||
|
|
||
| @GET("search") | ||
| suspend fun searchRecipe( | ||
| @Query("page") page: Int, | ||
| @Query("query") query: String | ||
| ): RecipeSearchResponse | ||
| } |
7 changes: 7 additions & 0 deletions
7
app/src/main/java/com/rocketinsights/android/repos/RecipeRepository.kt
This file contains hidden or 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,7 @@ | ||
| package com.rocketinsights.android.repos | ||
|
|
||
| import com.rocketinsights.android.models.recipe_hilt.Recipe | ||
|
|
||
| interface RecipeRepository { | ||
| suspend fun search(page: Int, query: String): List<Recipe>? | ||
| } |
14 changes: 14 additions & 0 deletions
14
app/src/main/java/com/rocketinsights/android/repos/RecipeRepositoryImplementation.kt
This file contains hidden or 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,14 @@ | ||
| package com.rocketinsights.android.repos | ||
|
|
||
| import com.rocketinsights.android.models.recipe_hilt.Recipe | ||
| import com.rocketinsights.android.models.recipe_hilt.RecipeDtoMapper | ||
| import com.rocketinsights.android.network.hiltexample.RecipeService | ||
|
|
||
| class RecipeRepositoryImplementation( | ||
| private val recipeService: RecipeService, | ||
| private val mapper: RecipeDtoMapper, | ||
| ) : RecipeRepository { | ||
|
|
||
| override suspend fun search(page: Int, query: String): List<Recipe> = | ||
| mapper.toDomainList(recipeService.searchRecipe(page = page, query = query).results) | ||
| } |
22 changes: 22 additions & 0 deletions
22
app/src/main/java/com/rocketinsights/android/ui/HiltActivity.kt
This file contains hidden or 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,22 @@ | ||
| package com.rocketinsights.android.ui | ||
|
|
||
| import android.os.Bundle | ||
| import androidx.activity.viewModels | ||
| import androidx.appcompat.app.AppCompatActivity | ||
| import com.rocketinsights.android.viewmodels.HiltListViewModel | ||
| import dagger.hilt.android.AndroidEntryPoint | ||
|
|
||
| @AndroidEntryPoint | ||
| class HiltActivity : AppCompatActivity() { | ||
|
|
||
| private val viewModel: HiltListViewModel by viewModels() | ||
|
|
||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
|
|
||
| // TODO: Show data with JetpackCompose in next PR | ||
| viewModel.liveRecipe.observe(this) { recipe -> | ||
| // do nothing for now | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
35 changes: 35 additions & 0 deletions
35
app/src/main/java/com/rocketinsights/android/viewmodels/HiltListViewModel.kt
This file contains hidden or 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.rocketinsights.android.viewmodels | ||
|
|
||
| import androidx.lifecycle.LiveData | ||
| import androidx.lifecycle.MutableLiveData | ||
| import androidx.lifecycle.ViewModel | ||
| import androidx.lifecycle.viewModelScope | ||
| import com.rocketinsights.android.models.recipe_hilt.Recipe | ||
| import com.rocketinsights.android.repos.RecipeRepository | ||
| import dagger.hilt.android.lifecycle.HiltViewModel | ||
| import kotlinx.coroutines.launch | ||
| import javax.inject.Inject | ||
|
|
||
| @HiltViewModel | ||
| class HiltListViewModel | ||
| @Inject | ||
| constructor( | ||
| private val repository: RecipeRepository | ||
| ) : ViewModel() { | ||
|
|
||
| private val recipes: MutableLiveData<List<Recipe>> = MutableLiveData() | ||
| val liveRecipe: LiveData<List<Recipe>> = recipes | ||
|
|
||
| init { | ||
| viewModelScope.launch { | ||
| val result = repository.search( | ||
| page = 1, | ||
| query = "beef carrot potato onion" | ||
| ) | ||
|
|
||
| result?.apply { | ||
| recipes.value = this | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.