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

[22438] Ticket caching fix #464

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import com.skedgo.tripkit.data.database.booking.ticket.TicketDao
import com.skedgo.tripkit.data.database.booking.ticket.TicketEntity
import com.skedgo.tripkit.data.database.locations.bikepods.BikePodDao
Expand Down Expand Up @@ -50,7 +52,7 @@ import skedgo.tripgo.data.timetables.ParentStopEntity
ServiceAlertsEntity::class,
TicketEntity::class,
FacilityLocationEntity::class,
], version = 7
], version = 8
)
abstract class TripKitDatabase : RoomDatabase() {
abstract fun carParkDao(): CarParkDao
Expand All @@ -70,10 +72,17 @@ abstract class TripKitDatabase : RoomDatabase() {
return Room.databaseBuilder(
context.applicationContext,
TripKitDatabase::class.java, "tripkit.db"
)
).addMigrations(MIGRATION_7_8)
.fallbackToDestructiveMigration()
.build()
}

val MIGRATION_7_8 = object : Migration(7, 8) {
override fun migrate(database: SupportSQLiteDatabase) {
// Add the new column with a default value
database.execSQL("ALTER TABLE tickets ADD COLUMN userId TEXT DEFAULT NULL")
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,7 @@ interface TicketDao {

@Query("SELECT * FROM tickets")
fun getAllTicketsRx(): Single<List<TicketEntity>>

@Query("SELECT * FROM tickets WHERE userId = :userId")
fun getTicketsByUserIdRx(userId: String?): Maybe<List<TicketEntity>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ data class TicketEntity(
val status: String?,
val qrCode: String? = null,
val ticketActionsJson: String?, // Serialized TicketAction
val userId: String? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ data class Ticket(
)
}

fun Ticket.toEntity(): TicketEntity {
fun Ticket.toEntity(userId: String? = null): TicketEntity {
val gson = Gson()
return TicketEntity(
id = id,
Expand All @@ -177,7 +177,8 @@ data class Ticket(
fareJson = gson.toJson(fare),
status = status,
ticketActionsJson = if (actions.isNullOrEmpty()) "" else gson.toJson(actions),
qrCode = qrCode
qrCode = qrCode,
userId = userId
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,17 @@ class QuickBookingRepository @Inject constructor(
emit(Result.error(e.message ?: "Unknown error"))
}.flowOn(Dispatchers.IO)

fun getTicketsRx(): Single<List<Ticket>> {
fun getTicketsRx(userId: String?): Single<List<Ticket>> {
return quickBookingService.getTickets(true)
.flatMap { tickets ->
if (tickets.isNotEmpty()) {
// insertTicketsRx() saves all tickets and returns Completable
ticketDao.insertTicketsRx(tickets.map { it.toEntity() })
ticketDao.insertTicketsRx(tickets.map { it.toEntity(userId) })
.andThen(Single.just(tickets))
} else {
Single.just(tickets)
ticketDao.getTicketsByUserIdRx(userId).toSingle().flatMap {
Single.just(it.map { it.toTicket() })
}
}
}
}
Expand Down