Skip to content
Merged
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
176 changes: 176 additions & 0 deletions app/schemas/com.philkes.notallyx.data.NotallyDatabase/11.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
{
"formatVersion": 1,
"database": {
"version": 11,
"identityHash": "80a04d33cf13bc8ca45396f5a5d85e61",
"entities": [
{
"tableName": "BaseNote",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `type` TEXT NOT NULL, `folder` TEXT NOT NULL, `color` TEXT NOT NULL, `title` TEXT NOT NULL, `pinned` INTEGER NOT NULL, `timestamp` INTEGER NOT NULL, `modifiedTimestamp` INTEGER NOT NULL, `labels` TEXT NOT NULL, `body` TEXT NOT NULL, `spans` TEXT NOT NULL, `items` TEXT NOT NULL, `images` TEXT NOT NULL, `files` TEXT NOT NULL, `audios` TEXT NOT NULL, `reminders` TEXT NOT NULL, `viewMode` TEXT NOT NULL, `isPinnedToStatus` INTEGER NOT NULL)",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "type",
"columnName": "type",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "folder",
"columnName": "folder",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "color",
"columnName": "color",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "title",
"columnName": "title",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "pinned",
"columnName": "pinned",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "timestamp",
"columnName": "timestamp",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "modifiedTimestamp",
"columnName": "modifiedTimestamp",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "labels",
"columnName": "labels",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "body",
"columnName": "body",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "spans",
"columnName": "spans",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "items",
"columnName": "items",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "images",
"columnName": "images",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "files",
"columnName": "files",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "audios",
"columnName": "audios",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "reminders",
"columnName": "reminders",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "viewMode",
"columnName": "viewMode",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "isPinnedToStatus",
"columnName": "isPinnedToStatus",
"affinity": "INTEGER",
"notNull": true
}
],
"primaryKey": {
"autoGenerate": true,
"columnNames": [
"id"
]
},
"indices": [
{
"name": "index_BaseNote_id_folder_pinned_timestamp_labels",
"unique": false,
"columnNames": [
"id",
"folder",
"pinned",
"timestamp",
"labels"
],
"orders": [],
"createSql": "CREATE INDEX IF NOT EXISTS `index_BaseNote_id_folder_pinned_timestamp_labels` ON `${TABLE_NAME}` (`id`, `folder`, `pinned`, `timestamp`, `labels`)"
}
],
"foreignKeys": []
},
{
"tableName": "Label",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`value` TEXT NOT NULL, `order` INTEGER NOT NULL, PRIMARY KEY(`value`))",
"fields": [
{
"fieldPath": "value",
"columnName": "value",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "order",
"columnName": "order",
"affinity": "INTEGER",
"notNull": true
}
],
"primaryKey": {
"autoGenerate": false,
"columnNames": [
"value"
]
},
"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, '80a04d33cf13bc8ca45396f5a5d85e61')"
]
}
}
21 changes: 20 additions & 1 deletion app/src/main/java/com/philkes/notallyx/data/NotallyDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import java.io.File
import net.zetetic.database.sqlcipher.SupportOpenHelperFactory

@TypeConverters(Converters::class)
@Database(entities = [BaseNote::class, Label::class], version = 10)
@Database(entities = [BaseNote::class, Label::class], version = 11)
abstract class NotallyDatabase : RoomDatabase() {

abstract fun getLabelDao(): LabelDao
Expand Down Expand Up @@ -162,6 +162,7 @@ abstract class NotallyDatabase : RoomDatabase() {
Migration8,
Migration9,
Migration10,
Migration11
)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
System.loadLibrary("sqlcipher")
Expand Down Expand Up @@ -310,5 +311,23 @@ abstract class NotallyDatabase : RoomDatabase() {
)
}
}

object Migration11 : Migration(10, 11) {

override fun migrate(db: SupportSQLiteDatabase) {
db.execSQL("ALTER TABLE `Label` ADD COLUMN `order` INTEGER NOT NULL DEFAULT 0")
val cursor = db.query("SELECT value FROM Label ORDER BY value DESC")
var order = 0
while (cursor.moveToNext()) {
val value = cursor.getString(0)
db.execSQL(
"UPDATE Label SET `order` = ? WHERE value = ?",
arrayOf(order, value),
)
order++
}
Comment thread
Crustack marked this conversation as resolved.
cursor.close()
}
}
}
}
12 changes: 10 additions & 2 deletions app/src/main/java/com/philkes/notallyx/data/dao/CommonDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ abstract class CommonDao(private val database: NotallyDatabase) {
duplicates++
}
}
database.getLabelDao().insert(labels)
val labelDao = database.getLabelDao()
val maxOrder = labelDao.getMaxOrder() ?: -1
labelDao.insert(
labels.mapIndexed { index, label -> Label(label.value, maxOrder + 1 + index) }
)
return ImportResult(inserted = insertedCount, duplicates = duplicates)
}

Expand Down Expand Up @@ -134,7 +138,11 @@ abstract class CommonDao(private val database: NotallyDatabase) {
}
}

database.getLabelDao().insert(labels)
val labelDaoForRemap = database.getLabelDao()
val maxOrderForRemap = labelDaoForRemap.getMaxOrder() ?: -1
labelDaoForRemap.insert(
labels.mapIndexed { index, label -> Label(label.value, maxOrderForRemap + 1 + index) }
)
return ImportResult(inserted = insertedCount, duplicates = duplicates)
}

Expand Down
15 changes: 12 additions & 3 deletions app/src/main/java/com/philkes/notallyx/data/dao/LabelDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,35 @@ import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import com.philkes.notallyx.data.model.Label

@Dao
interface LabelDao {

@Insert suspend fun insert(label: Label)
@Insert(onConflict = OnConflictStrategy.IGNORE) suspend fun insert(label: Label)

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

@Update suspend fun update(label: Label)

@Update suspend fun update(labels: List<Label>)

@Query("DELETE FROM Label WHERE value = :value") suspend fun delete(value: String)

@Query("DELETE FROM Label") suspend fun deleteAll()

@Query("UPDATE Label SET value = :newValue WHERE value = :oldValue")
suspend fun update(oldValue: String, newValue: String)

@Query("SELECT value FROM Label ORDER BY value") fun getAll(): LiveData<List<String>>
@Query("SELECT * FROM Label ORDER BY `order` DESC, value ASC")
fun getAll(): LiveData<List<Label>>

@Query("SELECT value FROM Label ORDER BY value") suspend fun getArrayOfAll(): Array<String>
@Query("SELECT value FROM Label ORDER BY `order` DESC, value ASC")
suspend fun getArrayOfAll(): Array<String>

@Query("SELECT EXISTS(SELECT 1 FROM Label WHERE value = :value)")
suspend fun exists(value: String): Boolean

@Query("SELECT MAX(`order`) FROM Label") suspend fun getMaxOrder(): Int?
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,15 @@ class NotesImporter(private val app: Application, private val database: NotallyD
progress?.postValue(ImportProgress(inProgress = false))
throw e
}
database.getLabelDao().insert(notes.flatMap { it.labels }.distinct().map { Label(it) })
val labelDao = database.getLabelDao()
val maxOrder = labelDao.getMaxOrder() ?: -1
labelDao.insert(
notes
.flatMap { it.labels }
.distinct()
.sorted()
.mapIndexed { index, value -> Label(value, maxOrder + 1 + index) }
)
val files = notes.flatMap { it.files }.distinct()
val images = notes.flatMap { it.images }.distinct()
val audios = notes.flatMap { it.audios }.distinct()
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/philkes/notallyx/data/model/Label.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ package com.philkes.notallyx.data.model
import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity class Label(@PrimaryKey val value: String)
@Entity class Label(@PrimaryKey val value: String, val order: Int)
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import com.google.android.material.transition.platform.MaterialFade
import com.philkes.notallyx.R
import com.philkes.notallyx.data.NotallyDatabase
import com.philkes.notallyx.data.model.BaseNote
import com.philkes.notallyx.data.model.Folder
import com.philkes.notallyx.data.model.Label
import com.philkes.notallyx.databinding.ActivityMainBinding
import com.philkes.notallyx.presentation.activity.LockedActivity
import com.philkes.notallyx.presentation.activity.main.fragment.DisplayLabelFragment.Companion.EXTRA_DISPLAYED_LABEL
Expand Down Expand Up @@ -268,8 +270,8 @@ class MainActivity : LockedActivity<ActivityMainBinding>() {

private var labelsMenuItems: List<MenuItem> = listOf()
private var labelsMoreMenuItem: MenuItem? = null
private var labels: List<String> = listOf()
private var labelsLiveData: LiveData<List<String>>? = null
private var labels: List<Label> = listOf()
private var labelsLiveData: LiveData<List<Label>>? = null

private fun setupMenu() {
binding.NavigationView.menu.apply {
Expand Down Expand Up @@ -318,19 +320,19 @@ class MainActivity : LockedActivity<ActivityMainBinding>() {
.setIcon(R.drawable.label_more)
}

private fun Menu.setupLabelsMenuItems(labels: List<String>, maxLabelsToDisplay: Int) {
private fun Menu.setupLabelsMenuItems(labels: List<Label>, maxLabelsToDisplay: Int) {
removeGroup(1)
addStaticLabelsMenuItems()
labelsMenuItems =
labels
.mapIndexed { index, label ->
add(1, R.id.DisplayLabel, CATEGORY_CONTAINER + index + 3, label)
add(1, R.id.DisplayLabel, CATEGORY_CONTAINER + index + 3, label.value)
.setCheckable(true)
.setChecked(baseModel.currentLabel == label)
.setChecked(baseModel.currentLabel == label.value)
.setVisible(index < maxLabelsToDisplay)
.setIcon(R.drawable.label)
.setOnMenuItemClickListener {
navigateToLabel(label)
navigateToLabel(label.value)
false
}
}
Expand Down
Loading