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

GTFS: Add file storage and service implementation #571

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions composeApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ kotlin {
implementation(projects.feature.tripPlanner.ui)
implementation(projects.sandook)
implementation(projects.taj)
implementation(projects.gtfsStatic)

implementation(libs.navigation.compose)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import xyz.ksharma.krail.core.appinfo.di.appInfoModule
import xyz.ksharma.krail.core.di.DispatchersComponent.Companion.IODispatcher
import xyz.ksharma.krail.core.di.coroutineDispatchersModule
import xyz.ksharma.krail.core.remote_config.di.remoteConfigModule
import xyz.ksharma.krail.gtfs_static.di.fileStorageModule
import xyz.ksharma.krail.gtfs_static.di.gtfsModule
import xyz.ksharma.krail.sandook.di.sandookModule
import xyz.ksharma.krail.splash.SplashViewModel
import xyz.ksharma.krail.trip.planner.network.api.di.networkModule
Expand All @@ -27,6 +29,8 @@ val koinConfig = koinConfiguration {
analyticsModule,
remoteConfigModule,
coroutineDispatchersModule,
gtfsModule,
fileStorageModule,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class SplashViewModel(
.onStart {
loadKrailThemeStyle()
trackAppStartEvent()

// TODO - handle app stat events separately
remoteConfig.setup() // App Start Event
}.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), true)

Expand Down
1 change: 1 addition & 0 deletions core/test/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ kotlin {
implementation(projects.feature.tripPlanner.ui)
implementation(projects.feature.tripPlanner.state)
implementation(projects.feature.tripPlanner.network)
implementation(projects.gtfsStatic)
implementation(projects.taj)

implementation(libs.test.kotlin)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package xyz.ksharma.core.test.fakes

import xyz.ksharma.krail.gtfs_static.NswGtfsService

class FakeGtfsService : NswGtfsService {

override suspend fun getSydneyTrains() {
// Do nothing
}

override suspend fun getSydneyMetro() {
// Do nothing
}

override suspend fun getLightRail() {
// Do nothing
}

override suspend fun getNswTrains() {
// Do nothing
}

override suspend fun getSydneyFerries() {
// Do nothing
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.test.setMain
import xyz.ksharma.core.test.fakes.FakeAnalytics
import xyz.ksharma.core.test.fakes.FakeGtfsService
import xyz.ksharma.core.test.fakes.FakeSandook
import xyz.ksharma.core.test.helpers.AnalyticsTestHelper.assertScreenViewEventTracked
import xyz.ksharma.krail.core.analytics.Analytics
import xyz.ksharma.krail.core.analytics.AnalyticsScreen
import xyz.ksharma.krail.core.analytics.event.AnalyticsEvent
import xyz.ksharma.krail.gtfs_static.NswGtfsService
import xyz.ksharma.krail.sandook.Sandook
import xyz.ksharma.krail.trip.planner.ui.savedtrips.SavedTripsViewModel
import xyz.ksharma.krail.trip.planner.ui.state.savedtrip.SavedTripUiEvent
Expand All @@ -31,14 +33,15 @@ class SavedTripsViewModelTest {

private val sandook: Sandook = FakeSandook()
private val fakeAnalytics: Analytics = FakeAnalytics()
private val gtfsService: NswGtfsService = FakeGtfsService()
private lateinit var viewModel: SavedTripsViewModel

private val testDispatcher = StandardTestDispatcher()

@BeforeTest
fun setUp() {
Dispatchers.setMain(testDispatcher)
viewModel = SavedTripsViewModel(sandook, fakeAnalytics, testDispatcher)
viewModel = SavedTripsViewModel(sandook, fakeAnalytics, testDispatcher, gtfsService)
}

@AfterTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ actual fun httpClient(appInfoProvider: AppInfoProvider): HttpClient {
level = LogLevel.BODY
logger = object : Logger {
override fun log(message: String) {
println(message)
// println(message)
}
}
sanitizeHeader { header -> header == HttpHeaders.Authorization }
Expand Down
1 change: 1 addition & 0 deletions feature/trip-planner/ui/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ kotlin {
implementation(projects.core.remoteConfig)
implementation(projects.feature.tripPlanner.network)
implementation(projects.feature.tripPlanner.state)
implementation(projects.gtfsStatic)
implementation(projects.sandook)
implementation(projects.taj)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ val viewModelsModule = module {
sandook = get(),
analytics = get(),
ioDispatcher = get(named(IODispatcher)),
gtfsService = get(),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import xyz.ksharma.krail.core.analytics.AnalyticsScreen
import xyz.ksharma.krail.core.analytics.event.AnalyticsEvent
import xyz.ksharma.krail.core.analytics.event.trackScreenViewEvent
import xyz.ksharma.krail.core.log.log
import xyz.ksharma.krail.gtfs_static.NswGtfsService
import xyz.ksharma.krail.sandook.Sandook
import xyz.ksharma.krail.sandook.SavedTrip
import xyz.ksharma.krail.trip.planner.ui.state.savedtrip.SavedTripUiEvent
Expand All @@ -28,12 +29,14 @@ class SavedTripsViewModel(
private val sandook: Sandook,
private val analytics: Analytics,
private val ioDispatcher: CoroutineDispatcher,
private val gtfsService: NswGtfsService,
) : ViewModel() {

private val _uiState: MutableStateFlow<SavedTripsState> = MutableStateFlow(SavedTripsState())
val uiState: StateFlow<SavedTripsState> = _uiState
.onStart {
analytics.trackScreenViewEvent(screen = AnalyticsScreen.SavedTrips)
gtfsService.getSydneyTrains() // App Start Event
}.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), SavedTripsState())

fun onEvent(event: SavedTripUiEvent) {
Expand Down
1 change: 1 addition & 0 deletions gtfs-static/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ kotlin {
implementation(projects.core.di)

api(libs.di.koinComposeViewmodel)
implementation(libs.firebase.gitLiveCrashlytics)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
package xyz.ksharma.krail.gtfs_static

import android.content.Context
import dev.gitlive.firebase.Firebase
import dev.gitlive.firebase.crashlytics.crashlytics
import xyz.ksharma.krail.core.log.log
import java.io.File

class AndroidFileStorage(private val context: Context) : FileStorage {
override suspend fun saveFile(fileName: String, data: ByteArray) {
val file = File(context.filesDir, fileName)
file.writeBytes(data)
runCatching {
val file = File(context.filesDir, fileName)
file.writeBytes(data)
}.onFailure {
log("Failed to save file: $it")
Firebase.crashlytics.recordException(it)
}.onSuccess {
log("File saved at: ${context.filesDir.absolutePath}")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import kotlinx.coroutines.withContext
import xyz.ksharma.krail.core.di.DispatchersComponent
import xyz.ksharma.krail.core.log.log

class RealNswGtfsService(
internal class RealNswGtfsService(
private val httpClient: HttpClient,
private val fileStorage: FileStorage,
private val ioDispatcher: CoroutineDispatcher = DispatchersComponent().ioDispatcher,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package xyz.ksharma.krail.gtfs_static.di

import org.koin.core.module.Module
import org.koin.core.module.dsl.bind
import org.koin.core.module.dsl.singleOf
import org.koin.dsl.module
import xyz.ksharma.krail.gtfs_static.NswGtfsService
import xyz.ksharma.krail.gtfs_static.RealNswGtfsService

val gtfsModule = module {
singleOf(::RealNswGtfsService) { bind<NswGtfsService>() }
single<NswGtfsService> {
RealNswGtfsService(get(), get())
}
}

expect val fileStorageModule: Module
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package xyz.ksharma.krail.gtfs_static

import dev.gitlive.firebase.Firebase
import dev.gitlive.firebase.crashlytics.crashlytics
import kotlinx.cinterop.ExperimentalForeignApi
import kotlinx.cinterop.addressOf
import kotlinx.cinterop.usePinned
Expand All @@ -14,21 +16,28 @@ import xyz.ksharma.krail.core.log.log
class IosFileStorage : FileStorage {
@OptIn(ExperimentalForeignApi::class)
override suspend fun saveFile(fileName: String, data: ByteArray) {
log("Saving file: $fileName")
val fileManager = NSFileManager.defaultManager
val directory = fileManager.URLForDirectory(
directory = NSDocumentDirectory,
inDomain = NSUserDomainMask,
appropriateForURL = null,
create = true,
error = null,
)?.path
log("Trying to Save file: $fileName")
runCatching {
val fileManager = NSFileManager.defaultManager
val directory = fileManager.URLForDirectory(
directory = NSDocumentDirectory,
inDomain = NSUserDomainMask,
appropriateForURL = null,
create = true,
error = null,
)?.path

val filePath = "$directory/$fileName"
data.usePinned { pinned ->
NSData.dataWithBytes(pinned.addressOf(0), data.size.toULong())
.writeToFile(filePath, true)
val filePath = "$directory/$fileName"
data.usePinned { pinned ->
NSData.dataWithBytes(pinned.addressOf(0), data.size.toULong())
.writeToFile(filePath, true)
}
log("File saved: $filePath")
}.onFailure {
log("Failed to save file: $it")
Firebase.crashlytics.recordException(it) // todo - move to another module
}.onSuccess {
log("File saved successfully")
}
log("File saved: $filePath")
}
}
Loading