Skip to content

Commit

Permalink
bookmarking languages (closes #75)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Nov 21, 2022
1 parent 3acee86 commit 20b83de
Show file tree
Hide file tree
Showing 22 changed files with 204 additions and 41 deletions.
96 changes: 96 additions & 0 deletions app/schemas/com.bnyro.translate.db.AppDatabase/2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
{
"formatVersion": 1,
"database": {
"version": 2,
"identityHash": "5dd91a2a8eee2a5ed8050f69dafb44e8",
"entities": [
{
"tableName": "HistoryItem",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `sourceLanguageName` TEXT NOT NULL, `sourceLanguageCode` TEXT NOT NULL, `targetLanguageName` TEXT NOT NULL, `targetLanguageCode` TEXT NOT NULL, `insertedText` TEXT NOT NULL, `translatedText` TEXT NOT NULL)",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "sourceLanguageName",
"columnName": "sourceLanguageName",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "sourceLanguageCode",
"columnName": "sourceLanguageCode",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "targetLanguageName",
"columnName": "targetLanguageName",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "targetLanguageCode",
"columnName": "targetLanguageCode",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "insertedText",
"columnName": "insertedText",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "translatedText",
"columnName": "translatedText",
"affinity": "TEXT",
"notNull": true
}
],
"primaryKey": {
"columnNames": [
"id"
],
"autoGenerate": true
},
"indices": [],
"foreignKeys": []
},
{
"tableName": "Language",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`code` TEXT NOT NULL, `name` TEXT NOT NULL, PRIMARY KEY(`code`))",
"fields": [
{
"fieldPath": "code",
"columnName": "code",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "name",
"columnName": "name",
"affinity": "TEXT",
"notNull": true
}
],
"primaryKey": {
"columnNames": [
"code"
],
"autoGenerate": false
},
"indices": [],
"foreignKeys": []
}
],
"views": [],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '5dd91a2a8eee2a5ed8050f69dafb44e8')"
]
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.bnyro.translate.api.deepl

import com.bnyro.translate.const.ApiKeyState
import com.bnyro.translate.obj.Language
import com.bnyro.translate.db.obj.Language
import com.bnyro.translate.obj.Translation
import com.bnyro.translate.util.RetrofitHelper
import com.bnyro.translate.util.TranslationEngine
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/bnyro/translate/api/lt/LTEngine.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.bnyro.translate.api.lt

import com.bnyro.translate.const.ApiKeyState
import com.bnyro.translate.obj.Language
import com.bnyro.translate.db.obj.Language
import com.bnyro.translate.obj.Translation
import com.bnyro.translate.util.RetrofitHelper
import com.bnyro.translate.util.TranslationEngine
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.bnyro.translate.api.lt

import com.bnyro.translate.api.lt.obj.LTTranslation
import com.bnyro.translate.obj.Language
import com.bnyro.translate.db.obj.Language
import retrofit2.http.GET
import retrofit2.http.POST
import retrofit2.http.Query
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/bnyro/translate/api/lv/LVEngine.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.bnyro.translate.api.lv

import com.bnyro.translate.const.ApiKeyState
import com.bnyro.translate.db.obj.Language
import com.bnyro.translate.ext.expOrNull
import com.bnyro.translate.obj.Definition
import com.bnyro.translate.obj.Language
import com.bnyro.translate.obj.Translation
import com.bnyro.translate.util.RetrofitHelper
import com.bnyro.translate.util.TranslationEngine
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.bnyro.translate.api.lv.obj

import com.bnyro.translate.obj.Language
import com.bnyro.translate.db.obj.Language

data class LvLanguage(
val languages: List<Language> = emptyList()
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/bnyro/translate/api/mm/MMEngine.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.bnyro.translate.api.mm

import com.bnyro.translate.const.ApiKeyState
import com.bnyro.translate.obj.Language
import com.bnyro.translate.db.obj.Language
import com.bnyro.translate.obj.Translation
import com.bnyro.translate.util.RetrofitHelper
import com.bnyro.translate.util.TranslationEngine
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.bnyro.translate.api.reverso

import com.bnyro.translate.api.reverso.obj.ReversoRequestBody
import com.bnyro.translate.const.ApiKeyState
import com.bnyro.translate.obj.Language
import com.bnyro.translate.db.obj.Language
import com.bnyro.translate.obj.Translation
import com.bnyro.translate.util.RetrofitHelper
import com.bnyro.translate.util.TranslationEngine
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/bnyro/translate/api/st/STEngine.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.bnyro.translate.api.st

import com.bnyro.translate.const.ApiKeyState
import com.bnyro.translate.obj.Language
import com.bnyro.translate.db.obj.Language
import com.bnyro.translate.obj.Translation
import com.bnyro.translate.util.Preferences
import com.bnyro.translate.util.RetrofitHelper
Expand Down
13 changes: 11 additions & 2 deletions app/src/main/java/com/bnyro/translate/db/AppDatabase.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
package com.bnyro.translate.db

import androidx.room.AutoMigration
import androidx.room.Database
import androidx.room.RoomDatabase
import com.bnyro.translate.db.dao.HistoryDao
import com.bnyro.translate.db.dao.LanguageBookmarksDao
import com.bnyro.translate.db.obj.HistoryItem
import com.bnyro.translate.db.obj.Language

@Database(
entities = [
HistoryItem::class
HistoryItem::class,
Language::class
],
version = 1
version = 2,
autoMigrations = [
AutoMigration(from = 1, to = 2)
]
)
abstract class AppDatabase : RoomDatabase() {
abstract fun historyDao(): HistoryDao

abstract fun languageBookmarksDao(): LanguageBookmarksDao
}
4 changes: 2 additions & 2 deletions app/src/main/java/com/bnyro/translate/db/dao/HistoryDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ interface HistoryDao {
fun getAll(): List<HistoryItem>

@Insert
fun insertAll(vararg HistoryItems: HistoryItem)
fun insertAll(vararg historyItems: HistoryItem)

@Delete
fun delete(HistoryItem: HistoryItem)
fun delete(historyItem: HistoryItem)

@Query("DELETE FROM HistoryItem")
fun deleteAll()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.bnyro.translate.db.dao

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.Query
import com.bnyro.translate.db.obj.Language

@Dao
interface LanguageBookmarksDao {
@Query("SELECT * FROM Language")
fun getAll(): List<Language>

@Insert
fun insertAll(vararg languages: Language)

@Delete
fun delete(language: Language)

@Query("DELETE FROM Language")
fun deleteAll()
}
12 changes: 12 additions & 0 deletions app/src/main/java/com/bnyro/translate/db/obj/Language.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.bnyro.translate.db.obj

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity
data class Language(
@PrimaryKey(autoGenerate = false)
val code: String = "",
@ColumnInfo val name: String = ""
)
12 changes: 12 additions & 0 deletions app/src/main/java/com/bnyro/translate/ext/AwaitQuery.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.bnyro.translate.ext

fun <T> awaitQuery(action: () -> T): T {
var value: T? = null
Thread {
value = action.invoke()
}.apply {
start()
join()
}
return value!!
}
2 changes: 1 addition & 1 deletion app/src/main/java/com/bnyro/translate/ext/Query.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.bnyro.translate.ext

fun Query(dbQuery: () -> Unit) {
fun query(dbQuery: () -> Unit) {
Thread {
dbQuery.invoke()
}.start()
Expand Down
6 changes: 0 additions & 6 deletions app/src/main/java/com/bnyro/translate/obj/Language.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ class MainActivity : BaseActivity() {
Toast.LENGTH_LONG
).show()
}

mainModel.fetchBookmarkedLanguages()
}

override fun onNewIntent(intent: Intent?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.bnyro.translate.obj.Language
import com.bnyro.translate.db.obj.Language

@Composable
fun LanguageItem(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.bnyro.translate.DatabaseHolder
import com.bnyro.translate.R
import com.bnyro.translate.obj.Language
import com.bnyro.translate.db.obj.Language
import com.bnyro.translate.ext.query
import com.bnyro.translate.ui.models.MainModel

@Composable
Expand Down Expand Up @@ -57,10 +59,6 @@ fun LanguageSelector(
)
}

var favoriteLanguages by remember {
mutableStateOf(listOf<Language>())
}

if (showDialog) {
AlertDialog(
onDismissRequest = {
Expand All @@ -70,25 +68,28 @@ fun LanguageSelector(
LazyColumn(
horizontalAlignment = Alignment.CenterHorizontally
) {
items(favoriteLanguages) {
items(viewModel.bookmarkedLanguages) {
LanguageItem(
language = it,
isPinned = favoriteLanguages.contains(it),
isPinned = viewModel.bookmarkedLanguages.contains(it),
onClick = {
showDialog = false
viewModel.enqueueTranslation()
onClick.invoke(it)
},
onPinnedChange = {
favoriteLanguages = favoriteLanguages.filter { language ->
viewModel.bookmarkedLanguages = viewModel.bookmarkedLanguages.filter { language ->
it != language
}
query {
DatabaseHolder.Db.languageBookmarksDao().delete(it)
}
}
)
}

item {
if (favoriteLanguages.isNotEmpty()) {
if (viewModel.bookmarkedLanguages.isNotEmpty()) {
Divider(
color = MaterialTheme.colorScheme.onSurface,
modifier = Modifier
Expand All @@ -101,17 +102,23 @@ fun LanguageSelector(
items(languages) {
LanguageItem(
language = it,
isPinned = favoriteLanguages.contains(it),
isPinned = viewModel.bookmarkedLanguages.contains(it),
onClick = {
showDialog = false
viewModel.enqueueTranslation()
onClick.invoke(it)
},
onPinnedChange = {
favoriteLanguages = if (favoriteLanguages.contains(it)) {
favoriteLanguages - it
viewModel.bookmarkedLanguages = if (viewModel.bookmarkedLanguages.contains(it)) {
query {
DatabaseHolder.Db.languageBookmarksDao().delete(it)
}
viewModel.bookmarkedLanguages - it
} else {
favoriteLanguages + it
query {
DatabaseHolder.Db.languageBookmarksDao().insertAll(it)
}
viewModel.bookmarkedLanguages + it
}
}
)
Expand Down
Loading

0 comments on commit 20b83de

Please sign in to comment.