Skip to content

Commit

Permalink
Update usage of Strava API to use "sport_type" instead of "type" (#25)
Browse files Browse the repository at this point in the history
* This commit updates usage of Strava API to use "sport_type" instead of "type"
* To translate complicated new sport types, an enum class SportType is used instead of storing a String for the type which was used previously.
  • Loading branch information
Tyler-Lopez authored Feb 16, 2023
1 parent 5dcb6e0 commit bed7fbb
Show file tree
Hide file tree
Showing 22 changed files with 251 additions and 102 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ android {
applicationId "com.activityartapp"
minSdk 26
targetSdk 33
versionCode 9
versionName "1.2.1"
versionCode 10
versionName "1.3.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import com.activityartapp.data.entities.OAuth2Entity

@Database(
entities = [ActivityEntity::class, AthleteCacheDictionaryEntity::class, OAuth2Entity::class],
version = 2
version = 3
)
@TypeConverters(Converters::class)
abstract class AthleteDatabase : RoomDatabase() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.activityartapp.data.entities
import androidx.room.Entity
import androidx.room.PrimaryKey
import com.activityartapp.domain.models.Activity
import com.activityartapp.util.enums.SportType

@Entity
data class ActivityEntity(
Expand All @@ -22,5 +23,5 @@ data class ActivityEntity(
override val sufferScore: Int?,
override val iso8601LocalDate: String,
override val summaryPolyline: String?,
override val type: String, // "Ride"
override val sportType: SportType,
) : Activity
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.activityartapp.data.remote.responses

import com.activityartapp.domain.models.Activity
import com.activityartapp.util.enums.SportType
import com.google.gson.annotations.SerializedName

data class ActivityResponse(
Expand All @@ -26,54 +27,21 @@ data class ActivityResponse(
override val movingTime: Int,
@SerializedName("name")
override val name: String,
@SerializedName("type")
override val type: String,
@SerializedName("sport_type")
val sportTypeRaw: String,
@SerializedName("start_date_local")
override val iso8601LocalDate: String,
@SerializedName("suffer_score")
override val sufferScore: Int?,

val achievement_count: Int,
val athlete: AthleteWithResourceState,
val athlete_count: Int,
val average_cadence: Double,
val comment_count: Int,
val commute: Boolean,
val display_hide_heartrate_option: Boolean,
val elapsed_time: Int,
val elev_high: Double,
val elev_low: Double,
val end_latlng: List<Double>,
val external_id: String,
val flagged: Boolean,
val from_accepted_tag: Boolean,
val has_heartrate: Boolean,
val has_kudoed: Boolean,
val heartrate_opt_out: Boolean,
val manual: Boolean,
val map: Map?,
val photo_count: Int,
val pr_count: Int,
val `private`: Boolean,
val resource_state: Int,
val start_date: String,
val start_latitude: Double,
val start_latlng: List<Double>,
val start_longitude: Double,
val timezone: String,
val total_elevation_gain: Double,
val total_photo_count: Int,
val trainer: Boolean,
val upload_id: Long,
val upload_id_str: String,
val utc_offset: Double,
val visibility: String,
val workout_type: Int
val map: Map?
) : Activity {

override val athleteId: Long
get() = athlete.id

override val summaryPolyline: String?
get() = map?.summary_polyline

override val sportType: SportType
get() = SportType.fromSportTypeString(sportTypeRaw)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.activityartapp.domain.models

import com.activityartapp.util.enums.SportType

interface Activity {
val athleteId: Long
val averageSpeed: Double
Expand All @@ -16,5 +18,6 @@ interface Activity {
val name: String // "Happy Friday"
val sufferScore: Int?
val summaryPolyline: String?
val type: String // "Ride"
val sportType: SportType

}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class GetActivitiesByYearFromRemote @Inject constructor(
activities.addAll(data)
}
.doOnError {
println("Here, an error occurred, that error was ${this.exception}")
return Response.Error(data = activities, exception = this.exception)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class InsertActivitiesIntoDisk @Inject constructor(
sufferScore,
iso8601LocalDate,
summaryPolyline,
type
sportType
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,11 @@ class MainActivity : ComponentActivity(), Router<MainDestination> {

private fun navigateSaveArt(destination: NavigateSaveArt) {
destination.apply {
println("yo here, the json is ${gson.toJson(activityTypes)}")
navController.navigate(
route = SaveArt.withArgs(
args = arrayOf(
ActivityTypes to gson.toJson(activityTypes),
ActivityTypes to gson.toJson(activityTypes.map { it.toString() }),
BackgroundType to backgroundType.toString(),
ColorActivitiesArgb to colorActivitiesArgb.toString(),
ColorsBackgroundArgb to gson.toJson(backgroundColorsArgb),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import com.activityartapp.architecture.ViewEvent
import com.activityartapp.architecture.ViewState
import com.activityartapp.presentation.editArtScreen.StrokeWidthType
import com.activityartapp.presentation.errorScreen.ErrorScreenType
import com.activityartapp.util.enums.BackgroundType
import com.activityartapp.util.enums.EditArtSortDirectionType
import com.activityartapp.util.enums.EditArtSortType
import com.activityartapp.util.enums.FontSizeType
import com.activityartapp.util.enums.*

sealed interface MainViewState : ViewState {
object Unauthenticated : MainViewState
Expand All @@ -34,7 +31,7 @@ sealed interface MainDestination : Destination {
) : MainDestination

data class NavigateSaveArt(
val activityTypes: List<String>,
val activityTypes: List<SportType>,
val backgroundType: BackgroundType,
val backgroundColorsArgb: List<Int>,
val colorActivitiesArgb: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ sealed interface EditArtViewEvent : ViewEvent {
}
}

data class FilterTypeToggled(val type: String) : FilterChanged {
data class FilterTypeToggled(val type: SportType) : FilterChanged {
override val filterType: EditArtFilterType
get() = EditArtFilterType.TYPE
}
Expand Down Expand Up @@ -179,7 +179,7 @@ sealed interface EditArtViewState : ViewState {
val filterDistanceTotalEnd: Double? = null,
@IgnoredOnParcel val filterDistancePendingChangeStart: String? = null,
@IgnoredOnParcel val filterDistancePendingChangeEnd: String? = null,
val filterTypes: Map<String, Boolean>? = null,
val filterTypes: Map<SportType, Boolean>? = null,
@IgnoredOnParcel override val pagerStateWrapper: PagerStateWrapper = PagerStateWrapper(
pagerHeaders = EditArtHeaderType.values().toList(),
pagerState = PagerState(EditArtHeaderType.values().toList().size),
Expand Down Expand Up @@ -274,10 +274,7 @@ sealed interface EditArtViewState : ViewState {
) != NO_ACTIVITIES_COUNT

@IgnoredOnParcel
val filterDateSelectionUnset = filterDateSelectionIndex == INIT_SELECTION_INDEX

@IgnoredOnParcel
val filteredTypes: List<String>
val filteredTypes: List<SportType>
get() = filterTypes?.entries?.filter { it.value }?.map { it.key } ?: emptyList()

val filteredDistanceRangeMeters: IntRange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class EditArtViewModel @Inject constructor(
private val activitiesSummedDistance: Double get() = activitiesFiltered.sumOf { it.distance }

/** Updates [activitiesFilteredByFilterType] for a given [EditArtFilterType].
* Designates which activities this particular filter type is in-charge of filtering. **/
* Designates which activities this particular filter sportType is in-charge of filtering. **/
private fun EditArtFilterType.updateFilteredActivities() {
withLastState {
val prevActivities = activitiesFilteredByFilterType[lastFilter] ?: activities
Expand Down Expand Up @@ -203,7 +203,7 @@ class EditArtViewModel @Inject constructor(
* the various saved filters. **/
it.updateFilteredActivities()
} else {
/** Otherwise, simply initialize filtered activities for each type as all activities and
/** Otherwise, simply initialize filtered activities for each sportType as all activities and
* initialize filters. **/
activitiesFilteredByFilterType[it] = activities
it.pushUpdatedFilteredActivityCountToView()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.activityartapp.presentation.editArtScreen.EditArtFilterType.*
import com.activityartapp.presentation.editArtScreen.EditArtViewEvent
import com.activityartapp.presentation.editArtScreen.subscreens.filters.composables.FilterSectionDate
import com.activityartapp.presentation.editArtScreen.subscreens.filters.composables.FilterSectionDistances
import com.activityartapp.util.enums.SportType

@Composable
fun ColumnScope.EditArtFiltersScreen(
Expand All @@ -22,7 +23,7 @@ fun ColumnScope.EditArtFiltersScreen(
distanceTotal: ClosedFloatingPointRange<Double>?,
distancePendingChangeStart: String?,
distancePendingChangeEnd: String?,
typesWithSelectedFlag: List<Pair<String, Boolean>>?,
typesWithSelectedFlag: List<Pair<SportType, Boolean>>?,
eventReceiver: EventReceiver<EditArtViewEvent>
) {
EditArtFilterType.values().onEach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ import com.activityartapp.architecture.EventReceiver
import com.activityartapp.presentation.editArtScreen.EditArtViewEvent
import com.activityartapp.presentation.ui.theme.spacing
import com.activityartapp.presentation.editArtScreen.subscreens.filters.composables.FilterSection
import com.activityartapp.util.enums.SportType

@Composable
fun ColumnScope.FilterSectionActivityType(
count: Int,
typesWithSelectedFlag: List<Pair<String, Boolean>>,
typesWithSelectedFlag: List<Pair<SportType, Boolean>>,
eventReceiver: EventReceiver<EditArtViewEvent>
) {
FilterSection(
Expand All @@ -41,7 +42,7 @@ fun ColumnScope.FilterSectionActivityType(
)
})
Text(
text = typeMap.first,
text = stringResource(typeMap.first.stringRes),
style = MaterialTheme.typography.body1
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ import com.activityartapp.presentation.saveArtScreen.SaveArtViewState.Standby.Do
import com.activityartapp.presentation.saveArtScreen.SaveArtViewEvent.*
import com.activityartapp.util.*
import com.activityartapp.util.NavArgSpecification.*
import com.activityartapp.util.enums.*
import com.activityartapp.util.enums.BackgroundType
import com.activityartapp.util.enums.EditArtSortDirectionType
import com.activityartapp.util.enums.EditArtSortType
import com.activityartapp.util.enums.FontSizeType
import com.google.gson.Gson
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
Expand All @@ -40,10 +38,12 @@ class SaveArtViewModel @Inject constructor(
ssh: SavedStateHandle,
) : BaseRoutingViewModel<SaveArtViewState, SaveArtViewEvent, MainDestination>() {

private val activityTypes = gson.fromJson<List<String>>(
ActivityTypes.rawArg(ssh),
List::class.java
)
private val activityTypes = gson
.fromJson<List<String>>(
ActivityTypes.rawArg(ssh),
List::class.java
)
.map { SportType.valueOf(it) }
private val activities = getActivitiesFromMemory()
private val backgroundType =
BackgroundType.valueOf(NavArgSpecification.BackgroundType.rawArg(ssh))
Expand Down Expand Up @@ -163,6 +163,7 @@ class SaveArtViewModel @Inject constructor(
}

private fun createArtBitmapOfSize(isPreview: Boolean, size: Size): Bitmap {
println("here, activity types are $activityTypes")
return visualizationUtils.createBitmap(
activities = activityFilterUtils.filterActivities(
activities = activities,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fun List<Activity>.parcelize(): List<ParcelableActivity> {
it.name,
it.sufferScore,
it.summaryPolyline,
it.type
it.sportType
)
}
}
22 changes: 13 additions & 9 deletions app/src/main/java/com/activityartapp/util/ActivityFilterUtils.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.activityartapp.util

import android.util.Range
import com.activityartapp.domain.models.Activity
import com.activityartapp.presentation.editArtScreen.DateSelection
import com.activityartapp.presentation.editArtScreen.EditArtViewModel
import com.activityartapp.util.enums.SportType
import java.util.concurrent.TimeUnit
import javax.inject.Inject
import kotlin.math.roundToInt
Expand All @@ -18,12 +17,13 @@ class ActivityFilterUtils @Inject constructor(

fun filterActivities(
activities: List<Activity>,
includeActivityTypes: Collection<String>,
includeActivityTypes: Collection<SportType>,
unixMsRange: LongProgression,
distanceRange: IntRange
): List<Activity> {
println(unixMsRange)
return activities.filter {
println("checking to see if ${it.sportType} is in $includeActivityTypes")
activityWithinDistanceRange(it, distanceRange) &&
activityWithinUnixMs(it, unixMsRange) &&
activityTypeContainedWithinTypes(it, includeActivityTypes)
Expand All @@ -46,8 +46,12 @@ class ActivityFilterUtils @Inject constructor(

fun activityTypeContainedWithinTypes(
activity: Activity,
types: Collection<String>
): Boolean = types.contains(activity.type)
types: Collection<SportType>
): Boolean = types.contains(activity.sportType).also {
println("result $it types was $types")
println("activity sport type ${activity.sportType}")
println("sport type in types: ${types.contains(activity.sportType)}")
}

/**
* Provided a [List] of [Activity], returns all possible [DateSelection]. Returns null
Expand Down Expand Up @@ -96,12 +100,12 @@ class ActivityFilterUtils @Inject constructor(

fun getPossibleActivityTypes(
activities: List<Activity>,
filterTypesPrevious: Map<String, Boolean>?
): Map<String, Boolean>? {
filterTypesPrevious: Map<SportType, Boolean>?
): Map<SportType, Boolean>? {
return activities
.takeIf { it.isNotEmpty() }
?.distinctBy(Activity::type)
?.map(Activity::type)
?.distinctBy(Activity::sportType)
?.map(Activity::sportType)
?.sorted()
?.associateWith {
filterTypesPrevious?.get(it) ?: DEFAULT_ACTIVITY_TYPE_SELECTION
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ActivitySortUtils @Inject constructor(
when (sortType) {
EditArtSortType.DATE -> timeUtils.iso8601StringToUnixSecond(it.iso8601LocalDate)
EditArtSortType.DISTANCE -> it.distance
EditArtSortType.TYPE -> it.type
EditArtSortType.TYPE -> it.sportType
}
})
.run {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.activityartapp.util

import android.os.Parcelable
import com.activityartapp.domain.models.Activity
import com.activityartapp.util.enums.SportType
import kotlinx.parcelize.Parcelize

@Parcelize
Expand All @@ -21,5 +22,5 @@ data class ParcelableActivity(
override val name: String,
override val sufferScore: Int?,
override val summaryPolyline: String?,
override val type: String
override val sportType: SportType
) : Activity, Parcelable
Loading

0 comments on commit bed7fbb

Please sign in to comment.