├── app ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── mipmap-hdpi │ │ │ │ ├── ic_launcher.webp │ │ │ │ └── ic_launcher_round.webp │ │ │ ├── mipmap-mdpi │ │ │ │ ├── ic_launcher.webp │ │ │ │ └── ic_launcher_round.webp │ │ │ ├── mipmap-xhdpi │ │ │ │ ├── ic_launcher.webp │ │ │ │ └── ic_launcher_round.webp │ │ │ ├── mipmap-xxhdpi │ │ │ │ ├── ic_launcher.webp │ │ │ │ └── ic_launcher_round.webp │ │ │ ├── mipmap-xxxhdpi │ │ │ │ ├── ic_launcher.webp │ │ │ │ └── ic_launcher_round.webp │ │ │ ├── mipmap-anydpi-v26 │ │ │ │ ├── ic_launcher.xml │ │ │ │ └── ic_launcher_round.xml │ │ │ ├── drawable │ │ │ │ ├── ic_circle_green.xml │ │ │ │ ├── ic_circle_grey.xml │ │ │ │ ├── ic_circle_red.xml │ │ │ │ └── ic_launcher_background.xml │ │ │ ├── xml │ │ │ │ ├── backup_rules.xml │ │ │ │ └── data_extraction_rules.xml │ │ │ ├── values │ │ │ │ ├── colors.xml │ │ │ │ ├── strings.xml │ │ │ │ └── themes.xml │ │ │ ├── values-night │ │ │ │ └── themes.xml │ │ │ ├── navigation │ │ │ │ └── nav_graph.xml │ │ │ ├── layout │ │ │ │ ├── fragment_characters.xml │ │ │ │ ├── activity_main.xml │ │ │ │ ├── row_character.xml │ │ │ │ └── fragment_character_detail.xml │ │ │ └── drawable-v24 │ │ │ │ └── ic_launcher_foreground.xml │ │ ├── java │ │ │ └── com │ │ │ │ └── mahmudul │ │ │ │ └── rickandmortyapi │ │ │ │ ├── domain │ │ │ │ ├── models │ │ │ │ │ ├── Characters.kt │ │ │ │ │ ├── Info.kt │ │ │ │ │ ├── Origin.kt │ │ │ │ │ ├── Location.kt │ │ │ │ │ ├── ResultById.kt │ │ │ │ │ └── Result.kt │ │ │ │ ├── repository │ │ │ │ │ └── CharacterRepository.kt │ │ │ │ ├── use_cases │ │ │ │ │ ├── GetCharacterByIdUseCase.kt │ │ │ │ │ └── GetCharactersUseCase.kt │ │ │ │ └── adapters │ │ │ │ │ └── CharacterAdapter.kt │ │ │ │ ├── BaseApp.kt │ │ │ │ ├── util │ │ │ │ ├── Constants.kt │ │ │ │ ├── Resource.kt │ │ │ │ └── AutoClearedValue.kt │ │ │ │ ├── data │ │ │ │ ├── remote │ │ │ │ │ ├── dto │ │ │ │ │ │ ├── LocationDto.kt │ │ │ │ │ │ ├── OriginDto.kt │ │ │ │ │ │ ├── CharactersDto.kt │ │ │ │ │ │ ├── InfoDto.kt │ │ │ │ │ │ └── ResultDto.kt │ │ │ │ │ └── CharacterApi.kt │ │ │ │ └── repository │ │ │ │ │ └── CharacterRepositoryImpl.kt │ │ │ │ ├── presentation │ │ │ │ ├── MainActivity.kt │ │ │ │ ├── viewmodels │ │ │ │ │ ├── CharacterDetailViewModel.kt │ │ │ │ │ └── CharactersViewModel.kt │ │ │ │ └── fragments │ │ │ │ │ ├── CharacterDetailFragment.kt │ │ │ │ │ └── CharactersFragment.kt │ │ │ │ └── di │ │ │ │ └── NetworkModule.kt │ │ └── AndroidManifest.xml │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── mahmudul │ │ │ └── rickandmortyapi │ │ │ └── ExampleUnitTest.kt │ └── androidTest │ │ └── java │ │ └── com │ │ └── mahmudul │ │ └── rickandmortyapi │ │ └── ExampleInstrumentedTest.kt ├── proguard-rules.pro └── build.gradle ├── .idea ├── .gitignore ├── compiler.xml ├── vcs.xml ├── gradle.xml └── misc.xml ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── settings.gradle ├── gradle.properties ├── README.md ├── gradlew.bat └── gradlew /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mhasancse15/RickAndMortyAPI/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mhasancse15/RickAndMortyAPI/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mhasancse15/RickAndMortyAPI/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mhasancse15/RickAndMortyAPI/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mhasancse15/RickAndMortyAPI/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mhasancse15/RickAndMortyAPI/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mhasancse15/RickAndMortyAPI/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mhasancse15/RickAndMortyAPI/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mhasancse15/RickAndMortyAPI/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mhasancse15/RickAndMortyAPI/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mhasancse15/RickAndMortyAPI/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/java/com/mahmudul/rickandmortyapi/domain/models/Characters.kt: -------------------------------------------------------------------------------- 1 | package com.mahmudul.rickandmortyapi.domain.models 2 | 3 | data class Characters( 4 | val info: Info?, 5 | val results: List? 6 | ) -------------------------------------------------------------------------------- /app/src/main/java/com/mahmudul/rickandmortyapi/BaseApp.kt: -------------------------------------------------------------------------------- 1 | package com.mahmudul.rickandmortyapi 2 | 3 | import android.app.Application 4 | import dagger.hilt.android.HiltAndroidApp 5 | 6 | @HiltAndroidApp 7 | class BaseApp: Application(){ 8 | } -------------------------------------------------------------------------------- /app/src/main/java/com/mahmudul/rickandmortyapi/domain/models/Info.kt: -------------------------------------------------------------------------------- 1 | package com.mahmudul.rickandmortyapi.domain.models 2 | 3 | data class Info( 4 | val count: Int?, 5 | val next: String?, 6 | val pages: Int?, 7 | val prev: String? 8 | ) -------------------------------------------------------------------------------- /app/src/main/java/com/mahmudul/rickandmortyapi/util/Constants.kt: -------------------------------------------------------------------------------- 1 | package com.mahmudul.rickandmortyapi.util 2 | 3 | class Constants { 4 | companion object { 5 | const val QUERY_PAGE_SIZE = 20 6 | const val BASE_URL = "https://rickandmortyapi.com/api/" 7 | } 8 | } -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Thu Sep 15 20:58:57 BDT 2022 2 | distributionBase=GRADLE_USER_HOME 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip 4 | distributionPath=wrapper/dists 5 | zipStorePath=wrapper/dists 6 | zipStoreBase=GRADLE_USER_HOME 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/caches 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | /.idea/navEditor.xml 9 | /.idea/assetWizardSettings.xml 10 | .DS_Store 11 | /build 12 | /captures 13 | .externalNativeBuild 14 | .cxx 15 | local.properties 16 | -------------------------------------------------------------------------------- /app/src/main/java/com/mahmudul/rickandmortyapi/domain/models/Origin.kt: -------------------------------------------------------------------------------- 1 | package com.mahmudul.rickandmortyapi.domain.models 2 | 3 | import android.os.Parcelable 4 | import kotlinx.parcelize.Parcelize 5 | 6 | @Parcelize 7 | data class Origin( 8 | val name: String?, 9 | val url: String? 10 | ) : Parcelable -------------------------------------------------------------------------------- /app/src/main/java/com/mahmudul/rickandmortyapi/domain/models/Location.kt: -------------------------------------------------------------------------------- 1 | package com.mahmudul.rickandmortyapi.domain.models 2 | 3 | import android.os.Parcelable 4 | import kotlinx.parcelize.Parcelize 5 | 6 | @Parcelize 7 | data class Location( 8 | val name: String?, 9 | val url: String? 10 | ) : Parcelable -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/java/com/mahmudul/rickandmortyapi/data/remote/dto/LocationDto.kt: -------------------------------------------------------------------------------- 1 | package com.mahmudul.rickandmortyapi.data.remote.dto 2 | 3 | import com.mahmudul.rickandmortyapi.domain.models.Location 4 | 5 | data class LocationDto( 6 | val name: String?, 7 | val url: String? 8 | ) { 9 | fun toLocation(): Location { 10 | return Location( 11 | name = name, 12 | url = url 13 | ) 14 | } 15 | } -------------------------------------------------------------------------------- /app/src/main/java/com/mahmudul/rickandmortyapi/data/remote/dto/OriginDto.kt: -------------------------------------------------------------------------------- 1 | package com.mahmudul.rickandmortyapi.data.remote.dto 2 | 3 | import com.mahmudul.rickandmortyapi.domain.models.Origin 4 | 5 | data class OriginDto( 6 | val name: String?, 7 | val url: String? 8 | ) { 9 | fun toOrigin(): Origin { 10 | return Origin( 11 | name = name, 12 | url = url 13 | ) 14 | } 15 | } 16 | 17 | -------------------------------------------------------------------------------- /app/src/main/java/com/mahmudul/rickandmortyapi/domain/models/ResultById.kt: -------------------------------------------------------------------------------- 1 | package com.mahmudul.rickandmortyapi.domain.models 2 | 3 | data class ResultById( 4 | val id: Int?, 5 | val image: String?, 6 | val name: String?, 7 | val species: String?, 8 | val status: String?, 9 | val gender: String?, 10 | val origin: Origin?, 11 | val location: Location?, 12 | val type: String?, 13 | val episode: List? 14 | ) -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_circle_green.xml: -------------------------------------------------------------------------------- 1 | 7 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_circle_grey.xml: -------------------------------------------------------------------------------- 1 | 7 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_circle_red.xml: -------------------------------------------------------------------------------- 1 | 7 | 10 | -------------------------------------------------------------------------------- /app/src/main/java/com/mahmudul/rickandmortyapi/domain/models/Result.kt: -------------------------------------------------------------------------------- 1 | package com.mahmudul.rickandmortyapi.domain.models 2 | 3 | import android.os.Parcelable 4 | import kotlinx.parcelize.Parcelize 5 | 6 | @Parcelize 7 | data class Result( 8 | val id: Int?, 9 | val name: String?, 10 | val image: String?, 11 | val location: Location?, 12 | val origin: Origin?, 13 | val species: String?, 14 | val status: String?, 15 | ) : Parcelable -------------------------------------------------------------------------------- /app/src/test/java/com/mahmudul/rickandmortyapi/ExampleUnitTest.kt: -------------------------------------------------------------------------------- 1 | package com.mahmudul.rickandmortyapi 2 | 3 | import org.junit.Test 4 | 5 | import org.junit.Assert.* 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * See [testing documentation](http://d.android.com/tools/testing). 11 | */ 12 | class ExampleUnitTest { 13 | @Test 14 | fun addition_isCorrect() { 15 | assertEquals(4, 2 + 2) 16 | } 17 | } -------------------------------------------------------------------------------- /app/src/main/java/com/mahmudul/rickandmortyapi/domain/repository/CharacterRepository.kt: -------------------------------------------------------------------------------- 1 | package com.mahmudul.rickandmortyapi.domain.repository 2 | 3 | import com.mahmudul.rickandmortyapi.data.remote.dto.CharactersDto 4 | import com.mahmudul.rickandmortyapi.data.remote.dto.ResultDto 5 | import io.reactivex.Single 6 | 7 | interface CharacterRepository { 8 | 9 | fun getAllCharacters(pageNumber: Int): Single 10 | 11 | fun getCharacterById(id: Int): Single 12 | } -------------------------------------------------------------------------------- /app/src/main/java/com/mahmudul/rickandmortyapi/data/remote/dto/CharactersDto.kt: -------------------------------------------------------------------------------- 1 | package com.mahmudul.rickandmortyapi.data.remote.dto 2 | 3 | import com.mahmudul.rickandmortyapi.domain.models.Characters 4 | 5 | 6 | data class CharactersDto( 7 | val info: InfoDto?, 8 | val results: List? 9 | ) { 10 | fun toCharacter(): Characters { 11 | return Characters( 12 | info = info?.toInfo(), 13 | results = results?.map { it.toResult() } 14 | ) 15 | } 16 | } -------------------------------------------------------------------------------- /app/src/main/java/com/mahmudul/rickandmortyapi/data/remote/dto/InfoDto.kt: -------------------------------------------------------------------------------- 1 | package com.mahmudul.rickandmortyapi.data.remote.dto 2 | 3 | import com.mahmudul.rickandmortyapi.domain.models.Info 4 | 5 | data class InfoDto( 6 | val count: Int?, 7 | val next: String?, 8 | val pages: Int?, 9 | val prev: String? 10 | ) { 11 | fun toInfo(): Info { 12 | return Info( 13 | count = count, 14 | next = next, 15 | pages = pages, 16 | prev = prev 17 | ) 18 | } 19 | } -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | pluginManagement { 2 | repositories { 3 | gradlePluginPortal() 4 | google() 5 | mavenCentral() 6 | } 7 | } 8 | dependencyResolutionManagement { 9 | repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) 10 | repositories { 11 | google() 12 | mavenCentral() 13 | jcenter() 14 | maven { url 'https://maven.google.com/' } 15 | maven {url "https://jitpack.io"} 16 | } 17 | } 18 | rootProject.name = "RickAndMortyAPI" 19 | include ':app' 20 | -------------------------------------------------------------------------------- /app/src/main/res/xml/backup_rules.xml: -------------------------------------------------------------------------------- 1 | 8 | 9 | 13 | -------------------------------------------------------------------------------- /app/src/main/res/xml/data_extraction_rules.xml: -------------------------------------------------------------------------------- 1 | 6 | 7 | 8 | 12 | 13 | 19 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #FFBB86FC 4 | #FF6200EE 5 | #FF3700B3 6 | #FF03DAC5 7 | #FF018786 8 | #FF000000 9 | #FFFFFFFF 10 | 11 | #807A7A 12 | #D6D6D6 13 | #F80202 14 | #03FB1D 15 | #FAECF1 16 | -------------------------------------------------------------------------------- /app/src/main/java/com/mahmudul/rickandmortyapi/data/remote/CharacterApi.kt: -------------------------------------------------------------------------------- 1 | package com.mahmudul.rickandmortyapi.data.remote 2 | 3 | import com.mahmudul.rickandmortyapi.data.remote.dto.CharactersDto 4 | import com.mahmudul.rickandmortyapi.data.remote.dto.ResultDto 5 | import io.reactivex.Single 6 | import retrofit2.http.GET 7 | import retrofit2.http.Path 8 | import retrofit2.http.Query 9 | 10 | interface CharacterApi { 11 | 12 | @GET("character") 13 | fun getAllCharacters( 14 | @Query("page") pageNumber: Int? 15 | ): Single 16 | 17 | @GET("character/{id}") 18 | fun getCharacterById( 19 | @Path("id") id: Int 20 | ): Single 21 | } -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /app/src/main/java/com/mahmudul/rickandmortyapi/domain/use_cases/GetCharacterByIdUseCase.kt: -------------------------------------------------------------------------------- 1 | package com.mahmudul.rickandmortyapi.domain.use_cases 2 | 3 | import com.mahmudul.rickandmortyapi.domain.models.ResultById 4 | import com.mahmudul.rickandmortyapi.domain.repository.CharacterRepository 5 | import io.reactivex.Single 6 | import io.reactivex.schedulers.Schedulers 7 | import javax.inject.Inject 8 | 9 | class GetCharacterByIdUseCase @Inject constructor( 10 | private val repository: CharacterRepository 11 | ) { 12 | operator fun invoke(id: Int): Single { 13 | return repository.getCharacterById(id = id) 14 | .map { it.toResultById() } 15 | .subscribeOn(Schedulers.io()) 16 | } 17 | } -------------------------------------------------------------------------------- /app/src/main/java/com/mahmudul/rickandmortyapi/domain/use_cases/GetCharactersUseCase.kt: -------------------------------------------------------------------------------- 1 | package com.mahmudul.rickandmortyapi.domain.use_cases 2 | 3 | import com.mahmudul.rickandmortyapi.domain.models.Characters 4 | import com.mahmudul.rickandmortyapi.domain.repository.CharacterRepository 5 | import io.reactivex.Single 6 | import io.reactivex.schedulers.Schedulers 7 | import javax.inject.Inject 8 | 9 | class GetCharactersUseCase @Inject constructor( 10 | private val repository: CharacterRepository 11 | ) { 12 | operator fun invoke(pageNumber: Int): Single { 13 | return repository.getAllCharacters(pageNumber = pageNumber) 14 | .map { it.toCharacter() } 15 | .subscribeOn(Schedulers.io()) 16 | } 17 | } -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile -------------------------------------------------------------------------------- /app/src/androidTest/java/com/mahmudul/rickandmortyapi/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.mahmudul.rickandmortyapi 2 | 3 | import androidx.test.platform.app.InstrumentationRegistry 4 | import androidx.test.ext.junit.runners.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext 22 | assertEquals("com.mahmudul.rickandmortyapi", appContext.packageName) 23 | } 24 | } -------------------------------------------------------------------------------- /app/src/main/res/values-night/themes.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | RickAndMortyAPI 3 | 4 | Origin: 5 | Location: 6 | Type: 7 | Dimension: 8 | Dimension C-137 9 | Episodes: 10 | character_image 11 | color_indicator 12 | Gender: 13 | characters_image 14 | color_indicator_all 15 | Last known location: 16 | First seen in: 17 | 18 | Hello blank fragment 19 | 20 | -------------------------------------------------------------------------------- /app/src/main/java/com/mahmudul/rickandmortyapi/data/repository/CharacterRepositoryImpl.kt: -------------------------------------------------------------------------------- 1 | package com.mahmudul.rickandmortyapi.data.repository 2 | 3 | import com.mahmudul.rickandmortyapi.data.remote.CharacterApi 4 | import com.mahmudul.rickandmortyapi.data.remote.dto.CharactersDto 5 | import com.mahmudul.rickandmortyapi.data.remote.dto.ResultDto 6 | import com.mahmudul.rickandmortyapi.domain.repository.CharacterRepository 7 | import io.reactivex.Single 8 | import io.reactivex.schedulers.Schedulers 9 | import javax.inject.Inject 10 | 11 | class CharacterRepositoryImpl @Inject constructor( 12 | private val characterApi: CharacterApi 13 | ) : CharacterRepository { 14 | 15 | override fun getAllCharacters(pageNumber: Int): Single { 16 | return characterApi.getAllCharacters(pageNumber) 17 | .subscribeOn(Schedulers.io()) 18 | } 19 | 20 | override fun getCharacterById(id: Int): Single { 21 | return characterApi.getCharacterById(id = id) 22 | .subscribeOn(Schedulers.io()) 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /app/src/main/java/com/mahmudul/rickandmortyapi/util/Resource.kt: -------------------------------------------------------------------------------- 1 | package com.mahmudul.rickandmortyapi.util 2 | 3 | import okhttp3.ResponseBody 4 | 5 | data class 6 | Resource( 7 | val status: Status, 8 | val data: T?, 9 | val message: String?, 10 | val isNetworkError: Boolean? = null, 11 | val errorCode: Int? = null, 12 | val errorBody: ResponseBody? = null) { 13 | 14 | enum class Status { 15 | SUCCESS, 16 | ERROR, 17 | LOADING 18 | } 19 | 20 | companion object { 21 | fun success(data: T): Resource { 22 | return Resource(Status.SUCCESS, data, null) 23 | } 24 | 25 | fun error( message: String, isNetworkError: Boolean? = null, errorCode: Int? = null, errorBody: ResponseBody? = null, data: T? = null): Resource { 26 | return Resource(Status.ERROR, data, message, isNetworkError, errorCode, errorBody) 27 | } 28 | 29 | fun loading(data: T? = null): Resource { 30 | return Resource(Status.LOADING, data, null, ) 31 | } 32 | } 33 | 34 | 35 | 36 | } -------------------------------------------------------------------------------- /app/src/main/res/navigation/nav_graph.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 11 | 14 | 15 | 16 | 20 | 21 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 15 | 16 | 17 | 18 | 19 | 20 | 22 | -------------------------------------------------------------------------------- /app/src/main/res/values/themes.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16 | 17 | 23 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 21 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /app/src/main/java/com/mahmudul/rickandmortyapi/presentation/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.mahmudul.rickandmortyapi.presentation 2 | 3 | import androidx.appcompat.app.AppCompatActivity 4 | import android.os.Bundle 5 | import androidx.navigation.NavController 6 | import androidx.navigation.fragment.NavHostFragment 7 | import androidx.navigation.ui.AppBarConfiguration 8 | import androidx.navigation.ui.setupWithNavController 9 | import com.mahmudul.rickandmortyapi.R 10 | import com.mahmudul.rickandmortyapi.databinding.ActivityMainBinding 11 | import dagger.hilt.android.AndroidEntryPoint 12 | 13 | @AndroidEntryPoint 14 | class MainActivity : AppCompatActivity() { 15 | 16 | private lateinit var binding: ActivityMainBinding 17 | 18 | override fun onCreate(savedInstanceState: Bundle?) { 19 | super.onCreate(savedInstanceState) 20 | binding = ActivityMainBinding.inflate(layoutInflater) 21 | setContentView(binding.root) 22 | val navHostFragment: NavHostFragment = 23 | supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment 24 | val navController: NavController = navHostFragment.navController 25 | val appBarConfiguration: AppBarConfiguration = AppBarConfiguration(navController.graph) 26 | binding.toolbar.setupWithNavController(navController, appBarConfiguration) 27 | } 28 | } -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. More details, visit 12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 13 | # org.gradle.parallel=true 14 | # AndroidX package structure to make it clearer which packages are bundled with the 15 | # Android operating system, and which are packaged with your app"s APK 16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn 17 | android.useAndroidX=true 18 | # Kotlin code style for this project: "official" or "obsolete": 19 | kotlin.code.style=official 20 | # Enables namespacing of each library's R class so that its R class includes only the 21 | # resources declared in the library itself and none from the library's dependencies, 22 | # thereby reducing the size of the R class for that library 23 | android.nonTransitiveRClass=true -------------------------------------------------------------------------------- /app/src/main/java/com/mahmudul/rickandmortyapi/data/remote/dto/ResultDto.kt: -------------------------------------------------------------------------------- 1 | package com.mahmudul.rickandmortyapi.data.remote.dto 2 | 3 | import com.mahmudul.rickandmortyapi.domain.models.Result 4 | import com.mahmudul.rickandmortyapi.domain.models.ResultById 5 | 6 | data class ResultDto( 7 | val created: String?, 8 | val episode: List?, 9 | val gender: String?, 10 | val id: Int?, 11 | val image: String?, 12 | val location: LocationDto?, 13 | val name: String?, 14 | val origin: OriginDto?, 15 | val species: String?, 16 | val status: String?, 17 | val type: String?, 18 | val url: String? 19 | ) { 20 | fun toResult(): Result { 21 | return com.mahmudul.rickandmortyapi.domain.models.Result( 22 | id = id, 23 | image = image, 24 | location = location?.toLocation(), 25 | name = name, 26 | species = species, 27 | status = status, 28 | origin = origin?.toOrigin() 29 | ) 30 | } 31 | 32 | fun toResultById(): ResultById { 33 | return ResultById( 34 | id = id, 35 | image = image, 36 | name = name, 37 | species = species, 38 | status = status, 39 | gender = gender, 40 | origin = origin?.toOrigin(), 41 | location = location?.toLocation(), 42 | type = type, 43 | episode = episode, 44 | ) 45 | } 46 | } -------------------------------------------------------------------------------- /app/src/main/java/com/mahmudul/rickandmortyapi/util/AutoClearedValue.kt: -------------------------------------------------------------------------------- 1 | package com.mahmudul.rickandmortyapi.util 2 | 3 | import androidx.fragment.app.Fragment 4 | import androidx.lifecycle.DefaultLifecycleObserver 5 | import androidx.lifecycle.LifecycleOwner 6 | import kotlin.properties.ReadWriteProperty 7 | import kotlin.reflect.KProperty 8 | 9 | class AutoClearedValue(val fragment: Fragment) : ReadWriteProperty { 10 | private var _value: T? = null 11 | 12 | init { 13 | fragment.lifecycle.addObserver(object : DefaultLifecycleObserver { 14 | override fun onCreate(owner: LifecycleOwner) { 15 | fragment.viewLifecycleOwnerLiveData.observe(fragment) { viewLifecycleOwner -> 16 | viewLifecycleOwner?.lifecycle?.addObserver(object : DefaultLifecycleObserver { 17 | override fun onDestroy(owner: LifecycleOwner) { 18 | _value = null 19 | } 20 | }) 21 | } 22 | } 23 | }) 24 | } 25 | 26 | override fun getValue(thisRef: Fragment, property: KProperty<*>): T { 27 | return _value ?: throw IllegalStateException( 28 | "should never call auto-cleared-value get when it might not be available" 29 | ) 30 | } 31 | 32 | override fun setValue(thisRef: Fragment, property: KProperty<*>, value: T) { 33 | _value = value 34 | } 35 | } 36 | 37 | /** 38 | * Creates an [AutoClearedValue] associated with this fragment. 39 | */ 40 | fun Fragment.autoCleared() = AutoClearedValue(this) -------------------------------------------------------------------------------- /app/src/main/res/layout/fragment_characters.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 10 | 14 | 15 | 24 | 25 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /app/src/main/java/com/mahmudul/rickandmortyapi/presentation/viewmodels/CharacterDetailViewModel.kt: -------------------------------------------------------------------------------- 1 | package com.mahmudul.rickandmortyapi.presentation.viewmodels 2 | 3 | import android.util.Log 4 | import androidx.lifecycle.LiveData 5 | import androidx.lifecycle.MutableLiveData 6 | import androidx.lifecycle.ViewModel 7 | import com.mahmudul.rickandmortyapi.domain.models.ResultById 8 | import com.mahmudul.rickandmortyapi.domain.use_cases.GetCharacterByIdUseCase 9 | import dagger.hilt.android.lifecycle.HiltViewModel 10 | import io.reactivex.android.schedulers.AndroidSchedulers 11 | import io.reactivex.disposables.CompositeDisposable 12 | import javax.inject.Inject 13 | 14 | 15 | @HiltViewModel 16 | class CharacterDetailViewModel @Inject constructor( 17 | private val getCharacterByIdUseCase: GetCharacterByIdUseCase 18 | ) : ViewModel() { 19 | 20 | private var compositeDisposable = CompositeDisposable() 21 | private val _newCharacterDetail = MutableLiveData() 22 | val newCharacterDetail: LiveData = _newCharacterDetail 23 | 24 | fun getCharacterById(id: Int?) { 25 | val disposable = id?.let { characterId -> 26 | getCharacterByIdUseCase(characterId) 27 | .observeOn(AndroidSchedulers.mainThread()) 28 | .subscribe({ character -> 29 | _newCharacterDetail.value = character 30 | }, { 31 | Log.e("TAG", "Не удалось получить персонажа") 32 | }) 33 | } 34 | disposable.let { 35 | if (it != null) { 36 | compositeDisposable.add(it) 37 | } 38 | } 39 | } 40 | 41 | override fun onCleared() { 42 | super.onCleared() 43 | compositeDisposable.clear() 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 15 | 18 | 21 | 22 | 23 | 24 | 30 | -------------------------------------------------------------------------------- /app/src/main/java/com/mahmudul/rickandmortyapi/presentation/viewmodels/CharactersViewModel.kt: -------------------------------------------------------------------------------- 1 | package com.mahmudul.rickandmortyapi.presentation.viewmodels 2 | 3 | import androidx.lifecycle.LiveData 4 | import androidx.lifecycle.MutableLiveData 5 | import androidx.lifecycle.ViewModel 6 | import com.mahmudul.rickandmortyapi.domain.models.Characters 7 | import com.mahmudul.rickandmortyapi.domain.use_cases.GetCharactersUseCase 8 | import dagger.hilt.android.lifecycle.HiltViewModel 9 | import io.reactivex.android.schedulers.AndroidSchedulers 10 | import io.reactivex.disposables.CompositeDisposable 11 | import javax.inject.Inject 12 | 13 | @HiltViewModel 14 | class CharactersViewModel @Inject constructor( 15 | private val getCharactersUseCase: GetCharactersUseCase 16 | ) : ViewModel() { 17 | var compositeDisposable = CompositeDisposable() 18 | var charactersPage = 1 19 | 20 | private val _newCharacters = MutableLiveData() 21 | val newCharacters: LiveData = _newCharacters 22 | 23 | init { 24 | getAllCharacters(charactersPage) 25 | } 26 | 27 | private fun getAllCharacters(page: Int) { 28 | val disposable = getCharactersUseCase(page) 29 | .observeOn(AndroidSchedulers.mainThread()) 30 | .subscribe({ characters -> 31 | val old = _newCharacters.value 32 | val new = old?.results.orEmpty() + characters?.results.orEmpty() 33 | _newCharacters.value = Characters(null, new) 34 | }, { 35 | 36 | }) 37 | 38 | compositeDisposable.add(disposable) 39 | } 40 | 41 | fun nextPage() { 42 | charactersPage += 1 43 | getAllCharacters(charactersPage) 44 | } 45 | 46 | override fun onCleared() { 47 | super.onCleared() 48 | compositeDisposable.clear() 49 | } 50 | 51 | } -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 17 | 18 | 25 | 26 | 27 | 38 | 39 | -------------------------------------------------------------------------------- /app/src/main/java/com/mahmudul/rickandmortyapi/di/NetworkModule.kt: -------------------------------------------------------------------------------- 1 | package com.mahmudul.rickandmortyapi.di 2 | 3 | import com.mahmudul.rickandmortyapi.data.remote.CharacterApi 4 | import com.mahmudul.rickandmortyapi.data.repository.CharacterRepositoryImpl 5 | import com.mahmudul.rickandmortyapi.domain.repository.CharacterRepository 6 | import com.mahmudul.rickandmortyapi.util.Constants 7 | import dagger.Module 8 | import dagger.Provides 9 | import dagger.hilt.InstallIn 10 | import dagger.hilt.components.SingletonComponent 11 | import okhttp3.OkHttpClient 12 | import okhttp3.logging.HttpLoggingInterceptor 13 | import retrofit2.Retrofit 14 | import retrofit2.converter.gson.GsonConverterFactory 15 | import java.util.concurrent.TimeUnit 16 | import javax.inject.Singleton 17 | import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory 18 | 19 | @Module 20 | @InstallIn(SingletonComponent::class) 21 | class NetworkModule { 22 | 23 | @Provides 24 | @Singleton 25 | fun provideRxJava2CallAdapterFactory(): RxJava2CallAdapterFactory { 26 | return RxJava2CallAdapterFactory.create() 27 | } 28 | 29 | @Provides 30 | @Singleton 31 | fun provideLoggingInterceptor(): HttpLoggingInterceptor { 32 | return HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY) 33 | } 34 | 35 | @Provides 36 | @Singleton 37 | fun provideOkHttpClient(logging: HttpLoggingInterceptor): OkHttpClient { 38 | return OkHttpClient.Builder() 39 | .addInterceptor(logging) 40 | .connectTimeout(15, TimeUnit.SECONDS) 41 | .readTimeout(15, TimeUnit.SECONDS) 42 | .build() 43 | } 44 | 45 | @Provides 46 | @Singleton 47 | fun provideRetrofit(client: OkHttpClient, rxJava2CallAdapterFactory: RxJava2CallAdapterFactory): Retrofit { 48 | return Retrofit.Builder() 49 | .baseUrl(Constants.BASE_URL) 50 | .addConverterFactory(GsonConverterFactory.create()) 51 | .addCallAdapterFactory(rxJava2CallAdapterFactory) 52 | .client(client) 53 | .build() 54 | } 55 | 56 | @Singleton 57 | @Provides 58 | fun provideCharacterApi(retrofit: Retrofit): CharacterApi { 59 | return retrofit.create(CharacterApi::class.java) 60 | } 61 | 62 | @Provides 63 | @Singleton 64 | fun provideCharacterRepository(api: CharacterApi): CharacterRepository { 65 | return CharacterRepositoryImpl(api) 66 | } 67 | 68 | 69 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RickAndMortyAPI - Android Clean Architecture Sample 2 | 3 | A Rick And Morty simple app that loads information from [The Rick and Morty API](https://rickandmortyapi.com/) to show one approach to using some of the best practices in Android Development. 4 | 5 | ## Built With 🛠 6 | - [Kotlin](https://kotlinlang.org/) - First class and official programming language for Android development. 7 | - [RxJava](https://github.com/ReactiveX/RxJava) - For asynchronous and more.. 8 | - [Hilt](https://developer.android.com/training/dependency-injection/hilt-android) - Dependency injection library for Android that reduces the boilerplate of doing manual dependency injection in your project 9 | - [Android Architecture Components](https://developer.android.com/topic/libraries/architecture) - Collection of libraries that help you design robust, testable, and maintainable apps. 10 | - [LiveData](https://developer.android.com/topic/libraries/architecture/livedata) - Data objects that notify views when the underlying database changes. 11 | - [ViewModel](https://developer.android.com/topic/libraries/architecture/viewmodel) - Stores UI-related data that isn't destroyed on UI changes. 12 | - [ViewDataBinding](https://developer.android.com/topic/libraries/view-binding) - Generates a binding class for each XML layout file present in that module and allows you to more easily write code that interacts with views. 13 | - [Retrofit](https://square.github.io/retrofit/) - A type-safe HTTP client for Android and Java. 14 | - [Coil-kt](https://coil-kt.github.io/coil/) - An image loading library for Android backed by Kotlin Coroutines. 15 | - [Material Components for Android](https://github.com/material-components/material-components-android) - Modular and customizable Material Design UI components for Android. 16 | 17 | 18 | ## 🚀 Getting Started 19 | These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. 20 | 21 | 22 | ### Prerequisites 23 | * Android Studio 3.2+ 24 | * Java JDK 25 | 26 | ### Installing 27 | Follow these steps if you want to get a local copy of the project on your machine. 28 | 29 | #### 1. Clone or fork the repository by running the command below 30 | ``` 31 | git https://github.com/mhasancse15/RickAndMortyAPI.git 32 | ``` 33 | 34 | #### 2. Import the project in AndroidStudio, and add API Key 35 | 1. In Android Studio, go to File -> New -> Import project. 36 | 2. Follow the dialog wizard to choose the folder where you cloned the project and click on open. 37 | 3. Android Studio imports the projects and builds it for you. 38 | 39 | ## You should also take a look at 40 | * [Guide to app architecture](https://developer.android.com/jetpack/guide) 41 | * [Android architecture samples](https://github.com/android/architecture-samples) 42 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto execute 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto execute 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :execute 68 | @rem Setup the command line 69 | 70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 71 | 72 | 73 | @rem Execute Gradle 74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 75 | 76 | :end 77 | @rem End local scope for the variables with windows NT shell 78 | if "%ERRORLEVEL%"=="0" goto mainEnd 79 | 80 | :fail 81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 82 | rem the _cmd.exe /c_ return code! 83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 84 | exit /b 1 85 | 86 | :mainEnd 87 | if "%OS%"=="Windows_NT" endlocal 88 | 89 | :omega 90 | -------------------------------------------------------------------------------- /app/src/main/java/com/mahmudul/rickandmortyapi/presentation/fragments/CharacterDetailFragment.kt: -------------------------------------------------------------------------------- 1 | package com.mahmudul.rickandmortyapi.presentation.fragments 2 | 3 | import android.annotation.SuppressLint 4 | import android.os.Bundle 5 | import androidx.fragment.app.Fragment 6 | import android.view.LayoutInflater 7 | import android.view.View 8 | import android.view.ViewGroup 9 | import androidx.fragment.app.viewModels 10 | import androidx.navigation.fragment.navArgs 11 | import coil.load 12 | import coil.transform.CircleCropTransformation 13 | import com.mahmudul.rickandmortyapi.R 14 | import com.mahmudul.rickandmortyapi.databinding.FragmentCharacterDetailBinding 15 | import com.mahmudul.rickandmortyapi.presentation.viewmodels.CharacterDetailViewModel 16 | import com.mahmudul.rickandmortyapi.util.autoCleared 17 | import dagger.hilt.android.AndroidEntryPoint 18 | 19 | @AndroidEntryPoint 20 | class CharacterDetailFragment : Fragment(R.layout.fragment_character_detail) { 21 | 22 | private var binding: FragmentCharacterDetailBinding by autoCleared() 23 | 24 | private val args: CharacterDetailFragmentArgs by navArgs() 25 | private val characterDetailViewModel: CharacterDetailViewModel by viewModels() 26 | 27 | override fun onViewCreated(view: View, savedInstanceState: Bundle?) { 28 | super.onViewCreated(view, savedInstanceState) 29 | 30 | binding = FragmentCharacterDetailBinding.bind(view) 31 | 32 | val characterId = args.character?.id 33 | characterDetailViewModel.getCharacterById(characterId) 34 | showProgressBar() 35 | fetchingData() 36 | hideProgressBar() 37 | } 38 | 39 | @SuppressLint("SetTextI18n") 40 | private fun fetchingData() { 41 | activity?.let { 42 | characterDetailViewModel.newCharacterDetail 43 | .observe(viewLifecycleOwner) { character -> 44 | binding.nameDetail.text = character.name 45 | binding.genderDetail.text = character.gender.toString() 46 | binding.dimensionDetail.text = binding.dimensionDetail.text 47 | binding.originDetail.text = character.origin?.name 48 | binding.locationDetail.text = character.location?.name 49 | binding.typeDetail.text = character.type 50 | binding.episodesDetail.text = character.episode?.size.toString() 51 | binding.let { 52 | binding.characterImageDetail.load(character.image) { 53 | crossfade(true) 54 | transformations(CircleCropTransformation()) 55 | } 56 | 57 | } 58 | binding.characterSpeciesAndStatusDetail.text = 59 | "${character.status} - ${character.species}" 60 | if (character.status?.contains("Dead") == true) { 61 | binding.colorIndicatorDetail.setImageResource(R.drawable.ic_circle_red) 62 | } else if (character.status?.contains("Alive") == true) { 63 | binding.colorIndicatorDetail.setImageResource(R.drawable.ic_circle_green) 64 | } else binding.colorIndicatorDetail.setImageResource(R.drawable.ic_circle_grey) 65 | } 66 | } 67 | } 68 | 69 | private fun hideProgressBar() { 70 | binding.progressBarDetail.visibility = View.INVISIBLE 71 | } 72 | 73 | private fun showProgressBar() { 74 | binding.progressBarDetail.visibility = View.VISIBLE 75 | } 76 | 77 | } -------------------------------------------------------------------------------- /app/src/main/java/com/mahmudul/rickandmortyapi/domain/adapters/CharacterAdapter.kt: -------------------------------------------------------------------------------- 1 | package com.mahmudul.rickandmortyapi.domain.adapters 2 | 3 | import android.annotation.SuppressLint 4 | import android.util.Log 5 | import android.view.LayoutInflater 6 | import android.view.ViewGroup 7 | import androidx.recyclerview.widget.AsyncListDiffer 8 | import androidx.recyclerview.widget.DiffUtil 9 | import androidx.recyclerview.widget.RecyclerView 10 | import coil.load 11 | import coil.transform.CircleCropTransformation 12 | import com.mahmudul.rickandmortyapi.R 13 | import com.mahmudul.rickandmortyapi.domain.models.Result 14 | import com.mahmudul.rickandmortyapi.databinding.RowCharacterBinding 15 | 16 | class CharacterAdapter : RecyclerView.Adapter() { 17 | 18 | private var binding: RowCharacterBinding? = null 19 | 20 | inner class CharacterViewHolder(itemBinding: RowCharacterBinding) : 21 | RecyclerView.ViewHolder(itemBinding.root) 22 | 23 | private val differCallback = object : DiffUtil.ItemCallback() { 24 | override fun areItemsTheSame(oldItem: Result, newItem: Result): Boolean { 25 | return oldItem.id == newItem.id 26 | } 27 | 28 | override fun areContentsTheSame(oldItem: Result, newItem: Result): Boolean { 29 | return oldItem == newItem 30 | } 31 | } 32 | 33 | val differ = AsyncListDiffer(this, differCallback) 34 | 35 | override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CharacterViewHolder { 36 | binding = RowCharacterBinding.inflate( 37 | LayoutInflater.from(parent.context), 38 | parent, 39 | false 40 | ) 41 | return CharacterViewHolder(requireNotNull(binding)) 42 | } 43 | 44 | @SuppressLint("SetTextI18n") 45 | override fun onBindViewHolder(holder: CharacterViewHolder, position: Int) { 46 | val character = differ.currentList[position] 47 | holder.itemView.apply { 48 | binding?.characterName?.text = character.name 49 | binding?.lastKnownLocation?.text = character.location?.name 50 | binding?.firstSeenIn?.text = character.origin?.name 51 | binding?.let { 52 | binding?.characterImage?.let { characterImage -> 53 | characterImage.load(character.image) { 54 | crossfade(true) 55 | transformations(CircleCropTransformation()) 56 | } 57 | 58 | } 59 | } 60 | binding?.characterSpeciesAndStatus?.text = 61 | "${character.status} - ${character.species}" 62 | 63 | if (character.status?.contains("Dead") == true) { 64 | binding?.colorIndicator?.setImageResource(R.drawable.ic_circle_red) 65 | } else if (character.status?.contains("Alive") == true) { 66 | binding?.colorIndicator?.setImageResource(R.drawable.ic_circle_green) 67 | } else binding?.colorIndicator?.setImageResource(R.drawable.ic_circle_grey) 68 | 69 | setOnClickListener { 70 | onItemClickListener?.let { it(character) } 71 | Log.d("TAG", "${character.id}") 72 | } 73 | } 74 | } 75 | 76 | override fun getItemCount(): Int { 77 | return differ.currentList.size 78 | } 79 | 80 | override fun getItemViewType(position: Int): Int { 81 | return position 82 | } 83 | 84 | private var onItemClickListener: ((Result) -> Unit)? = null 85 | 86 | fun setOnItemClickListener(listener: (Result) -> Unit) { 87 | onItemClickListener = listener 88 | 89 | } 90 | 91 | 92 | } -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'com.android.application' 3 | id 'org.jetbrains.kotlin.android' 4 | id 'kotlin-kapt' 5 | id 'kotlin-parcelize' 6 | id "androidx.navigation.safeargs.kotlin" 7 | id 'dagger.hilt.android.plugin' 8 | } 9 | 10 | android { 11 | compileSdk 32 12 | 13 | defaultConfig { 14 | applicationId "com.mahmudul.rickandmortyapi" 15 | minSdk 23 16 | targetSdk 32 17 | versionCode 1 18 | versionName "1.0" 19 | 20 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 21 | } 22 | 23 | buildTypes { 24 | release { 25 | minifyEnabled false 26 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 27 | } 28 | } 29 | compileOptions { 30 | sourceCompatibility JavaVersion.VERSION_1_8 31 | targetCompatibility JavaVersion.VERSION_1_8 32 | } 33 | kotlinOptions { 34 | jvmTarget = '1.8' 35 | } 36 | buildFeatures { 37 | viewBinding true 38 | dataBinding true 39 | } 40 | } 41 | 42 | dependencies { 43 | 44 | implementation 'androidx.core:core-ktx:1.7.0' 45 | implementation 'androidx.appcompat:appcompat:1.5.0' 46 | implementation 'com.google.android.material:material:1.6.1' 47 | implementation 'androidx.constraintlayout:constraintlayout:2.1.4' 48 | testImplementation 'junit:junit:4.13.2' 49 | androidTestImplementation 'androidx.test.ext:junit:1.1.3' 50 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' 51 | 52 | // Navigation Components 53 | def nav_version = "2.5.2" 54 | implementation("androidx.navigation:navigation-fragment-ktx:$nav_version") 55 | implementation("androidx.navigation:navigation-ui-ktx:$nav_version") 56 | 57 | //RxJava2 58 | implementation "io.reactivex.rxjava2:rxjava:2.2.19" 59 | implementation 'io.reactivex.rxjava2:rxandroid:2.1.1' 60 | 61 | //retrofit 62 | def retrofit_version = "2.9.0" 63 | implementation "com.google.code.gson:gson:$retrofit_version" 64 | implementation "com.squareup.retrofit2:retrofit:$retrofit_version" 65 | implementation "com.squareup.retrofit2:converter-gson:$retrofit_version" 66 | implementation "com.squareup.okhttp3:okhttp:4.10.0" 67 | implementation 'com.squareup.okhttp3:logging-interceptor:4.9.1' 68 | implementation 'androidx.legacy:legacy-support-v4:1.0.0' 69 | implementation 'androidx.preference:preference-ktx:1.2.0' 70 | implementation "com.squareup.retrofit2:converter-scalars:$retrofit_version" 71 | implementation "com.squareup.retrofit2:adapter-rxjava2:$retrofit_version" 72 | 73 | //lifecycle,view model & livedata 74 | def lifecycle_version = "2.6.0-alpha01" 75 | implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version" 76 | implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version" 77 | implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version" 78 | implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version" 79 | 80 | //DI with Hilt 81 | def hilt_version = "2.42" 82 | implementation "com.google.dagger:hilt-android:$hilt_version" 83 | kapt "com.google.dagger:hilt-compiler:$hilt_version" 84 | 85 | //room 86 | def room_version = "2.4.2" 87 | implementation "androidx.room:room-runtime:$room_version" 88 | kapt "androidx.room:room-compiler:$room_version" 89 | implementation "androidx.room:room-ktx:$room_version" 90 | 91 | //Timber 92 | implementation 'com.jakewharton.timber:timber:4.7.1' 93 | 94 | //Coil 95 | implementation "io.coil-kt:coil:2.1.0" 96 | 97 | 98 | } -------------------------------------------------------------------------------- /app/src/main/java/com/mahmudul/rickandmortyapi/presentation/fragments/CharactersFragment.kt: -------------------------------------------------------------------------------- 1 | package com.mahmudul.rickandmortyapi.presentation.fragments 2 | 3 | import android.os.Bundle 4 | import androidx.fragment.app.Fragment 5 | import android.view.View 6 | import android.widget.AbsListView 7 | import androidx.fragment.app.viewModels 8 | import androidx.navigation.fragment.findNavController 9 | import androidx.recyclerview.widget.LinearLayoutManager 10 | import androidx.recyclerview.widget.RecyclerView 11 | import com.mahmudul.rickandmortyapi.R 12 | import com.mahmudul.rickandmortyapi.databinding.FragmentCharactersBinding 13 | import com.mahmudul.rickandmortyapi.domain.adapters.CharacterAdapter 14 | import com.mahmudul.rickandmortyapi.presentation.viewmodels.CharactersViewModel 15 | import com.mahmudul.rickandmortyapi.util.Constants.Companion.QUERY_PAGE_SIZE 16 | import com.mahmudul.rickandmortyapi.util.autoCleared 17 | import dagger.hilt.android.AndroidEntryPoint 18 | 19 | @AndroidEntryPoint 20 | class CharactersFragment : Fragment(R.layout.fragment_characters) { 21 | 22 | private var binding: FragmentCharactersBinding by autoCleared() 23 | private val charactersViewModel: CharactersViewModel by viewModels() 24 | private var charactersAdapter: CharacterAdapter? = null 25 | 26 | var isLoading = false 27 | var isLastPage = false 28 | var isScrolling = false 29 | 30 | 31 | override fun onViewCreated(view: View, savedInstanceState: Bundle?) { 32 | super.onViewCreated(view, savedInstanceState) 33 | 34 | binding = FragmentCharactersBinding.bind(view) 35 | 36 | showProgressBar() 37 | setUpRecyclerView() 38 | 39 | charactersAdapter?.setOnItemClickListener { 40 | val bundle = Bundle().apply { 41 | putParcelable("character", it) 42 | } 43 | findNavController().navigate(R.id.action_charactersFragment_to_characterDetailFragment, bundle) 44 | 45 | } 46 | fetchingData() 47 | } 48 | 49 | private fun fetchingData() { 50 | activity?.let { 51 | charactersViewModel.newCharacters 52 | .observe(viewLifecycleOwner) { characters -> 53 | charactersAdapter?.differ?.submitList(characters.results) 54 | hideProgressBar() 55 | } 56 | } 57 | } 58 | 59 | 60 | private fun setUpRecyclerView() { 61 | charactersAdapter = CharacterAdapter() 62 | binding.charactersRv.apply { 63 | setHasFixedSize(true) 64 | adapter = charactersAdapter 65 | layoutManager = LinearLayoutManager(requireContext()) 66 | addOnScrollListener(this@CharactersFragment.scrollListener) 67 | } 68 | } 69 | 70 | private val scrollListener = object : RecyclerView.OnScrollListener() { 71 | override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) { 72 | super.onScrollStateChanged(recyclerView, newState) 73 | if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) { 74 | isScrolling = true 75 | } 76 | } 77 | 78 | override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { 79 | super.onScrolled(recyclerView, dx, dy) 80 | val layoutManager = recyclerView.layoutManager as LinearLayoutManager 81 | val firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition() 82 | val visibleItemCount = layoutManager.childCount 83 | val totalItemCount = layoutManager.itemCount 84 | val isNotLoadingAndNotLastPage = !isLoading && !isLastPage 85 | val isAtLastItem = firstVisibleItemPosition + visibleItemCount >= totalItemCount 86 | val isNotAtBeginning = firstVisibleItemPosition >= 0 87 | val isTotalMoreThanVisible = totalItemCount >= QUERY_PAGE_SIZE 88 | val shouldPaginate = isNotLoadingAndNotLastPage && isAtLastItem && isNotAtBeginning 89 | && isTotalMoreThanVisible && isScrolling 90 | if (shouldPaginate) { 91 | charactersViewModel.nextPage() 92 | isScrolling = false 93 | } 94 | } 95 | } 96 | 97 | private fun hideProgressBar() { 98 | binding.progressBar.visibility = View.INVISIBLE 99 | } 100 | 101 | private fun showProgressBar() { 102 | binding.progressBar.visibility = View.VISIBLE 103 | } 104 | 105 | 106 | } -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/layout/row_character.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 14 | 15 | 19 | 20 | 29 | 30 | 46 | 47 | 57 | 58 | 59 | 69 | 70 | 81 | 82 | 96 | 97 | 108 | 109 | 122 | 123 | 124 | 125 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | # 4 | # Copyright 2015 the original author or authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | ## 21 | ## Gradle start up script for UN*X 22 | ## 23 | ############################################################################## 24 | 25 | # Attempt to set APP_HOME 26 | # Resolve links: $0 may be a link 27 | PRG="$0" 28 | # Need this for relative symlinks. 29 | while [ -h "$PRG" ] ; do 30 | ls=`ls -ld "$PRG"` 31 | link=`expr "$ls" : '.*-> \(.*\)$'` 32 | if expr "$link" : '/.*' > /dev/null; then 33 | PRG="$link" 34 | else 35 | PRG=`dirname "$PRG"`"/$link" 36 | fi 37 | done 38 | SAVED="`pwd`" 39 | cd "`dirname \"$PRG\"`/" >/dev/null 40 | APP_HOME="`pwd -P`" 41 | cd "$SAVED" >/dev/null 42 | 43 | APP_NAME="Gradle" 44 | APP_BASE_NAME=`basename "$0"` 45 | 46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 48 | 49 | # Use the maximum available, or set MAX_FD != -1 to use that value. 50 | MAX_FD="maximum" 51 | 52 | warn () { 53 | echo "$*" 54 | } 55 | 56 | die () { 57 | echo 58 | echo "$*" 59 | echo 60 | exit 1 61 | } 62 | 63 | # OS specific support (must be 'true' or 'false'). 64 | cygwin=false 65 | msys=false 66 | darwin=false 67 | nonstop=false 68 | case "`uname`" in 69 | CYGWIN* ) 70 | cygwin=true 71 | ;; 72 | Darwin* ) 73 | darwin=true 74 | ;; 75 | MINGW* ) 76 | msys=true 77 | ;; 78 | NONSTOP* ) 79 | nonstop=true 80 | ;; 81 | esac 82 | 83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 84 | 85 | 86 | # Determine the Java command to use to start the JVM. 87 | if [ -n "$JAVA_HOME" ] ; then 88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 89 | # IBM's JDK on AIX uses strange locations for the executables 90 | JAVACMD="$JAVA_HOME/jre/sh/java" 91 | else 92 | JAVACMD="$JAVA_HOME/bin/java" 93 | fi 94 | if [ ! -x "$JAVACMD" ] ; then 95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 96 | 97 | Please set the JAVA_HOME variable in your environment to match the 98 | location of your Java installation." 99 | fi 100 | else 101 | JAVACMD="java" 102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 103 | 104 | Please set the JAVA_HOME variable in your environment to match the 105 | location of your Java installation." 106 | fi 107 | 108 | # Increase the maximum file descriptors if we can. 109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 110 | MAX_FD_LIMIT=`ulimit -H -n` 111 | if [ $? -eq 0 ] ; then 112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 113 | MAX_FD="$MAX_FD_LIMIT" 114 | fi 115 | ulimit -n $MAX_FD 116 | if [ $? -ne 0 ] ; then 117 | warn "Could not set maximum file descriptor limit: $MAX_FD" 118 | fi 119 | else 120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 121 | fi 122 | fi 123 | 124 | # For Darwin, add options to specify how the application appears in the dock 125 | if $darwin; then 126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 127 | fi 128 | 129 | # For Cygwin or MSYS, switch paths to Windows format before running java 130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then 131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 133 | 134 | JAVACMD=`cygpath --unix "$JAVACMD"` 135 | 136 | # We build the pattern for arguments to be converted via cygpath 137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 138 | SEP="" 139 | for dir in $ROOTDIRSRAW ; do 140 | ROOTDIRS="$ROOTDIRS$SEP$dir" 141 | SEP="|" 142 | done 143 | OURCYGPATTERN="(^($ROOTDIRS))" 144 | # Add a user-defined pattern to the cygpath arguments 145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 147 | fi 148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 149 | i=0 150 | for arg in "$@" ; do 151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 153 | 154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 156 | else 157 | eval `echo args$i`="\"$arg\"" 158 | fi 159 | i=`expr $i + 1` 160 | done 161 | case $i in 162 | 0) set -- ;; 163 | 1) set -- "$args0" ;; 164 | 2) set -- "$args0" "$args1" ;; 165 | 3) set -- "$args0" "$args1" "$args2" ;; 166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;; 167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 172 | esac 173 | fi 174 | 175 | # Escape application args 176 | save () { 177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 178 | echo " " 179 | } 180 | APP_ARGS=`save "$@"` 181 | 182 | # Collect all arguments for the java command, following the shell quoting and substitution rules 183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 184 | 185 | exec "$JAVACMD" "$@" 186 | -------------------------------------------------------------------------------- /app/src/main/res/layout/fragment_character_detail.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 12 | 16 | 17 | 28 | 29 | 30 | 39 | 40 | 55 | 56 | 66 | 67 | 81 | 82 | 94 | 95 | 107 | 108 | 119 | 120 | 132 | 133 | 144 | 145 | 157 | 158 | 169 | 170 | 182 | 183 | 194 | 195 | 207 | 208 | 219 | 220 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | --------------------------------------------------------------------------------