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

Added longest streak calculation. #56

Merged
merged 19 commits into from
Nov 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
d30df41
WTA #39: Updated StreakRange addition and updated tests.
Jacob3075 Nov 11, 2022
d79996b
WTA #39: Refactored RecalculateLatestStreakUC to not use recursive ap…
Jacob3075 Nov 11, 2022
e98a0ac
WTA #39: Refactored StreakRange.
Jacob3075 Nov 11, 2022
b56ad3f
WTA #39: Started working on CalculateLongestStreakUC and added test s…
Jacob3075 Nov 11, 2022
1604487
Updated AGP version.
Jacob3075 Nov 12, 2022
b6c198b
WTA #39: Added code for CalculateLongestStreakUC.
Jacob3075 Nov 12, 2022
248dbff
WTA #39: Added some tests.
Jacob3075 Nov 12, 2022
34b9eb8
WTA #39: Added rest of the tests.
Jacob3075 Nov 12, 2022
b01adad
WTA #39: Started updating CalculateLongestStreakUC to fetch different…
Jacob3075 Nov 12, 2022
ce4ae32
WTA #39: Updated CalculateLongestStreakUC to fetch and process differ…
Jacob3075 Nov 12, 2022
01f8aff
WTA #39: Updated view-model and other use-cases to handle longest str…
Jacob3075 Nov 13, 2022
86ba7fa
WTA #39: Updated tests.
Jacob3075 Nov 13, 2022
4b3c734
WTA #39: Cleaned up visibility access modifiers.
Jacob3075 Nov 13, 2022
7bb6479
WTA #39: Fixed some bugs with mapper and query parameter definition.
Jacob3075 Nov 13, 2022
facf53f
WTA #39: Removed Streaks and renamed StreakRange to Streak.
Jacob3075 Nov 13, 2022
c9da8bb
WTA #39: Added Timeout error and updated how exception messages are d…
Jacob3075 Nov 13, 2022
09c5220
WTA #39: Updated retrofit request timeout value.
Jacob3075 Nov 13, 2022
b45ca99
WTA #39: Updated Streak.canBeCombinedWith to not perform the addition…
Jacob3075 Nov 13, 2022
e77c279
WTA #39: Added better error message when CalculateLongestStreakUC tim…
Jacob3075 Nov 13, 2022
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
25 changes: 25 additions & 0 deletions .idea/runConfigurations/CalculateCurrentStreakUCTest.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions .idea/runConfigurations/CalculateLongestStreakUCTest.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions .idea/runConfigurations/GetCachedHomePageUiDataUCTest.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions .idea/runConfigurations/GetLast7DaysStatsUCTest.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions .idea/runConfigurations/RecalculateLatestStreakUCTest.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions .idea/runConfigurations/StreakTest.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath("com.android.tools.build:gradle:8.0.0-alpha07")
classpath("com.android.tools.build:gradle:8.0.0-alpha08")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10")
classpath("com.google.dagger:hilt-android-gradle-plugin:2.42")
classpath("de.mannodermaus.gradle.plugins:android-junit5:1.8.2.1")
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ buildscript {
}

dependencies {
implementation("com.android.tools.build:gradle:8.0.0-alpha07")
implementation("com.android.tools.build:gradle:8.0.0-alpha08")
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10")

implementation("org.jetbrains.kotlin:kotlin-serialization:1.7.20")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import java.time.Duration
import javax.inject.Singleton
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand All @@ -19,6 +20,7 @@ import kotlinx.serialization.json.Json
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import okhttp3.logging.HttpLoggingInterceptor.Level.BODY
import retrofit2.Retrofit

@Module
Expand All @@ -37,10 +39,11 @@ object DataModule {
@Provides
fun provideOkHttpClient(): OkHttpClient {
val builder = OkHttpClient.Builder()
val loggingInterceptor = HttpLoggingInterceptor()
.setLevel(HttpLoggingInterceptor.Level.BODY)
builder.networkInterceptors()
.add(loggingInterceptor)
.readTimeout(Duration.ofMinutes(1))
.connectTimeout(Duration.ofMinutes(1))

builder.networkInterceptors().add(HttpLoggingInterceptor().setLevel(BODY))

return builder.build()
}

Expand All @@ -55,10 +58,8 @@ object DataModule {
@ExperimentalSerializationApi
@Singleton
@Provides
fun provideRetrofit(okHttpClient: OkHttpClient, json: Json) = Retrofit.Builder()
.addConverterFactory(
json.asConverterFactory("application/json".toMediaType())
)
fun provideRetrofit(okHttpClient: OkHttpClient, json: Json): Retrofit = Retrofit.Builder()
.addConverterFactory(json.asConverterFactory("application/json".toMediaType()))
.baseUrl(BASE_URL)
.client(okHttpClient)
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ sealed class Error {
override val statusCode = -1
}

data class Timeout(override val message: String) : NetworkErrors() {
override val statusCode = -1
}

data class GenericError(override val message: String) : NetworkErrors() {
override val statusCode: Int = -1
override val statusCode = -1
}

data class ClientError(override val message: String, override val statusCode: Int) :
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.jacob.wakatimeapp.core.models

import kotlinx.datetime.LocalDate
import kotlinx.serialization.Serializable

@Serializable
Expand All @@ -14,7 +15,7 @@ data class UserDetails(
val lastProject: String,
val fullName: String,
val durationsSliceBy: String,
val createdAt: String,
val createdAt: LocalDate,
val dateFormat: String,
val photoUrl: String,
)
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ kotlin.code.style=official

org.gradle.unsafe.configuration-cache=true
org.gradle.unsafe.configuration-cache-problems=warn
android.nonTransitiveRClass=false
20 changes: 9 additions & 11 deletions home/src/main/java/com/jacob/wakatimeapp/home/HomePageModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@ import retrofit2.Retrofit

@Module
@InstallIn(SingletonComponent::class)
object HomePageModule {
@Singleton
@Provides
fun provideHomePageService(retrofit: Retrofit): HomePageAPI =
retrofit.create(HomePageAPI::class.java)
}

@Module
@InstallIn(SingletonComponent::class)
interface HomePageModuleBinds {
abstract class HomePageModule {
@Binds
fun provideInstantProvider(impl: DefaultInstantProvider): InstantProvider
abstract fun provideInstantProvider(impl: DefaultInstantProvider): InstantProvider

companion object {
@Singleton
@Provides
internal fun provideHomePageService(retrofit: Retrofit) =
retrofit.create(HomePageAPI::class.java)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import com.jacob.wakatimeapp.core.models.Error
import com.jacob.wakatimeapp.core.models.Error.DatabaseError
import com.jacob.wakatimeapp.home.domain.InstantProvider
import com.jacob.wakatimeapp.home.domain.models.Last7DaysStats
import com.jacob.wakatimeapp.home.domain.models.StreakRange
import com.jacob.wakatimeapp.home.domain.models.Streak
import javax.inject.Inject
import javax.inject.Singleton
import kotlinx.coroutines.flow.catch
Expand Down Expand Up @@ -55,24 +55,36 @@ class HomePageCache @Inject constructor(
}
}

fun getCurrentStreak() = dataStore.data.map<Preferences, Either<Error, StreakRange>> {
val streakRange = it[KEY_CURRENT_STREAK]?.let<String, StreakRange>(json::decodeFromString)
?: StreakRange.ZERO
streakRange.right()
}.catch {
Timber.e(it)
emit(DatabaseError.UnknownError(it.message!!, it).left())
fun getCurrentStreak() = getStreak(KEY_CURRENT_STREAK)

suspend fun updateCurrentStreak(streak: Streak) {
dataStore.edit {
it[KEY_CURRENT_STREAK] = json.encodeToString(streak)
}
}

suspend fun updateCurrentStreak(streakRange: StreakRange) {
fun getLongestStreak() = getStreak(KEY_LONGEST_STREAK)

suspend fun updateLongestStreak(streak: Streak) {
dataStore.edit {
it[KEY_CURRENT_STREAK] = json.encodeToString(streakRange)
it[KEY_LONGEST_STREAK] = json.encodeToString(streak)
}
}

private fun getStreak(key: Preferences.Key<String>) =
dataStore.data.map<Preferences, Either<Error, Streak>> {
val streak = it[key]?.let<String, Streak>(json::decodeFromString)
?: Streak.ZERO
streak.right()
}.catch {
Timber.e(it)
emit(DatabaseError.UnknownError(it.message!!, it).left())
}

companion object {
private val KEY_LAST_REQUEST_TIME = longPreferencesKey("KEY_LAST_REQUEST_TIME")
private val KEY_LAST_7_DAYS_STATS = stringPreferencesKey("KEY_LAST_7_DAYS_STATS")
private val KEY_CURRENT_STREAK = stringPreferencesKey("KEY_CURRENT_STREAK")
private val KEY_LONGEST_STREAK = stringPreferencesKey("KEY_LONGEST_STREAK")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import com.jacob.wakatimeapp.home.data.network.dtos.GetStatsForRangeResDTO
import retrofit2.Response
import retrofit2.http.GET
import retrofit2.http.Header
import retrofit2.http.Path
import retrofit2.http.Query

interface HomePageAPI {
internal interface HomePageAPI {
@GET("/api/v1/users/current/all_time_since_today")
suspend fun getData(@Header("Authorization") token: String): Response<AllTimeDataDTO>

Expand All @@ -19,10 +19,10 @@ interface HomePageAPI {
@GET("/api/v1/users/current/summaries?range=last_7_days")
suspend fun getLast7DaysStats(@Header("Authorization") token: String): Response<GetLast7DaysStatsResDTO>

@GET("/api/v1/users/current/summaries?start={start}&end={end}")
@GET("/api/v1/users/current/summaries")
suspend fun getStatsForRange(
@Header("Authorization") token: String,
@Path("start") start: String,
@Path("end") end: String,
@Query("start") start: String,
@Query("end") end: String,
): Response<GetStatsForRangeResDTO>
}
Loading