Skip to content

Commit

Permalink
Added retrofit dependencies, created quotable API DI, created retrofi…
Browse files Browse the repository at this point in the history
…t DI, Created Statistics viewmodel, finished Statistics view
  • Loading branch information
RamziJabali committed Jun 4, 2024
1 parent 9fb53f6 commit a7f6d5e
Show file tree
Hide file tree
Showing 15 changed files with 270 additions and 21 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ dependencies {
implementation(libs.androidx.compose.foundation)
implementation(libs.work.manager)
implementation(libs.koin.di)
implementation(libs.com.squareup.retrofit2)
implementation(libs.com.squareup.retrofit2.converter)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name=".util.JustJogApplication"
android:allowBackup="true"
Expand Down
7 changes: 6 additions & 1 deletion app/src/main/java/ramzi/eljabali/justjog/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@ import androidx.annotation.RequiresApi
import androidx.compose.material3.FabPosition
import androidx.compose.material3.Scaffold
import androidx.core.content.ContextCompat
import androidx.lifecycle.lifecycleScope
import androidx.navigation.compose.rememberNavController
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.flow.single
import kotlinx.coroutines.launch
import org.koin.android.ext.android.inject
import ramzi.eljabali.justjog.loactionservice.ForegroundService
import ramzi.eljabali.justjog.motivationalquotes.MotivationQuotesAPI
import ramzi.eljabali.justjog.util.permissions
import ramzi.eljabali.justjog.ui.design.JustJogTheme
import ramzi.eljabali.justjog.ui.views.JoggingFAB
Expand All @@ -32,8 +36,9 @@ class MainActivity : ComponentActivity() {
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

checkPermissionStatus()
val jogUseCase by inject<JogUseCase>()

setContent {
val navController = rememberNavController()
JustJogTheme(true) {
Expand Down
23 changes: 20 additions & 3 deletions app/src/main/java/ramzi/eljabali/justjog/koin/Module.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ import androidx.room.Room
import org.koin.android.ext.koin.androidApplication
import org.koin.androidx.viewmodel.dsl.viewModel
import org.koin.dsl.module
import ramzi.eljabali.justjog.motivationalquotes.MotivationQuotesAPI
import ramzi.eljabali.justjog.repository.room.database.JustJogDataBase
import ramzi.eljabali.justjog.repository.room.jogentries.JogEntryDAO
import ramzi.eljabali.justjog.repository.room.jogsummary.JogSummaryDAO
import ramzi.eljabali.justjog.repository.room.jogsummarytemp.JogSummaryTempDAO
import ramzi.eljabali.justjog.usecase.JogUseCase
import ramzi.eljabali.justjog.viewmodel.JogViewModel
import ramzi.eljabali.justjog.viewmodel.StatisticsViewModel
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

val statisticsModule = module {
viewModel { JogViewModel() }
viewModel { StatisticsViewModel(get<JogUseCase>(), get<MotivationQuotesAPI>()) }
}

val jogDataBaseModule = module {
Expand All @@ -28,5 +31,19 @@ val jogDataBaseModule = module {
}

val jogUseCaseModule = module {
single { JogUseCase(get(), get(), get()) }
single { JogUseCase(get<JogEntryDAO>(), get<JogSummaryDAO>(), get<JogSummaryTempDAO>()) }
}

const val BASE_URL = "https://api.quotable.io/"
val networkModule = module {
single<MotivationQuotesAPI> { get<Retrofit>().create(MotivationQuotesAPI::class.java) }
single<Retrofit> {
Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
}
}
val viewModelModule = module {
viewModel { StatisticsViewModel(get<JogUseCase>(), get<MotivationQuotesAPI>()) }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ramzi.eljabali.justjog.motivationalquotes

import retrofit2.Response
import retrofit2.http.GET

interface MotivationQuotesAPI {
@GET("/quotes/random?minLength=100&maxLength=140&tags=Motivational/")
suspend fun getQuote(): Response<Array<MotivationalQuote>>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ramzi.eljabali.justjog.motivationalquotes

import com.google.gson.annotations.SerializedName

data class MotivationalQuote(
@SerializedName("_id")
val id: String,
@SerializedName("content")
val quote: String,
@SerializedName("author")
val author: String,
@SerializedName("tags")
val tags: Array<String>,
@SerializedName("authorSlug")
val authorSlug: String,
@SerializedName("length")
val length: Int,
@SerializedName("dateAdded")
val dateAdded: String,
@SerializedName("dateModified")
val dateModified: String
)
24 changes: 22 additions & 2 deletions app/src/main/java/ramzi/eljabali/justjog/usecase/JogUseCase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import android.util.Log
import com.google.android.gms.maps.model.LatLng
import javatimefun.zoneddatetime.extensions.print
import javatimefun.zoneddatetime.extensions.toZonedDateTime
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.single
import ramzi.eljabali.justjog.motivationalquotes.MotivationQuotesAPI
import ramzi.eljabali.justjog.repository.room.jogentries.JogEntry
import ramzi.eljabali.justjog.repository.room.jogentries.JogEntryDAO
import ramzi.eljabali.justjog.repository.room.jogsummary.JogSummary
Expand All @@ -17,6 +19,7 @@ import ramzi.eljabali.justjog.repository.room.jogsummarytemp.JogSummaryTempDAO
import ramzi.eljabali.justjog.util.DateFormat
import ramzi.eljabali.justjog.util.TAG
import java.time.Duration
import java.time.ZonedDateTime

class JogUseCase(
private val jogEntryDao: JogEntryDAO,
Expand All @@ -36,13 +39,13 @@ class JogUseCase(
// SUMMARY
suspend fun getNewJogID(): Int {
Log.i(TAG, "getNewJogID() -> JogUseCase")
val id = jogSummaryDao.getLastID().firstOrNull()
val id = jogSummaryDao.getLastID().firstOrNull()

return if (id == null) {
Log.i(TAG, "getNewJogID() -> 1")
1
} else {
Log.i(TAG, "getNewJogID() -> ${id+1}")
Log.i(TAG, "getNewJogID() -> ${id + 1}")
id + 1
}
}
Expand All @@ -58,6 +61,22 @@ class JogUseCase(
)
}

fun getJogSummariesBetweenDates(
startDate: ZonedDateTime,
endDate: ZonedDateTime
): Flow<List<ModifiedJogSummary>> =
jogSummaryDao.getBetweenDatesUseCase(startDate, endDate).map { list ->
list.map {
ModifiedJogSummary(
jogId = it.id,
startDate = it.startDate.toZonedDateTime(DateFormat.YYYY_MM_DD_T_TIME.format)!!,
duration = Duration.ofSeconds(it.totalJogDuration),
totalDistance = it.totalJogDistance
)
}
}


// SUMMARY TEMP
fun getJogSummaryTemp(id: Int) = jogSummaryTempDao.getById(id).map { jogSummaryTemp ->
if (jogSummaryTemp != null) {
Expand All @@ -72,6 +91,7 @@ class JogUseCase(
null
}
}

fun deleteAllTempJogSummaries() = jogSummaryTempDao.deleteAll()
fun addOrUpdateJogSummaryTemp(modifiedTempJogSummary: ModifiedTempJogSummary) {
jogSummaryTempDao.add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@ package ramzi.eljabali.justjog.util

enum class DurationFormat(val format: String) {
HH_MM_SS("hh:mm:ss"),
H_M_S("h:m:s")
H_M_S("h:m:s"),
HMS("h m s"),
MS("m s")
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ import org.koin.core.context.GlobalContext.startKoin
import ramzi.eljabali.justjog.R
import ramzi.eljabali.justjog.koin.jogDataBaseModule
import ramzi.eljabali.justjog.koin.jogUseCaseModule
import ramzi.eljabali.justjog.koin.networkModule
import ramzi.eljabali.justjog.koin.statisticsModule

class JustJogApplication : Application() {
private val CHANNEL_ID_1 = "JUST_JOG_1"
private val modules = listOf(statisticsModule, jogDataBaseModule, jogUseCaseModule)
private val modules =
listOf(statisticsModule, jogDataBaseModule, jogUseCaseModule, networkModule)

override fun onCreate() {
super.onCreate()
startKoin {
Expand Down
16 changes: 11 additions & 5 deletions app/src/main/java/ramzi/eljabali/justjog/util/Time.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,18 @@ fun getFormattedTime(totalTimeInSeconds: Long, format: DurationFormat): String {
}


fun getFormattedTimeMinutes(totalTimeInMinutes: Long): String {
var tempTime = minutesToSeconds(totalTimeInMinutes)
fun getFormattedTimeSeconds(totalTimeInSeconds: Long, format: DurationFormat = DurationFormat.HH_MM_SS): String {
var tempTime = totalTimeInSeconds
val totalHours = secondsToHours(tempTime)
tempTime -= hoursToSeconds(totalHours)
tempTime %= 3600
val totalMinutes = secondsToMinutes(tempTime)
tempTime -= minutesToSeconds(totalMinutes)
tempTime %= 60
val totalSeconds = tempTime
return String.format("%02d:%02d:%02d", totalHours, totalMinutes, totalSeconds)

return when (format) {
DurationFormat.HH_MM_SS -> String.format("%02d:%02d:%02d", totalHours, totalMinutes, totalSeconds)
DurationFormat.H_M_S -> String.format("%d:%d:%d", totalHours, totalMinutes, totalSeconds)
DurationFormat.HMS -> String.format("%02dh %02dm %02ds", totalHours, totalMinutes, totalSeconds)
DurationFormat.MS -> String.format("%01dm %02ds", totalHours, totalMinutes, totalSeconds)
}
}

This file was deleted.

Loading

0 comments on commit a7f6d5e

Please sign in to comment.