Skip to content

Commit

Permalink
Add queries
Browse files Browse the repository at this point in the history
  • Loading branch information
ksharma-xyz committed Sep 3, 2024
1 parent 928bf43 commit b7fcb56
Show file tree
Hide file tree
Showing 12 changed files with 186 additions and 45 deletions.
14 changes: 14 additions & 0 deletions app/src/main/java/xyz/ksharma/krail/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,32 @@ import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.lifecycle.lifecycleScope
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import timber.log.Timber
import xyz.ksharma.krail.database.api.SydneyTrainsStaticDB
import xyz.ksharma.krail.design.system.theme.StartTheme
import javax.inject.Inject

@AndroidEntryPoint
class MainActivity : ComponentActivity() {

@Inject
lateinit var realSydneyTrainsStaticDb: SydneyTrainsStaticDB

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

// Configures the system bars with a transparent background.
enableEdgeToEdge()

lifecycleScope.launch {
realSydneyTrainsStaticDb.insertStopTimes()
val x = realSydneyTrainsStaticDb.getStopTimes()
Timber.d("$x")
}

setContent {
StartTheme {
KrailApp()
Expand Down
18 changes: 18 additions & 0 deletions core/di/src/main/kotlin/xyz/ksharma/krail/di/CoroutinesModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package xyz.ksharma.krail.di

import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob

@Module
@InstallIn(SingletonComponent::class)
object CoroutinesModule {

@Provides
fun provideCoroutineScope(@Dispatcher(AppDispatchers.IO) ioDispatcher: CoroutineDispatcher): CoroutineScope =
CoroutineScope(context = ioDispatcher + SupervisorJob())
}
9 changes: 9 additions & 0 deletions feature/sydney-trains/database/api/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
plugins {
alias(libs.plugins.krail.android.library)
alias(libs.plugins.cash.sqldelight)
}

android {
namespace = "xyz.ksharma.krail.sydney_trains.database.api"
}

sqldelight {
databases {
create("KrailDB") {
packageName.set("xyz.ksharma.krail")
}
}
}

dependencies {
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package xyz.ksharma.krail.database.api

import xyz.ksharma.krail.StopTimes

interface SydneyTrainsStaticDB {

suspend fun insertStopTimes()

suspend fun clearStopTimes()

suspend fun getStopTimes(): List<StopTimes>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
CREATE TABLE stopTimes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
trip_id TEXT NOT NULL,
arrival_time TEXT,
departure_time TEXT,
stop_id TEXT,
stop_sequence INTEGER,
stop_headsign TEXT,
pickup_type INTEGER,
drop_off_type INTEGER
);

insertIntoStopTime:
INSERT INTO stopTimes (
trip_id,
arrival_time,
departure_time,
stop_id,
stop_sequence,
stop_headsign,
pickup_type,
drop_off_type
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?);

selectAll:
SELECT * FROM stopTimes;
9 changes: 0 additions & 9 deletions feature/sydney-trains/database/real/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
plugins {
alias(libs.plugins.krail.android.library)
alias(libs.plugins.krail.android.hilt)
alias(libs.plugins.cash.sqldelight)
}

android {
namespace = "xyz.ksharma.krail.sydney_trains.database.real"
}

sqldelight {
databases {
create("KrailDB") {
packageName.set("xyz.ksharma.krail")
}
}
}

dependencies {
implementation(projects.krail.feature.sydneyTrains.database.api)
implementation(projects.core.di)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,26 @@ class RealSydneyTrainsDb @Inject constructor(
@ApplicationContext private val applicationContext: Context,
) : SydneyTrainsDB {

private lateinit var krailDB: KrailDB

init {
coroutineScope.launch { createDb() }
coroutineScope.launch {
krailDB = KrailDB(
driver = AndroidSqliteDriver(
schema = KrailDB.Schema,
context = applicationContext,
name = "krail.db"
)
)
}
}

override suspend fun createDb(): Unit = withContext(ioDispatcher) {
val db = KrailDB(
driver = AndroidSqliteDriver(
schema = KrailDB.Schema,
context = applicationContext,
name = "krail_db.db"
)
)

}

override suspend fun insertStopTimes() {
TODO("Not yet implemented")

}

override suspend fun clearStopTimes() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package xyz.ksharma.krail

import android.content.Context
import app.cash.sqldelight.driver.android.AndroidSqliteDriver
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.async
import xyz.ksharma.krail.database.api.SydneyTrainsStaticDB
import xyz.ksharma.krail.di.AppDispatchers
import xyz.ksharma.krail.di.Dispatcher
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class RealSydneyTrainsStaticDb @Inject constructor(
@Dispatcher(AppDispatchers.IO) private val ioDispatcher: CoroutineDispatcher,
coroutineScope: CoroutineScope,
@ApplicationContext private val applicationContext: Context,
) : SydneyTrainsStaticDB {

private val sydneyTrainsDB: Deferred<KrailDB> by lazy {
coroutineScope.async(ioDispatcher) {
KrailDB(
AndroidSqliteDriver(
KrailDB.Schema,
applicationContext,
"sydney_trains_static.db"
)
)
}
}

private suspend fun getSydneyTrainsDb(): KrailDB = sydneyTrainsDB.await()

override suspend fun insertStopTimes() {
getSydneyTrainsDb().stoptimesQueries.insertStopTimes(
trip_id = "id_1",
arrival_time = "arr",
departure_time = "depart",
stop_id = "",
stop_sequence = 1,
stop_headsign = "",
pickup_type = 1,
drop_off_type = 1,
)
}

override suspend fun getStopTimes(): List<StopTimes> {
val all = getSydneyTrainsDb().stoptimesQueries.selectAll()
return all.executeAsList()
}

override suspend fun clearStopTimes() {
TODO("Not yet implemented")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import xyz.ksharma.krail.RealSydneyTrainsDb
import xyz.ksharma.krail.database.api.SydneyTrainsDB
import xyz.ksharma.krail.RealSydneyTrainsStaticDb
import xyz.ksharma.krail.database.api.SydneyTrainsStaticDB
import javax.inject.Singleton

@Module
Expand All @@ -14,5 +14,5 @@ abstract class SydneyTrainsDbModule {

@Binds
@Singleton
abstract fun bindTrainsDb(impl: RealSydneyTrainsDb): SydneyTrainsDB
abstract fun bindSydneyTrainsStaticDB(impl: RealSydneyTrainsStaticDb): SydneyTrainsStaticDB
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
CREATE TABLE stopTimes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
trip_id TEXT NOT NULL,
arrival_time TEXT,
departure_time TEXT,
stop_id TEXT,
stop_sequence INTEGER,
stop_headsign TEXT,
pickup_type INTEGER,
drop_off_type INTEGER
);

insertIntoStopTime:
INSERT INTO stopTimes (
trip_id,
arrival_time,
departure_time,
stop_id,
stop_sequence,
stop_headsign,
pickup_type,
drop_off_type
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?);

selectAll:
<<<<<<<< Updated upstream:feature/sydney-trains/database/real/src/main/sqldelight/xyz/ksharma/krail/database/stoptimes.sq
SELECT *
FROM stopTimes;
========
SELECT * FROM stopTimes;
>>>>>>>> Stashed changes:feature/sydney-trains/database/api/src/main/sqldelight/xyz/ksharma/krail/stoptimes.sq

This file was deleted.

0 comments on commit b7fcb56

Please sign in to comment.