Skip to content

Commit 1f376c3

Browse files
authored
Merge pull request #7 from bit-shift-studios/feat/setup-db
2 parents fc12889 + 9f042ac commit 1f376c3

17 files changed

Lines changed: 187 additions & 8 deletions

File tree

.idea/kotlinc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ android {
3838
}
3939
kotlinOptions {
4040
jvmTarget = "17"
41+
freeCompilerArgs = freeCompilerArgs + "-opt-in=androidx.compose.material3.ExperimentalMaterial3Api"
4142
}
4243
buildFeatures {
4344
compose = true

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
xmlns:tools="http://schemas.android.com/tools">
44

55
<application
6+
android:name=".application.FlightsApplication"
67
android:allowBackup="true"
78
android:dataExtractionRules="@xml/data_extraction_rules"
89
android:fullBackupContent="@xml/backup_rules"
@@ -15,7 +16,6 @@
1516
<activity
1617
android:name=".MainActivity"
1718
android:exported="true"
18-
android:label="@string/app_name"
1919
android:theme="@style/Theme.FlightsApp">
2020
<intent-filter>
2121
<action android:name="android.intent.action.MAIN" />
16 KB
Binary file not shown.

app/src/main/java/bitshift/studios/flightsapp/MainActivity.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ import androidx.compose.material3.Text
1010
import androidx.compose.runtime.Composable
1111
import androidx.compose.ui.Modifier
1212
import androidx.compose.ui.tooling.preview.Preview
13-
import bitshift.studios.flightsapp.ui.theme.FlightsAppTheme
13+
import bitshift.studios.flightsapp.presentation.ui.theme.FlightsAppTheme
14+
import dagger.hilt.android.AndroidEntryPoint
1415

16+
@AndroidEntryPoint
1517
class MainActivity : ComponentActivity() {
1618
override fun onCreate(savedInstanceState: Bundle?) {
1719
super.onCreate(savedInstanceState)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package bitshift.studios.flightsapp.application
2+
3+
import android.app.Application
4+
import dagger.hilt.android.HiltAndroidApp
5+
6+
@HiltAndroidApp
7+
class FlightsApplication : Application()
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package bitshift.studios.flightsapp.application.di
2+
3+
import android.app.Application
4+
import androidx.room.Room
5+
import bitshift.studios.flightsapp.data.db.airport.AirportDatabase
6+
import bitshift.studios.flightsapp.data.db.bookmarked.BookmarkedAirportsDatabase
7+
import dagger.Module
8+
import dagger.Provides
9+
import dagger.hilt.InstallIn
10+
import dagger.hilt.components.SingletonComponent
11+
import javax.inject.Singleton
12+
13+
@Module
14+
@InstallIn(SingletonComponent::class)
15+
object AppModule {
16+
@Provides
17+
@Singleton
18+
fun providesAirportDatabase(app: Application): AirportDatabase {
19+
return Room
20+
.databaseBuilder(
21+
app.applicationContext,
22+
AirportDatabase::class.java,
23+
AirportDatabase.databaseName
24+
)
25+
.createFromAsset("database/flight_search.db")
26+
.fallbackToDestructiveMigration()
27+
.build()
28+
}
29+
30+
@Provides
31+
@Singleton
32+
fun providesBookmarkedAirportsDatabase(app: Application): BookmarkedAirportsDatabase {
33+
return Room
34+
.databaseBuilder(
35+
app.applicationContext,
36+
BookmarkedAirportsDatabase::class.java,
37+
BookmarkedAirportsDatabase.databaseName
38+
)
39+
.fallbackToDestructiveMigration()
40+
.build()
41+
}
42+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package bitshift.studios.flightsapp.data.db.airport
2+
3+
import androidx.room.Dao
4+
import androidx.room.Query
5+
import bitshift.studios.flightsapp.data.db.airport.entities.AirportEntity
6+
import kotlinx.coroutines.flow.Flow
7+
8+
@Dao
9+
interface AirportDao {
10+
@Query(
11+
"""
12+
SELECT * FROM airport WHERE (name LIKE :identifier OR iata_code LIKE :identifier)
13+
ORDER BY passengers DESC
14+
"""
15+
)
16+
fun getFlightByNameOrCode(identifier: String): Flow<List<AirportEntity>>
17+
18+
@Query(
19+
"""
20+
SELECT * FROM airport WHERE iata_code = :code
21+
"""
22+
)
23+
fun getFlightsFromAirportByCode(code: String): Flow<List<AirportEntity>>
24+
25+
@Query(
26+
"""
27+
SELECT name FROM airport WHERE iata_code = :code
28+
"""
29+
)
30+
suspend fun getAirportNameByCode(code: String): String
31+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package bitshift.studios.flightsapp.data.db.airport
2+
3+
import androidx.room.Database
4+
import androidx.room.RoomDatabase
5+
import bitshift.studios.flightsapp.data.db.airport.entities.AirportEntity
6+
7+
@Database(
8+
entities = [AirportEntity::class],
9+
exportSchema = false,
10+
version = 1
11+
)
12+
abstract class AirportDatabase : RoomDatabase() {
13+
abstract fun dao(): AirportDao
14+
15+
companion object {
16+
val databaseName = "airport_db"
17+
}
18+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package bitshift.studios.flightsapp.data.db.airport.entities
2+
3+
import androidx.room.ColumnInfo
4+
import androidx.room.Entity
5+
import androidx.room.PrimaryKey
6+
7+
@Entity(tableName = "airport")
8+
data class AirportEntity(
9+
@PrimaryKey
10+
val id: Int,
11+
@ColumnInfo(name = "iata_code")
12+
val iataCode: String,
13+
val name: String,
14+
val passengers: Int
15+
)

0 commit comments

Comments
 (0)