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
@HiltAndroidApp | |
class MainApplication : Application() |
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
{ | |
"info": { | |
"count": 591, | |
"pages": 20, | |
"next": "https://rickandmortyapi.com/api/character/?page=2", | |
"prev": null | |
}, | |
"results": [ | |
// ... | |
] |
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
@AndroidEntryPoint | |
class MainActivity : AppCompatActivity() { | |
//... | |
} |
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
@AndroidEntryPoint | |
class CharacterDetailFragment : Fragment() { | |
//... | |
} |
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
@Module | |
@InstallIn(ApplicationComponent::class) | |
object AppModule { | |
@Singleton | |
@Provides | |
fun provideRetrofit(gson: Gson) : Retrofit = Retrofit.Builder() | |
.baseUrl("https://rickandmortyapi.com/api/") | |
.addConverterFactory(GsonConverterFactory.create(gson)) | |
.build() |
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
class CharacterRepository @Inject constructor( | |
private val remoteDataSource: CharacterRemoteDataSource, | |
private val localDataSource: CharacterDao | |
) { | |
//... | |
} |
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
class CharactersViewModel @ViewModelInject constructor( | |
private val repository: CharacterRepository | |
) : ViewModel() { | |
val characters: LiveData<Resource<List<Character>>> = repository.getCharacters() | |
} |
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
data class Resource<out T>(val status: Status, val data: T?, val message: String?) { | |
enum class Status { | |
SUCCESS, | |
ERROR, | |
LOADING | |
} | |
companion object { | |
fun <T> success(data: T): Resource<T> { |
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
viewModel.characters.observe(viewLifecycleOwner, Observer { | |
when (it.status) { | |
Resource.Status.SUCCESS -> { | |
progress_bar.visibility = View.GONE | |
if (!it.data.isNullOrEmpty()) adapter.setItems(ArrayList(it.data)) | |
} | |
Resource.Status.ERROR -> | |
Toast.makeText(activity, it.message, Toast.LENGTH_SHORT).show() | |
Resource.Status.LOADING -> |
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
interface CharacterService { | |
@GET("character") | |
suspend fun getAllCharacters() : Response<CharacterList> | |
@GET("character/{id}") | |
suspend fun getCharacter(@Path("id") id: Int): Response<Character> | |
} |
OlderNewer