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

Add sizeOfStopTimes query #33

Merged
merged 2 commits into from
Sep 10, 2024
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
24 changes: 14 additions & 10 deletions app/src/main/java/xyz/ksharma/krail/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,11 @@ import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.lifecycle.lifecycleScope
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import timber.log.Timber
import xyz.ksharma.krail.database.sydney.trains.database.api.SydneyTrainsStaticDB
import xyz.ksharma.krail.design.system.theme.StartTheme
import xyz.ksharma.krail.sydney.trains.database.real.parser.StopTimesParser.parseStopTimes
import xyz.ksharma.krail.model.sydneytrains.GTFSFeedFileNames
import xyz.ksharma.krail.sydney.trains.network.api.repository.SydneyTrainsRepository
import xyz.ksharma.krail.utils.toPath
import java.time.Instant
import java.time.temporal.ChronoUnit
import javax.inject.Inject

@AndroidEntryPoint
Expand All @@ -36,10 +29,12 @@ class MainActivity : ComponentActivity() {
enableEdgeToEdge()

lifecycleScope.launch {
deleteStaticGtfsFiles()

repository.fetchStaticSydneyTrainsScheduleAndCache()

delay(5000)
val startTime = Instant.now()

/*val startTime = Instant.now()
parseStopTimes(
path = applicationContext.toPath(GTFSFeedFileNames.STOP_TIMES.fileName),
ioDispatcher = Dispatchers.IO,
Expand All @@ -51,7 +46,7 @@ class MainActivity : ComponentActivity() {
Timber.d("Time taken - $diff")

val dataSize = realSydneyTrainsStaticDb.stopTimesSize()
Timber.d("DATA SIZE: $dataSize")
Timber.d("DATA SIZE: $dataSize")*/
}

setContent {
Expand All @@ -60,4 +55,13 @@ class MainActivity : ComponentActivity() {
}
}
}

private fun deleteStaticGtfsFiles() {
val cacheDir = cacheDir
cacheDir.listFiles()?.forEach { file ->
if (file.isFile && file.name.endsWith(".txt")) {
file.delete()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import xyz.ksharma.krail.network.BuildConfig
import xyz.ksharma.krail.network.interceptor.AuthInterceptor
import java.util.concurrent.TimeUnit

@Module
@InstallIn(SingletonComponent::class)
Expand All @@ -23,6 +24,13 @@ class NetworkModule {
)
}
okhttpBuilder.addInterceptor(AuthInterceptor())

// Add Timeouts
okhttpBuilder
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(5, TimeUnit.MINUTES)
.writeTimeout(1, TimeUnit.MINUTES)

return okhttpBuilder.build()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,10 @@ class RealZipCacheManager @Inject constructor(
val isDirectory = zipEntry.name.endsWith(File.separator)
val path: Path = context.toPath(zipEntry.name)

/*
if (zipEntry.name == GTFSFeedFileNames.STOP_TIMES.fileName) {
/*if (zipEntry.name == GTFSFeedFileNames.STOP_TIMES.fileName) {
sydneyTrainsStaticDB.insertStopTimes()
}
}*/
inputStream.writeToCache(isDirectory, path)
*/

zipEntry = inputStream.nextEntry
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import okhttp3.Response

interface SydneyTrainsService {

suspend fun getSydneyTrainsStaticData(): Response
suspend fun getSydneyTrainsStaticData(): Result<Response>
}
1 change: 1 addition & 0 deletions feature/sydney-trains/network/real/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ android {
}

dependencies {
implementation(projects.core.coroutinesExt)
implementation(projects.feature.sydneyTrains.network.api)
implementation(projects.feature.sydneyTrains.database.api)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package xyz.ksharma.krail.sydney.trains.network.real

import kotlinx.coroutines.CoroutineDispatcher
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import xyz.ksharma.krail.coroutines.ext.safeResult
import xyz.ksharma.krail.di.AppDispatchers
import xyz.ksharma.krail.di.Dispatcher
import xyz.ksharma.krail.network.di.NetworkModule.Companion.BASE_URL
import xyz.ksharma.krail.network.interceptor.AuthInterceptor.Companion.API_KEY
import xyz.ksharma.krail.sydney.trains.network.api.SydneyTrainsService
Expand All @@ -11,14 +16,15 @@ import javax.inject.Singleton
@Singleton
class RealSydneyTrainsService @Inject constructor(
private val okHttpClient: OkHttpClient,
@Dispatcher(AppDispatchers.IO) private val ioDispatcher: CoroutineDispatcher,
) : SydneyTrainsService {

override suspend fun getSydneyTrainsStaticData(): okhttp3.Response {
override suspend fun getSydneyTrainsStaticData(): Result<Response> {
val request = Request.Builder().url("$BASE_URL/v1/gtfs/schedule/sydneytrains")
.header("Authorization", "apikey $API_KEY")
.header("accept", "application/x-google-protobuf").build()
.header("Authorization", "apikey $API_KEY").build()

val response = okHttpClient.newCall(request).execute()
return response
return safeResult(ioDispatcher) {
okHttpClient.newCall(request).execute()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,16 @@ class RealSydneyTrainsRepository @Inject constructor(
override suspend fun fetchStaticSydneyTrainsScheduleAndCache(): Unit =
withContext(ioDispatcher) {
Timber.d("getSydneyTrains: ")
val response: Response = gtfsService.getSydneyTrainsStaticData()
cacheManager.cacheZipResponse(response)
val result: Result<Response> = gtfsService.getSydneyTrainsStaticData()

// TODO - provide boolean response if files were cached successfully.
Timber.d("Response received")
if (result.isSuccess) {
result.getOrNull()?.let { response ->
// TODO - provide boolean response if files were cached successfully.
cacheManager.cacheZipResponse(response)
}
} else if (result.isFailure) {
Timber.e("Error fetching Sydney Trains static GTFS data: ${result.exceptionOrNull()}")
}
}
}