Skip to content
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

Follow Recommended Practices when Using Data classes along with Compose #26

Merged
merged 2 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ plugins {
// Hilt doesn't supports KSP yet
id 'kotlin-kapt'
id 'com.google.dagger.hilt.android'
id("com.google.devtools.ksp")
}

android {
Expand Down Expand Up @@ -43,7 +44,7 @@ android {
buildConfig true
}
composeOptions {
kotlinCompilerExtensionVersion '1.4.6'
kotlinCompilerExtensionVersion '1.5.0'
}
packagingOptions {
resources {
Expand Down Expand Up @@ -101,7 +102,7 @@ dependencies {
implementation "com.squareup.okhttp3:okhttp:$okhttp_version"

// Hilt
def hilt_version = "2.44"
def hilt_version = "2.47"
def hilt_navigation_compose = "1.0.0"
implementation "com.google.dagger:hilt-android:$hilt_version"
kapt "com.google.dagger:hilt-compiler:$hilt_version"
Expand All @@ -121,7 +122,7 @@ dependencies {

// Room
implementation "androidx.room:room-runtime:2.5.1"
kapt "androidx.room:room-compiler:2.5.1"
ksp "androidx.room:room-compiler:2.5.1"
implementation "androidx.room:room-ktx:2.5.1"

// WorkManager
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.raghav.spacedawnv2.launchesscreen

import androidx.compose.runtime.Stable
import com.raghav.spacedawnv2.domain.model.LaunchDetail
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.persistentListOf

@Stable
data class LaunchesScreenState(
val launches: List<LaunchDetail> = emptyList(),
val launches: ImmutableList<LaunchDetail> = persistentListOf(),
val isLoading: Boolean = false,
val errorMessage: String? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import com.raghav.spacedawnv2.domain.util.Constants
import com.raghav.spacedawnv2.domain.util.Resource
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject
import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.toImmutableList
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableSharedFlow
Expand Down Expand Up @@ -49,15 +51,15 @@ class LaunchesScreenVM @Inject constructor(
it.copy(
isLoading = false,
errorMessage = result.errorMessage,
launches = result.data ?: emptyList()
launches = result.data?.toImmutableList() ?: persistentListOf()
)
}
}

is Resource.Success -> {
_uiState.update {
it.copy(
launches = result.data ?: emptyList(),
launches = result.data?.toImmutableList() ?: persistentListOf(),
isLoading = false,
errorMessage = null
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.raghav.spacedawnv2.remindersscreen

import androidx.compose.runtime.Stable
import com.raghav.spacedawnv2.domain.model.Reminder
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.persistentListOf

@Stable
data class RemindersScreenState(
val reminders: List<Reminder> = emptyList(),
val reminders: ImmutableList<Reminder> = persistentListOf(),
val isLoading: Boolean = false,
val infoMessage: String? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.raghav.spacedawnv2.domain.usecase.GetRemindersUseCase
import com.raghav.spacedawnv2.domain.util.Resource
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject
import kotlinx.collections.immutable.toImmutableList
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableSharedFlow
Expand Down Expand Up @@ -51,7 +52,7 @@ class RemindersScreenVM @Inject constructor(
.collect { reminders ->
_uiState.update {
it.copy(
reminders = reminders,
reminders = reminders.toImmutableList(),
isLoading = false,
infoMessage = it.infoMessage
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.raghav.spacedawnv2.util.launchDetailDtoString
import com.raghav.spacedawnv2.util.launchesResponseDtoString
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.Moshi
import kotlinx.collections.immutable.toImmutableList
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.TestCoroutineScheduler
Expand Down Expand Up @@ -59,7 +60,7 @@ class LaunchesScreenVMTest {
launchesResponse?.results?.filterNotNull()
?: emptyList()

val expected = LaunchesScreenState(launches = launches)
val expected = LaunchesScreenState(launches = launches.toImmutableList())

assertThat(result).isEqualTo(expected)
}
Expand Down
7 changes: 4 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
plugins {
id 'com.android.application' version '8.1.0' apply false
id 'com.android.library' version '8.1.0' apply false
id 'org.jetbrains.kotlin.android' version '1.7.20' apply false
id 'org.jetbrains.kotlin.jvm' version '1.8.20' apply false
id 'org.jetbrains.kotlin.android' version '1.9.0' apply false
id 'org.jetbrains.kotlin.jvm' version '1.9.0' apply false
id "org.jlleitschuh.gradle.ktlint" version "11.5.0"
id 'com.google.dagger.hilt.android' version '2.44' apply false
id 'com.google.dagger.hilt.android' version '2.47' apply false
id("com.google.devtools.ksp") version "1.9.0-1.0.13" apply false
}
12 changes: 6 additions & 6 deletions data/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ plugins {
id 'com.android.library'
id 'org.jetbrains.kotlin.android'
id "org.jlleitschuh.gradle.ktlint"
// Hilt doesn't supports KSP yet
id 'kotlin-kapt'
id("com.google.devtools.ksp")
id "kotlin-kapt"
id 'com.google.dagger.hilt.android'
}

Expand Down Expand Up @@ -63,17 +63,17 @@ dependencies {
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation("com.squareup.retrofit2:converter-moshi:2.9.0")
implementation("com.squareup.moshi:moshi:1.14.0")
kapt("com.squareup.moshi:moshi-kotlin-codegen:1.14.0")
ksp("com.squareup.moshi:moshi-kotlin-codegen:1.14.0")
implementation "com.squareup.okhttp3:okhttp:5.0.0-alpha.2"
implementation "com.squareup.okhttp3:logging-interceptor:5.0.0-alpha.2"

// Hilt
implementation "com.google.dagger:hilt-android:2.44"
kapt "com.google.dagger:hilt-compiler:2.44"
implementation "com.google.dagger:hilt-android:2.47"
kapt "com.google.dagger:hilt-compiler:2.47"

// Room
implementation "androidx.room:room-runtime:2.5.1"
kapt "androidx.room:room-compiler:2.5.1"
ksp "androidx.room:room-compiler:2.5.1"
implementation "androidx.room:room-ktx:2.5.1"

// For making Assertions in Test cases
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ data class LaunchDetailDto(
@Json(name = "failreason")
val failreason: String?,
@Json(name = "hashtag")
val hashtag: Any?,
val hashtag: String?,
@Json(name = "holdreason")
val holdreason: String?,
@Json(name = "id")
val id: String,
@Json(name = "image")
val image: String?,
@Json(name = "infographic")
val infographic: Any?,
val infographic: String?,
@Json(name = "last_updated")
val lastUpdated: String?,
@Json(name = "launch_service_provider")
Expand Down Expand Up @@ -55,9 +55,7 @@ data class LaunchDetailDto(
@Json(name = "pad_launch_attempt_count_year")
val padLaunchAttemptCountYear: Int?,
@Json(name = "probability")
val probability: Any?,
@Json(name = "program")
val program: List<ProgramDto?>?,
val probability: String?,
@Json(name = "rocket")
val rocketDto: RocketDto?,
@Json(name = "slug")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.raghav.spacedawnv2.data.remote.dto
import com.raghav.spacedawnv2.domain.model.LaunchesResponse
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import kotlinx.collections.immutable.toImmutableList

// @JsonClass is used to make sure that Moshi uses code-gen instead of Reflection
// for Serializing and Deserializing data
Expand All @@ -13,7 +14,7 @@ data class LaunchesResponseDto(
@Json(name = "next")
val next: String?,
@Json(name = "previous")
val previous: Any?,
val previous: String?,
@Json(name = "results")
val results: List<LaunchDetailDto?>?
)
Expand All @@ -23,6 +24,6 @@ fun LaunchesResponseDto.toDomain(): LaunchesResponse {
count = count,
next = next,
previous = previous,
results = results?.map { it?.toLaunchDetail() }
results = results?.map { it?.toLaunchDetail() }?.toImmutableList()
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ data class MissionDto(
@Json(name = "id")
val id: Int?,
@Json(name = "launch_designator")
val launchDesignator: Any?,
val launchDesignator: String?,
@Json(name = "name")
val name: String?,
@Json(name = "orbit")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ data class PadDto(
@Json(name = "id")
val id: Int?,
@Json(name = "info_url")
val infoUrl: Any?,
val infoUrl: String?,
@Json(name = "latitude")
val latitude: String?,
@Json(name = "location")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
package com.raghav.spacedawnv2.data.remote.dto

import com.raghav.spacedawnv2.domain.model.Program
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import kotlinx.collections.immutable.ImmutableList

// @JsonClass is used to make sure that Moshi uses code-gen instead of Reflection
// for Serializing and Deserializing data
@JsonClass(generateAdapter = true)
data class ProgramDto(
@Json(name = "agencies")
val agencies: List<AgencyDto?>?,
val agencies: ImmutableList<AgencyDto?>?,
@Json(name = "description")
val description: String?,
@Json(name = "end_date")
val endDate: Any?,
val endDate: String?,
@Json(name = "id")
val id: Int?,
@Json(name = "image_url")
val imageUrl: String?,
@Json(name = "info_url")
val infoUrl: String?,
@Json(name = "mission_patches")
val missionPatches: List<Any?>?,
val missionPatches: ImmutableList<String?>?,
@Json(name = "name")
val name: String?,
@Json(name = "start_date")
Expand All @@ -31,19 +31,3 @@ data class ProgramDto(
@Json(name = "wiki_url")
val wikiUrl: String?
)

fun ProgramDto.toProgram(): Program {
return Program(
agencies = agencies?.map { it?.toAgency() },
description = description,
end_date = endDate,
id = id,
image_url = imageUrl,
info_url = infoUrl,
mission_patches = missionPatches,
name = name,
start_date = startDate,
url = url,
wiki_url = wikiUrl
)
}
5 changes: 4 additions & 1 deletion domain/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
plugins {
id 'java-library'
id 'org.jetbrains.kotlin.jvm'
id("com.google.devtools.ksp") version "1.8.20-1.0.11"
id("com.google.devtools.ksp")
id "org.jlleitschuh.gradle.ktlint"
}

Expand Down Expand Up @@ -42,4 +42,7 @@ dependencies {

def okhttp_interceptor = "5.0.0-alpha.2"
api "com.squareup.okhttp3:logging-interceptor:$okhttp_interceptor"

// For Making Kotlin Collections Immutable as to be considered stable
api("org.jetbrains.kotlinx:kotlinx-collections-immutable:0.3.5")
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.raghav.spacedawnv2.domain.model

import androidx.annotation.Keep
import kotlinx.collections.immutable.ImmutableList

@Keep
data class LaunchesResponse(
val count: Int?,
val next: String?,
val previous: Any?,
val results: List<LaunchDetail?>?
val previous: String?,
val results: ImmutableList<LaunchDetail?>?
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.squareup.moshi.JsonClass
data class Mission(
val description: String?,
val id: Int?,
val launch_designator: Any?,
val launch_designator: String?,
val name: String?,
val orbit: Orbit?,
val type: String?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ data class Pad(
val agency_id: Int?,
val country_code: String?,
val id: Int?,
val info_url: Any?,
val info_url: String?,
val latitude: String?,
val location: Location?,
val longitude: String?,
Expand Down

This file was deleted.

Loading