Skip to content

Commit

Permalink
Merge pull request #7 from teogor/refactor/improve-code-readability
Browse files Browse the repository at this point in the history
Optimized database access logic for efficiency
  • Loading branch information
teogor authored Feb 15, 2024
2 parents 8074bc5 + 7cd9040 commit 19783d2
Show file tree
Hide file tree
Showing 35 changed files with 1,077 additions and 1,040 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ import org.junit.runner.RunWith
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("dev.teogor.stitch", appContext.packageName)
}
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("dev.teogor.stitch", appContext.packageName)
}
}
35 changes: 19 additions & 16 deletions app/src/main/kotlin/dev/teogor/stitch/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,34 @@ import androidx.compose.ui.tooling.preview.Preview
import dev.teogor.stitch.ui.theme.StitchTheme

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
StitchTheme {
// A surface container using the 'background' color from the theme
Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) {
Greeting("Android")
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
StitchTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background,
) {
Greeting("Android")
}
}
}
}
}

@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
Text(
text = "Hello $name!",
modifier = modifier,
)
Text(
text = "Hello $name!",
modifier = modifier,
)
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
StitchTheme {
Greeting("Android")
}
StitchTheme {
Greeting("Android")
}
}
50 changes: 25 additions & 25 deletions app/src/main/kotlin/dev/teogor/stitch/core/database/AppDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,45 +21,45 @@ import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import dev.teogor.stitch.core.database.util.Converters
import dev.teogor.stitch.core.database.util.DurationConverter
import dev.teogor.stitch.core.database.util.ZonedDateTimeConverter
import dev.teogor.stitch.core.database.dao.SavedGameDao
import dev.teogor.stitch.core.database.dao.TestingKindDao
import dev.teogor.stitch.core.database.model.SavedGame
import dev.teogor.stitch.core.database.model.TestingKind
import dev.teogor.stitch.core.database.util.Converters
import dev.teogor.stitch.core.database.util.DurationConverter
import dev.teogor.stitch.core.database.util.ZonedDateTimeConverter

@Database(
entities = [
TestingKind::class,
SavedGame::class,
],
version = 1,
entities = [
TestingKind::class,
SavedGame::class,
],
version = 1,
)
@TypeConverters(
Converters::class,
DurationConverter::class,
ZonedDateTimeConverter::class,
Converters::class,
DurationConverter::class,
ZonedDateTimeConverter::class,
)
abstract class AppDatabase : RoomDatabase() {

abstract fun testingKindDao(): TestingKindDao
abstract fun testingKindDao(): TestingKindDao

abstract fun savedGameDao(): SavedGameDao
abstract fun savedGameDao(): SavedGameDao

companion object {
private var INSTANCE: AppDatabase? = null
companion object {
private var INSTANCE: AppDatabase? = null

fun getInstance(context: Context): AppDatabase {
if (INSTANCE == null) {
INSTANCE = Room.databaseBuilder(
context,
AppDatabase::class.java,
"main_database",
).build()
}
fun getInstance(context: Context): AppDatabase {
if (INSTANCE == null) {
INSTANCE = Room.databaseBuilder(
context,
AppDatabase::class.java,
"main_database",
).build()
}

return INSTANCE as AppDatabase
}
return INSTANCE as AppDatabase
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,29 @@ import kotlinx.coroutines.flow.Flow

@Dao
interface SavedGameDao {
@Query("SELECT * FROM saved_games")
fun getAll(): Flow<List<SavedGame>>
@Query("SELECT * FROM saved_games")
fun getAll(): Flow<List<SavedGame>>

@Query("SELECT * FROM saved_games WHERE board_id == :id")
suspend fun get(id: Long): SavedGame?
@Query("SELECT * FROM saved_games WHERE board_id == :id")
suspend fun get(id: Long): SavedGame?

@Query(
"SELECT * FROM saved_games " +
"WHERE completed == 'false' " +
"ORDER BY board_id DESC " +
"LIMIT 1",
)
fun getLast(): Flow<SavedGame?>
@Query(
"SELECT * FROM saved_games " +
"WHERE completed == 'false' " +
"ORDER BY board_id DESC " +
"LIMIT 1",
)
fun getLast(): Flow<SavedGame?>

@Update(onConflict = OnConflictStrategy.REPLACE)
suspend fun update(savedGame: SavedGame)
@Update(onConflict = OnConflictStrategy.REPLACE)
suspend fun update(savedGame: SavedGame)

@Delete
suspend fun delete(savedGame: SavedGame)
@Delete
suspend fun delete(savedGame: SavedGame)

@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun insert(savedGames: List<SavedGame>)
@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun insert(savedGames: List<SavedGame>)

@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun insert(savedGame: SavedGame): Long
@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun insert(savedGame: SavedGame): Long
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,26 @@ import kotlinx.coroutines.flow.Flow

@Dao
interface TestingKindDao {
@Query("SELECT * FROM testing_kind")
fun getAll(): Flow<List<SavedGame>>
@Query("SELECT * FROM testing_kind")
fun getAll(): Flow<List<SavedGame>>

@Query("SELECT * FROM testing_kind WHERE board_id == :id")
suspend fun get(id: Long): SavedGame?
@Query("SELECT * FROM testing_kind WHERE board_id == :id")
suspend fun get(id: Long): SavedGame?

@Query(
"SELECT * FROM testing_kind " +
"WHERE completed == 'false' " +
"ORDER BY board_id DESC " +
"LIMIT 1",
)
fun getLast(): Flow<SavedGame?>
@Query(
"SELECT * FROM testing_kind " +
"WHERE completed == 'false' " +
"ORDER BY board_id DESC " +
"LIMIT 1",
)
fun getLast(): Flow<SavedGame?>

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insert(savedGame: SavedGame): Long
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insert(savedGame: SavedGame): Long

@Update(onConflict = OnConflictStrategy.REPLACE)
suspend fun update(savedGame: SavedGame)
@Update(onConflict = OnConflictStrategy.REPLACE)
suspend fun update(savedGame: SavedGame)

@Delete
suspend fun delete(savedGame: SavedGame)
@Delete
suspend fun delete(savedGame: SavedGame)
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,28 @@ package dev.teogor.stitch.core.database.model
* | VERY_HARD | 0-10 | 10-20 | No | These puzzles are the most difficult to solve and are intended only for Sudoku experts, requiring extreme patience and logical reasoning. |
*/
enum class Difficulty {
/**
* Indicates an easy Sudoku puzzle with a large number of clues.
*/
VERY_EASY,
/**
* Indicates an easy Sudoku puzzle with a large number of clues.
*/
VERY_EASY,

/**
* Indicates an easy Sudoku puzzle with fewer clues than VERY_EASY puzzles.
*/
EASY,
/**
* Indicates an easy Sudoku puzzle with fewer clues than VERY_EASY puzzles.
*/
EASY,

/**
* Indicates a Sudoku puzzle of moderate difficulty with a moderate number of clues.
*/
MEDIUM,
/**
* Indicates a Sudoku puzzle of moderate difficulty with a moderate number of clues.
*/
MEDIUM,

/**
* Indicates a challenging Sudoku puzzle with few clues and intricate arrangements.
*/
HARD,
/**
* Indicates a challenging Sudoku puzzle with few clues and intricate arrangements.
*/
HARD,

/**
* Indicates an extremely challenging Sudoku puzzle with almost no clues and highly complex arrangements.
*/
VERY_HARD,
/**
* Indicates an extremely challenging Sudoku puzzle with almost no clues and highly complex arrangements.
*/
VERY_HARD,
}
Loading

0 comments on commit 19783d2

Please sign in to comment.