From 9e98340fd2f8d2b0975525bb8a8075772d19f74a Mon Sep 17 00:00:00 2001 From: Gary Wang Date: Tue, 16 Nov 2021 00:16:36 +0800 Subject: [PATCH] prepare room for servers --- README.md | 6 ++++ app/build.gradle | 17 ++++++++- app/src/main/AndroidManifest.xml | 1 + .../java/net/blumia/pcmdroid/MainActivity.kt | 4 ++- .../net/blumia/pcmdroid/MainApplication.kt | 12 +++++++ .../java/net/blumia/pcmdroid/MainViewModel.kt | 36 +++++++++---------- .../pcmdroid/databases/PcmRoomDatabase.kt | 35 ++++++++++++++++++ .../java/net/blumia/pcmdroid/model/Server.kt | 20 +++++++++++ .../pcmdroid/repository/ServerRepository.kt | 22 +++++++++++- build.gradle | 3 +- 10 files changed, 132 insertions(+), 24 deletions(-) create mode 100644 README.md create mode 100644 app/src/main/java/net/blumia/pcmdroid/MainApplication.kt create mode 100644 app/src/main/java/net/blumia/pcmdroid/databases/PcmRoomDatabase.kt diff --git a/README.md b/README.md new file mode 100644 index 0000000..5dff559 --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ +TODO: + +- [ ] Add server +- [ ] Music playback +- [ ] Download music to local folder +- [ ] Back key to navigate folder up \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 0fed5ec..1b145d7 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -3,6 +3,8 @@ plugins { id 'kotlin-android' } +apply plugin: 'kotlin-kapt' + android { compileSdk 31 @@ -51,16 +53,29 @@ dependencies { implementation 'androidx.core:core-ktx:1.7.0' implementation 'androidx.appcompat:appcompat:1.3.1' implementation 'com.google.android.material:material:1.4.0' + implementation "androidx.compose.ui:ui:$compose_version" implementation "androidx.compose.runtime:runtime-livedata:$compose_version" implementation "androidx.compose.material:material:$compose_version" implementation "androidx.compose.material:material-icons-extended:$compose_version" implementation "androidx.compose.ui:ui-tooling-preview:$compose_version" + + def nav_compose_version = '2.4.0-beta02' implementation "androidx.navigation:navigation-compose:$nav_compose_version" - implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.0' + + def lifecycle_version = '2.4.0' + implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version" + implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version" + implementation 'androidx.activity:activity-compose:1.4.0' implementation "androidx.datastore:datastore-preferences:1.0.0" + def room_version = '2.3.0' + kapt "androidx.room:room-compiler:$room_version" + implementation "androidx.room:room-ktx:$room_version" + implementation "androidx.room:room-runtime:$room_version" + annotationProcessor "androidx.room:room-compiler:$room_version" + implementation("com.squareup.okhttp3:okhttp:4.9.0") testImplementation 'junit:junit:4.+' diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 564250e..fc6adb3 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -5,6 +5,7 @@ > = MutableLiveData>( - listOf( - Server( - url = "https://localhost/api.php", - name = "PCM", - baseFolderName = "pcm", - ), - Server( - url = "https://localhost/pcm.cgi", - name = "Chris", - baseFolderName = "pcm2", - ) - ) - ) + val servers: LiveData> = repository.allServers.asLiveData() + fun addServer(server: Server) = viewModelScope.launch { + repository.insert(server) + } private val _currentServer: MutableLiveData = MutableLiveData(null) val currentServer: LiveData = _currentServer; @@ -139,4 +127,14 @@ class MainViewModel : ViewModel() { } } } +} + +class MainViewModelFactory(private val repository: ServerRepository) : ViewModelProvider.Factory { + override fun create(modelClass: Class): T { + if (modelClass.isAssignableFrom(MainViewModel::class.java)) { + @Suppress("UNCHECKED_CAST") + return MainViewModel(repository) as T + } + throw IllegalArgumentException("Unknown ViewModel class") + } } \ No newline at end of file diff --git a/app/src/main/java/net/blumia/pcmdroid/databases/PcmRoomDatabase.kt b/app/src/main/java/net/blumia/pcmdroid/databases/PcmRoomDatabase.kt new file mode 100644 index 0000000..3454d60 --- /dev/null +++ b/app/src/main/java/net/blumia/pcmdroid/databases/PcmRoomDatabase.kt @@ -0,0 +1,35 @@ +package net.blumia.pcmdroid.databases + +import android.content.Context +import androidx.room.Database +import androidx.room.Room +import androidx.room.RoomDatabase +import net.blumia.pcmdroid.model.Server +import net.blumia.pcmdroid.model.ServerDao + +// Annotates class to be a Room Database with a table (entity) of the Word class +@Database(entities = [Server::class], version = 1, exportSchema = false) +public abstract class PcmRoomDatabase : RoomDatabase() { + abstract fun serverDao(): ServerDao + + companion object { + // Singleton prevents multiple instances of database opening at the same time. + @Volatile + private var INSTANCE: PcmRoomDatabase? = null + + fun getDatabase(context: Context): PcmRoomDatabase { + // if the INSTANCE is not null, then return it, + // if it is, then create the database + return INSTANCE ?: synchronized(this) { + val instance = Room.databaseBuilder( + context.applicationContext, + PcmRoomDatabase::class.java, + "pcm_database" + ).build() + INSTANCE = instance + // return instance + instance + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/net/blumia/pcmdroid/model/Server.kt b/app/src/main/java/net/blumia/pcmdroid/model/Server.kt index beed1ba..58853f4 100644 --- a/app/src/main/java/net/blumia/pcmdroid/model/Server.kt +++ b/app/src/main/java/net/blumia/pcmdroid/model/Server.kt @@ -1,11 +1,31 @@ package net.blumia.pcmdroid.model +import androidx.room.* +import kotlinx.coroutines.flow.Flow + +@Entity(tableName = "server_table") data class Server ( + @PrimaryKey @ColumnInfo(name = "url") val url: String, + @ColumnInfo(name = "name") val name: String, + @ColumnInfo(name = "baseFolderName") val baseFolderName: String, ) { fun displayName(): String { return name } } + +@Dao +interface ServerDao { + + @Query("SELECT * FROM server_table") // ORDER BY wtf ASC + fun getServers(): Flow> + + @Insert(onConflict = OnConflictStrategy.IGNORE) + suspend fun insert(server: Server) + + @Query("DELETE FROM server_table") + suspend fun deleteAll() +} \ No newline at end of file diff --git a/app/src/main/java/net/blumia/pcmdroid/repository/ServerRepository.kt b/app/src/main/java/net/blumia/pcmdroid/repository/ServerRepository.kt index abe395e..b84d6f2 100644 --- a/app/src/main/java/net/blumia/pcmdroid/repository/ServerRepository.kt +++ b/app/src/main/java/net/blumia/pcmdroid/repository/ServerRepository.kt @@ -1,4 +1,24 @@ package net.blumia.pcmdroid.repository -class ServerRepository { +import androidx.annotation.WorkerThread +import kotlinx.coroutines.flow.Flow +import net.blumia.pcmdroid.model.Server +import net.blumia.pcmdroid.model.ServerDao + +// Declares the DAO as a private property in the constructor. Pass in the DAO +// instead of the whole database, because you only need access to the DAO +class ServerRepository(private val serverDao: ServerDao) { + + // Room executes all queries on a separate thread. + // Observed Flow will notify the observer when the data has changed. + val allServers: Flow> = serverDao.getServers() + + // By default Room runs suspend queries off the main thread, therefore, we don't need to + // implement anything else to ensure we're not doing long running database work + // off the main thread. + @Suppress("RedundantSuspendModifier") + @WorkerThread + suspend fun insert(server: Server) { + serverDao.insert(server) + } } \ No newline at end of file diff --git a/build.gradle b/build.gradle index 848f45d..97d47a5 100644 --- a/build.gradle +++ b/build.gradle @@ -1,9 +1,8 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { ext { - compose_version = '1.1.0-beta02' - nav_compose_version = '2.4.0-beta02' kotlin_version = '1.5.31' + compose_version = '1.1.0-beta02' } repositories { google()