-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #603 from igorescodro/kmp/pre-populate
✨ Introduce category pre-population on iOS
- Loading branch information
Showing
10 changed files
with
140 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 21 additions & 43 deletions
64
data/local/src/androidMain/kotlin/com/escodro/local/provider/AndroidDriverFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,41 @@ | ||
package com.escodro.local.provider | ||
|
||
import android.annotation.SuppressLint | ||
import android.content.Context | ||
import app.cash.sqldelight.db.SqlDriver | ||
import app.cash.sqldelight.driver.android.AndroidSqliteDriver | ||
import com.escodro.coroutines.AppCoroutineScope | ||
import com.escodro.local.AlkaaDatabase | ||
import com.escodro.local.Category | ||
import com.escodro.local.R | ||
import com.escodro.resources.MR | ||
import dev.icerock.moko.resources.desc.desc | ||
|
||
/** | ||
* Provides the platform-specific [SqlDriver] to be used in the database. | ||
*/ | ||
internal class AndroidDriverFactory( | ||
private val context: Context, | ||
private val appCoroutineScope: AppCoroutineScope, | ||
) : DriverFactory { | ||
|
||
/** | ||
* Creates the platform-specific [SqlDriver] to be used in the database. | ||
* | ||
* @param databaseName the database name | ||
* | ||
* @return the [SqlDriver] to be used in the database | ||
*/ | ||
override fun createDriver(databaseName: String): SqlDriver = | ||
AndroidSqliteDriver(AlkaaDatabase.Schema, context, databaseName) | ||
|
||
override fun prepopulateDatabase(database: AlkaaDatabase, databaseName: String) { | ||
val databaseFile = context.getDatabasePath(databaseName) | ||
if (!databaseFile.exists()) { | ||
appCoroutineScope.launch { | ||
for (category in getDefaultCategoryList()) { | ||
database.categoryQueries.insert( | ||
category_name = category.category_name, | ||
category_color = category.category_color, | ||
) | ||
} | ||
} | ||
} | ||
} | ||
override fun shouldPrepopulateDatabase(databaseName: String): Boolean = | ||
!context.getDatabasePath(databaseName).exists() | ||
|
||
@SuppressLint("ResourceType") | ||
private fun getDefaultCategoryList() = | ||
listOf( | ||
Category( | ||
category_id = 0, | ||
category_name = context.getString(R.string.category_default_personal), | ||
category_color = context.resources.getString(R.color.blue), | ||
), | ||
Category( | ||
category_id = 0, | ||
category_name = context.getString(R.string.category_default_work), | ||
category_color = context.getString(R.color.green), | ||
), | ||
Category( | ||
category_id = 0, | ||
category_name = context.getString(R.string.category_default_shopping), | ||
category_color = context.getString(R.color.orange), | ||
), | ||
) | ||
override fun getPrepopulateData(): List<Category> = listOf( | ||
Category( | ||
category_id = 0, | ||
category_name = MR.strings.category_default_personal.desc().toString(context), | ||
category_color = context.getString(MR.colors.blue.resourceId), | ||
), | ||
Category( | ||
category_id = 0, | ||
category_name = MR.strings.category_default_work.desc().toString(context), | ||
category_color = context.getString(MR.colors.green.resourceId), | ||
), | ||
Category( | ||
category_id = 0, | ||
category_name = MR.strings.category_default_shopping.desc().toString(context), | ||
category_color = context.getString(MR.colors.orange.resourceId), | ||
), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 28 additions & 4 deletions
32
data/local/src/commonMain/kotlin/com/escodro/local/provider/DriverFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,35 @@ | ||
package com.escodro.local.provider | ||
|
||
import app.cash.sqldelight.db.SqlDriver | ||
import com.escodro.local.AlkaaDatabase | ||
import com.escodro.local.Category | ||
|
||
interface DriverFactory { | ||
/** | ||
* Provides the platform-specific [SqlDriver] to be used in the database. | ||
*/ | ||
internal interface DriverFactory { | ||
|
||
/** | ||
* Creates the platform-specific [SqlDriver] to be used in the database. | ||
* | ||
* @param databaseName the database name | ||
* | ||
* @return the [SqlDriver] to be used in the database | ||
*/ | ||
fun createDriver(databaseName: String): SqlDriver | ||
|
||
// TODO better document here | ||
fun prepopulateDatabase(database: AlkaaDatabase, databaseName: String) | ||
/** | ||
* Checks if the database is opening for the first time and should be prepopulated. | ||
* | ||
* @param databaseName the database name | ||
* | ||
* @return true if the database should be prepopulated, false otherwise | ||
*/ | ||
fun shouldPrepopulateDatabase(databaseName: String): Boolean | ||
|
||
/** | ||
* Gets the prepopulate data to be inserted in the database. | ||
* | ||
* @return the prepopulate data | ||
*/ | ||
fun getPrepopulateData(): List<Category> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters