From 1f2be8cdee1eea21473d20b27bbdc630ae33cdb5 Mon Sep 17 00:00:00 2001 From: dece Date: Wed, 26 Jan 2022 01:26:29 +0100 Subject: [PATCH] Identities: WIP This commit overwrites the version 1 database schema. Yeah as long as Comet is not out of alpha, I'm not dealing with this horrendous migration process. --- .../1.json | 68 ++++++++++++++++++- .../java/dev/lowrespalmtree/comet/Database.kt | 13 +++- .../java/dev/lowrespalmtree/comet/History.kt | 6 +- .../lowrespalmtree/comet/HistoryFragment.kt | 14 ++-- .../dev/lowrespalmtree/comet/Identities.kt | 38 +++++++++++ .../comet/IdentitiesFragment.kt | 21 ++++++ .../lowrespalmtree/comet/IdentityAdapter.kt | 23 +++++++ .../main/res/layout/fragment_history_list.xml | 2 - .../main/res/layout/fragment_identities.xml | 32 +++++++++ app/src/main/res/layout/fragment_identity.xml | 21 ++++++ app/src/main/res/menu/drawer.xml | 3 + app/src/main/res/navigation/main.xml | 7 ++ app/src/main/res/values/strings.xml | 3 + 13 files changed, 236 insertions(+), 15 deletions(-) create mode 100644 app/src/main/java/dev/lowrespalmtree/comet/Identities.kt create mode 100644 app/src/main/java/dev/lowrespalmtree/comet/IdentitiesFragment.kt create mode 100644 app/src/main/java/dev/lowrespalmtree/comet/IdentityAdapter.kt create mode 100644 app/src/main/res/layout/fragment_identities.xml create mode 100644 app/src/main/res/layout/fragment_identity.xml diff --git a/app/schemas/dev.lowrespalmtree.comet.AppDatabase/1.json b/app/schemas/dev.lowrespalmtree.comet.AppDatabase/1.json index de2e736..07172fb 100644 --- a/app/schemas/dev.lowrespalmtree.comet.AppDatabase/1.json +++ b/app/schemas/dev.lowrespalmtree.comet.AppDatabase/1.json @@ -2,7 +2,7 @@ "formatVersion": 1, "database": { "version": 1, - "identityHash": "44cb07b76e6b8abfaa988e1d3696a347", + "identityHash": "70da3095877de4a82021855471523b90", "entities": [ { "tableName": "HistoryEntry", @@ -35,12 +35,76 @@ }, "indices": [], "foreignKeys": [] + }, + { + "tableName": "Identity", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `key` TEXT NOT NULL, `name` TEXT)", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "key", + "columnName": "key", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "name", + "columnName": "name", + "affinity": "TEXT", + "notNull": false + } + ], + "primaryKey": { + "columnNames": [ + "id" + ], + "autoGenerate": true + }, + "indices": [], + "foreignKeys": [] + }, + { + "tableName": "IdentityUsage", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `uri` TEXT NOT NULL, `identityId` INTEGER NOT NULL)", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "uri", + "columnName": "uri", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "identityId", + "columnName": "identityId", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "columnNames": [ + "id" + ], + "autoGenerate": true + }, + "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, '44cb07b76e6b8abfaa988e1d3696a347')" + "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '70da3095877de4a82021855471523b90')" ] } } \ No newline at end of file diff --git a/app/src/main/java/dev/lowrespalmtree/comet/Database.kt b/app/src/main/java/dev/lowrespalmtree/comet/Database.kt index 48ef946..4b111d4 100644 --- a/app/src/main/java/dev/lowrespalmtree/comet/Database.kt +++ b/app/src/main/java/dev/lowrespalmtree/comet/Database.kt @@ -1,13 +1,22 @@ package dev.lowrespalmtree.comet import android.content.Context +import androidx.room.AutoMigration import androidx.room.Database import androidx.room.Room import androidx.room.RoomDatabase -@Database(entities = [History.HistoryEntry::class], version = 1) +@Database( + entities = [ + History.HistoryEntry::class, + Identities.Identity::class, + Identities.IdentityUsage::class, + ], + version = 1 +) abstract class AppDatabase : RoomDatabase() { abstract fun historyEntryDao(): History.HistoryEntryDao + abstract fun identityDao(): Identities.IdentityDao } object Database { @@ -16,6 +25,6 @@ object Database { fun init(context: Context) { if (::INSTANCE.isInitialized) return - INSTANCE = Room.databaseBuilder(context, AppDatabase::class.java, "comet").build() + INSTANCE = Room.databaseBuilder(context, AppDatabase::class.java, "comet.db").build() } } \ No newline at end of file diff --git a/app/src/main/java/dev/lowrespalmtree/comet/History.kt b/app/src/main/java/dev/lowrespalmtree/comet/History.kt index e6efe23..e916a8e 100644 --- a/app/src/main/java/dev/lowrespalmtree/comet/History.kt +++ b/app/src/main/java/dev/lowrespalmtree/comet/History.kt @@ -5,9 +5,9 @@ import androidx.room.* object History { @Entity data class HistoryEntry( - @PrimaryKey @ColumnInfo(name = "uri") val uri: String, - @ColumnInfo(name = "title") var title: String?, - @ColumnInfo(name = "lastVisit") var lastVisit: Long, + @PrimaryKey val uri: String, + var title: String?, + var lastVisit: Long, ) @Dao diff --git a/app/src/main/java/dev/lowrespalmtree/comet/HistoryFragment.kt b/app/src/main/java/dev/lowrespalmtree/comet/HistoryFragment.kt index a6f955c..274f556 100644 --- a/app/src/main/java/dev/lowrespalmtree/comet/HistoryFragment.kt +++ b/app/src/main/java/dev/lowrespalmtree/comet/HistoryFragment.kt @@ -6,9 +6,10 @@ import android.view.View import android.view.ViewGroup import androidx.core.os.bundleOf import androidx.fragment.app.Fragment +import androidx.fragment.app.viewModels import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.SavedStateHandle import androidx.lifecycle.ViewModel -import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.viewModelScope import androidx.navigation.fragment.findNavController import androidx.recyclerview.widget.LinearLayoutManager @@ -21,8 +22,8 @@ import kotlinx.coroutines.launch @ExperimentalCoroutinesApi class HistoryFragment : Fragment(), HistoryItemAdapterListener { + private val vm: HistoryViewModel by viewModels() private lateinit var binding: FragmentHistoryListBinding - private lateinit var historyViewModel: HistoryViewModel private lateinit var adapter: HistoryItemAdapter override fun onCreateView( @@ -35,14 +36,13 @@ class HistoryFragment : Fragment(), HistoryItemAdapterListener { } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - historyViewModel = ViewModelProvider(this)[HistoryViewModel::class.java] adapter = HistoryItemAdapter(this) binding.list.layoutManager = LinearLayoutManager(requireContext()) binding.list.adapter = adapter - historyViewModel.items.observe(viewLifecycleOwner, { adapter.setItems(it) }) + vm.items.observe(viewLifecycleOwner, { adapter.setItems(it) }) - historyViewModel.refreshHistory() + vm.refreshHistory() } override fun onItemClick(url: String) { @@ -51,7 +51,9 @@ class HistoryFragment : Fragment(), HistoryItemAdapterListener { } @ExperimentalCoroutinesApi - class HistoryViewModel : ViewModel() { + class HistoryViewModel( + @Suppress("unused") private val savedStateHandle: SavedStateHandle + ) : ViewModel() { val items: MutableLiveData> by lazy { MutableLiveData>() } diff --git a/app/src/main/java/dev/lowrespalmtree/comet/Identities.kt b/app/src/main/java/dev/lowrespalmtree/comet/Identities.kt new file mode 100644 index 0000000..c0f21e1 --- /dev/null +++ b/app/src/main/java/dev/lowrespalmtree/comet/Identities.kt @@ -0,0 +1,38 @@ +package dev.lowrespalmtree.comet + +import androidx.room.* + +object Identities { + @Entity + data class Identity( + /** ID. */ + @PrimaryKey(autoGenerate = true) val id: Int, + /** Key to retrieve certificate from the keystore. */ + val key: String, + /** Label for this identity. */ + val name: String?, + ) + + @Entity + data class IdentityUsage( + /** ID. */ + @PrimaryKey(autoGenerate = true) val id: Int, + /** URL path where an identity can be used. */ + val uri: String, + /** ID of the Identity to use. */ + val identityId: Int + ) + + @Dao + interface IdentityDao { + @Insert + suspend fun insert(vararg entries: Identity) + + @Query("SELECT * FROM IdentityUsage WHERE :identityId = identityId") + fun getUsagesFor(identityId: Int): List + } + + suspend fun insert(key: String, name: String? = null) { + Database.INSTANCE.identityDao().insert(Identity(0, key, name)) + } +} \ No newline at end of file diff --git a/app/src/main/java/dev/lowrespalmtree/comet/IdentitiesFragment.kt b/app/src/main/java/dev/lowrespalmtree/comet/IdentitiesFragment.kt new file mode 100644 index 0000000..13e8418 --- /dev/null +++ b/app/src/main/java/dev/lowrespalmtree/comet/IdentitiesFragment.kt @@ -0,0 +1,21 @@ +package dev.lowrespalmtree.comet + +import android.os.Bundle +import androidx.fragment.app.Fragment +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import dev.lowrespalmtree.comet.databinding.FragmentIdentitiesBinding + +class IdentitiesFragment : Fragment() { + private lateinit var binding: FragmentIdentitiesBinding + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + binding = FragmentIdentitiesBinding.inflate(layoutInflater) + return binding.root + } +} \ No newline at end of file diff --git a/app/src/main/java/dev/lowrespalmtree/comet/IdentityAdapter.kt b/app/src/main/java/dev/lowrespalmtree/comet/IdentityAdapter.kt new file mode 100644 index 0000000..398a3f4 --- /dev/null +++ b/app/src/main/java/dev/lowrespalmtree/comet/IdentityAdapter.kt @@ -0,0 +1,23 @@ +package dev.lowrespalmtree.comet + +import android.view.ViewGroup +import androidx.recyclerview.widget.RecyclerView +import dev.lowrespalmtree.comet.databinding.FragmentIdentityBinding + +class IdentityAdapter : RecyclerView.Adapter() { + private var identities = listOf() + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HistoryItemAdapter.ViewHolder { + TODO("Not yet implemented") + } + + override fun onBindViewHolder(holder: HistoryItemAdapter.ViewHolder, position: Int) { + TODO("Not yet implemented") + } + + override fun getItemCount(): Int { + TODO("Not yet implemented") + } + + inner class ViewHolder(val binding: FragmentIdentityBinding) : RecyclerView.ViewHolder(binding.root) +} \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_history_list.xml b/app/src/main/res/layout/fragment_history_list.xml index 5189eab..321e303 100644 --- a/app/src/main/res/layout/fragment_history_list.xml +++ b/app/src/main/res/layout/fragment_history_list.xml @@ -1,6 +1,5 @@ \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_identities.xml b/app/src/main/res/layout/fragment_identities.xml new file mode 100644 index 0000000..1161c16 --- /dev/null +++ b/app/src/main/res/layout/fragment_identities.xml @@ -0,0 +1,32 @@ + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_identity.xml b/app/src/main/res/layout/fragment_identity.xml new file mode 100644 index 0000000..44ed6f8 --- /dev/null +++ b/app/src/main/res/layout/fragment_identity.xml @@ -0,0 +1,21 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/drawer.xml b/app/src/main/res/menu/drawer.xml index d2cf40b..5c29004 100644 --- a/app/src/main/res/menu/drawer.xml +++ b/app/src/main/res/menu/drawer.xml @@ -9,6 +9,9 @@ android:id="@+id/historyFragment" android:icon="@android:drawable/ic_menu_recent_history" android:title="@string/history" /> + + + + + Only download attachments when manually requested Home + + Hello blank fragment + Identities \ No newline at end of file