Skip to content

Commit

Permalink
NTV-273: Location Model (#1498)
Browse files Browse the repository at this point in the history
* - Migrate to kotlin
* - Using Parcelize over Autoparcel library
* - Factory migrated to kotlin
* - Added test

Co-authored-by: leighdouglas <leighcdouglas1@gmail.com>
  • Loading branch information
Arkariang and leighdouglas committed Jan 4, 2022
1 parent 565349e commit 853db18
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 142 deletions.

This file was deleted.

@@ -0,0 +1,76 @@
package com.kickstarter.mock.factories

import com.kickstarter.models.Location
import com.kickstarter.models.Location.Companion.builder

object LocationFactory {
@JvmStatic
fun germany(): Location {
return builder()
.id(1L)
.displayableName("Berlin, Germany")
.name("Berlin")
.state("Berlin")
.country("DE")
.expandedCountry("Germany")
.build()
}

@JvmStatic
fun mexico(): Location {
return builder()
.id(2L)
.displayableName("Mexico City, Mexico")
.name("Mexico City")
.state("Mexico")
.country("MX")
.expandedCountry("Mexico")
.build()
}

fun nigeria(): Location {
return builder()
.id(3L)
.displayableName("Nigeria")
.name("Nigeria")
.state("Imo State")
.country("NG")
.expandedCountry("Nigeria")
.build()
}

@JvmStatic
fun sydney(): Location {
return builder()
.id(4L)
.name("Sydney")
.displayableName("Sydney, AU")
.country("AU")
.state("NSW")
.projectsCount(33)
.expandedCountry("Australia")
.build()
}

@JvmStatic
fun unitedStates(): Location {
return builder()
.id(5L)
.displayableName("Brooklyn, NY")
.name("Brooklyn")
.state("NY")
.country("US")
.expandedCountry("United States")
.build()
}

fun empty(): Location {
return builder()
.id(-1L)
.displayableName("")
.name("")
.country("")
.expandedCountry("")
.build()
}
}
67 changes: 0 additions & 67 deletions app/src/main/java/com/kickstarter/models/Location.java

This file was deleted.

92 changes: 92 additions & 0 deletions app/src/main/java/com/kickstarter/models/Location.kt
@@ -0,0 +1,92 @@
package com.kickstarter.models

import android.os.Parcelable
import kotlinx.android.parcel.Parcelize

@Parcelize
class Location private constructor(
private val id: Long,
private val city: String,
private val country: String,
private val displayableName: String,
private val expandedCountry: String,
private val name: String,
private val projectsCount: Int,
private val state: String
) : Parcelable, Relay {

fun displayableName() = this.displayableName
fun city() = this.city
fun country() = this.country
fun expandedCountry() = this.expandedCountry
fun name() = this.name
fun state() = this.state
fun projectsCount() = this.projectsCount
override fun id(): Long = this.id

@Parcelize
data class Builder(
private var id: Long = 0L,
private var city: String = "",
private var country: String = "",
private var displayableName: String = "",
private var expandedCountry: String = "",
private var name: String = "",
private var projectsCount: Int = 0,
private var state: String = ""
) : Parcelable {
fun displayableName(dName: String?) = apply { dName?.let { this.displayableName = it } }
fun city(city: String?) = apply { city?.let { this.city = it } }
fun country(count: String?) = apply { count?.let { this.country = it } }
fun expandedCountry(expCount: String?) = apply { expCount?.let { this.expandedCountry = it } }
fun name(name: String?) = apply { name?.let { this.name = it } }
fun state(state: String?) = apply { state?.let { this.state = it } }
fun projectsCount(pCount: Int?) = apply { pCount?.let { this.projectsCount = it } }
fun id(id: Long?) = apply { id?.let { this.id = it } }
fun build() = Location(
id = id,
city = city,
country = country,
displayableName = displayableName,
expandedCountry = expandedCountry,
name = name,
projectsCount = projectsCount,
state = state
)
}

companion object {
@JvmStatic
fun builder() = Builder()
}

fun toBuilder() = Builder(
id = id,
city = city,
country = country,
displayableName = displayableName,
expandedCountry = expandedCountry,
name = name,
projectsCount = projectsCount,
state = state
)

override fun equals(obj: Any?): Boolean {
var equals = super.equals(obj)
if (obj is Location) {
equals = id() == obj.id() &&
city() == obj.city() &&
country() == obj.country() &&
displayableName() == obj.displayableName() &&
expandedCountry() == obj.expandedCountry() &&
name() == obj.name() &&
projectsCount() == obj.projectsCount() &&
state() == obj.state()
}
return equals
}

override fun hashCode(): Int {
return super.hashCode()
}
}
13 changes: 13 additions & 0 deletions app/src/test/java/com/kickstarter/models/LocationTest.kt
Expand Up @@ -39,4 +39,17 @@ class LocationTest : TestCase() {

assertFalse(locA == locB)
}

fun testDefaultInit() {
val loc = Location.builder().build()

assertTrue(loc.displayableName() == "")
assertTrue(loc.city() == "")
assertTrue(loc.country() == "")
assertTrue(loc.expandedCountry() == "")
assertTrue(loc.name() == "")
assertTrue(loc.state() == "")
assertTrue(loc.projectsCount() == 0)
assertTrue(loc.id() == 0L)
}
}

0 comments on commit 853db18

Please sign in to comment.