From 13685974fd0ca05c599463080e2f70def1d6fc5e Mon Sep 17 00:00:00 2001 From: kosharskiy Date: Thu, 14 Jan 2021 10:46:29 +0200 Subject: [PATCH] implemented server edit; converted ServerRepository and ServerDao to kotlin --- app/build.gradle | 4 +- .../activity/ServerAddressBookActivity.kt | 11 ++- .../peertube/adapter/ServerListAdapter.kt | 3 +- .../database/{ServerDao.java => ServerDao.kt} | 29 +++---- .../peertube/database/ServerRepository.java | 79 ------------------- .../peertube/database/ServerRepository.kt | 48 +++++++++++ .../peertube/database/ServerViewModel.java | 44 ----------- .../peertube/database/ServerViewModel.kt | 47 +++++++++++ .../peertube/fragment/AddServerFragment.kt | 12 +++ 9 files changed, 134 insertions(+), 143 deletions(-) rename app/src/main/java/net/schueller/peertube/database/{ServerDao.java => ServerDao.kt} (60%) delete mode 100644 app/src/main/java/net/schueller/peertube/database/ServerRepository.java create mode 100644 app/src/main/java/net/schueller/peertube/database/ServerRepository.kt delete mode 100644 app/src/main/java/net/schueller/peertube/database/ServerViewModel.java create mode 100644 app/src/main/java/net/schueller/peertube/database/ServerViewModel.kt diff --git a/app/build.gradle b/app/build.gradle index ab33d68..ef4901c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -148,9 +148,8 @@ dependencies { // database lib implementation "androidx.room:room-runtime:$room_version" - implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0' + implementation "androidx.room:room-ktx:$room_version" kapt "androidx.room:room-compiler:$room_version" - androidTestImplementation "androidx.room:room-testing:$room_version" // Lifecycle components implementation "androidx.lifecycle:lifecycle-extensions:$lifecycleVersion" @@ -166,6 +165,7 @@ dependencies { testImplementation 'junit:junit:4.13' androidTestImplementation 'androidx.test:runner:1.3.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' + androidTestImplementation "androidx.room:room-testing:$room_version" } tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all { diff --git a/app/src/main/java/net/schueller/peertube/activity/ServerAddressBookActivity.kt b/app/src/main/java/net/schueller/peertube/activity/ServerAddressBookActivity.kt index 4a1f294..3c55cfa 100644 --- a/app/src/main/java/net/schueller/peertube/activity/ServerAddressBookActivity.kt +++ b/app/src/main/java/net/schueller/peertube/activity/ServerAddressBookActivity.kt @@ -108,8 +108,17 @@ class ServerAddressBookActivity : CommonActivity() { Toast.makeText(this, getString(R.string.server_selection_set_server, serverUrl), Toast.LENGTH_LONG).show() } + private fun onEditClick(server: Server) { + val fragmentTransaction = fragmentManager.beginTransaction() + addServerFragment = AddServerFragment.newInstance(server).also { + fragmentTransaction.replace(R.id.server_book, it) + fragmentTransaction.commit() + mBinding.addServer.hide() + } + } + private fun showServers() { - val adapter = ServerListAdapter(mutableListOf()) { onServerClick(it) }.also { + val adapter = ServerListAdapter(mutableListOf(), { onServerClick(it) }, { onEditClick(it) }).also { mBinding.serverListRecyclerview.adapter = it } diff --git a/app/src/main/java/net/schueller/peertube/adapter/ServerListAdapter.kt b/app/src/main/java/net/schueller/peertube/adapter/ServerListAdapter.kt index 908a71c..c970fde 100644 --- a/app/src/main/java/net/schueller/peertube/adapter/ServerListAdapter.kt +++ b/app/src/main/java/net/schueller/peertube/adapter/ServerListAdapter.kt @@ -24,7 +24,7 @@ import net.schueller.peertube.database.Server import net.schueller.peertube.databinding.RowServerAddressBookBinding import net.schueller.peertube.utils.visibleIf -class ServerListAdapter(private val mServers: MutableList, private val onClick: (Server) -> Unit) : RecyclerView.Adapter() { +class ServerListAdapter(private val mServers: MutableList, private val onClick: (Server) -> Unit, private val onEditClick: (Server) -> Unit) : RecyclerView.Adapter() { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ServerViewHolder { @@ -66,6 +66,7 @@ class ServerListAdapter(private val mServers: MutableList, private val o binding.sbRowHasLoginIcon.visibleIf { server.username.isNullOrBlank().not() } binding.root.setOnClickListener { onClick(server) } + binding.editIcon.setOnClickListener { onEditClick(server) } } } diff --git a/app/src/main/java/net/schueller/peertube/database/ServerDao.java b/app/src/main/java/net/schueller/peertube/database/ServerDao.kt similarity index 60% rename from app/src/main/java/net/schueller/peertube/database/ServerDao.java rename to app/src/main/java/net/schueller/peertube/database/ServerDao.kt index 4c6a565..a8d42ee 100644 --- a/app/src/main/java/net/schueller/peertube/database/ServerDao.java +++ b/app/src/main/java/net/schueller/peertube/database/ServerDao.kt @@ -14,29 +14,26 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ -package net.schueller.peertube.database; +package net.schueller.peertube.database -import androidx.lifecycle.LiveData; -import androidx.room.Dao; -import androidx.room.Delete; -import androidx.room.Insert; -import androidx.room.OnConflictStrategy; -import androidx.room.Query; - -import java.util.List; +import androidx.lifecycle.LiveData +import androidx.room.* @Dao -public interface ServerDao { +interface ServerDao { - @Insert(onConflict = OnConflictStrategy.REPLACE) - void insert(Server server); + @Insert + suspend fun insert(server: Server) + + @Update + suspend fun update(server: Server) @Query("DELETE FROM server_table") - void deleteAll(); + suspend fun deleteAll() @Delete - void delete(Server server); + suspend fun delete(server: Server) - @Query("SELECT * from server_table ORDER BY server_name DESC") - LiveData> getAllServers(); + @get:Query("SELECT * from server_table ORDER BY server_name DESC") + val allServers: LiveData> } \ No newline at end of file diff --git a/app/src/main/java/net/schueller/peertube/database/ServerRepository.java b/app/src/main/java/net/schueller/peertube/database/ServerRepository.java deleted file mode 100644 index 7aa7c86..0000000 --- a/app/src/main/java/net/schueller/peertube/database/ServerRepository.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright (C) 2020 Stefan Schüller - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ -package net.schueller.peertube.database; - -import android.app.Application; -import android.os.AsyncTask; - - -import androidx.lifecycle.LiveData; - -import java.util.List; - -class ServerRepository { - - private ServerDao mServerDao; - private LiveData> mAllServers; - - ServerRepository(Application application) { - ServerRoomDatabase db = ServerRoomDatabase.getDatabase(application); - mServerDao = db.serverDao(); - mAllServers = mServerDao.getAllServers(); - - } - - LiveData> getAllServers() { - return mAllServers; - } - - void insert (Server server) { - new insertAsyncTask(mServerDao).execute(server); - } - - public void delete(Server server) { - new deleteServerAsyncTask(mServerDao).execute(server); - } - - private static class insertAsyncTask extends AsyncTask { - - private ServerDao mAsyncTaskDao; - - insertAsyncTask(ServerDao dao) { - mAsyncTaskDao = dao; - } - - @Override - protected Void doInBackground(final Server... params) { - mAsyncTaskDao.insert(params[0]); - return null; - } - } - - private static class deleteServerAsyncTask extends AsyncTask { - private ServerDao mAsyncTaskDao; - - deleteServerAsyncTask(ServerDao dao) { - mAsyncTaskDao = dao; - } - - @Override - protected Void doInBackground(final Server... params) { - mAsyncTaskDao.delete(params[0]); - return null; - } - } -} diff --git a/app/src/main/java/net/schueller/peertube/database/ServerRepository.kt b/app/src/main/java/net/schueller/peertube/database/ServerRepository.kt new file mode 100644 index 0000000..871b9a9 --- /dev/null +++ b/app/src/main/java/net/schueller/peertube/database/ServerRepository.kt @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2020 Stefan Schüller + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package net.schueller.peertube.database + +import android.app.Application +import android.os.AsyncTask +import androidx.lifecycle.LiveData +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext + +internal class ServerRepository(application: Application) { + + private val mServerDao: ServerDao + + val allServers: LiveData> + get() = mServerDao.allServers + + init { + val db = ServerRoomDatabase.getDatabase(application) + mServerDao = db.serverDao() + } + + suspend fun update(server: Server) = withContext(Dispatchers.IO) { + mServerDao.update(server) + } + + suspend fun insert(server: Server) = withContext(Dispatchers.IO) { + mServerDao.insert(server) + } + + suspend fun delete(server: Server) = withContext(Dispatchers.IO){ + mServerDao.delete(server) + } +} \ No newline at end of file diff --git a/app/src/main/java/net/schueller/peertube/database/ServerViewModel.java b/app/src/main/java/net/schueller/peertube/database/ServerViewModel.java deleted file mode 100644 index 19044e0..0000000 --- a/app/src/main/java/net/schueller/peertube/database/ServerViewModel.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (C) 2020 Stefan Schüller - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ -package net.schueller.peertube.database; - -import android.app.Application; - -import androidx.lifecycle.AndroidViewModel; -import androidx.lifecycle.LiveData; - -import java.util.List; - -public class ServerViewModel extends AndroidViewModel { - - private ServerRepository mRepository; - - private LiveData> mAllServers; - - public ServerViewModel (Application application) { - super(application); - mRepository = new ServerRepository(application); - mAllServers = mRepository.getAllServers(); - } - - public LiveData> getAllServers() { return mAllServers; } - - public void insert(Server server) { mRepository.insert(server); } - - public void delete(Server server) {mRepository.delete(server);} - -} diff --git a/app/src/main/java/net/schueller/peertube/database/ServerViewModel.kt b/app/src/main/java/net/schueller/peertube/database/ServerViewModel.kt new file mode 100644 index 0000000..da17ef9 --- /dev/null +++ b/app/src/main/java/net/schueller/peertube/database/ServerViewModel.kt @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2020 Stefan Schüller + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package net.schueller.peertube.database + +import android.app.Application +import androidx.lifecycle.AndroidViewModel +import androidx.lifecycle.LiveData +import androidx.lifecycle.viewModelScope +import kotlinx.coroutines.launch + +class ServerViewModel(application: Application) : AndroidViewModel(application) { + + private val mRepository: ServerRepository = ServerRepository(application) + val allServers: LiveData> = mRepository.allServers + + fun insert(server: Server) { + viewModelScope.launch { + mRepository.insert(server) + } + } + + fun update(server: Server) { + viewModelScope.launch { + mRepository.update(server) + } + } + + fun delete(server: Server) { + viewModelScope.launch { + mRepository.delete(server) + } + } +} \ No newline at end of file diff --git a/app/src/main/java/net/schueller/peertube/fragment/AddServerFragment.kt b/app/src/main/java/net/schueller/peertube/fragment/AddServerFragment.kt index 099a98a..2bfc3be 100644 --- a/app/src/main/java/net/schueller/peertube/fragment/AddServerFragment.kt +++ b/app/src/main/java/net/schueller/peertube/fragment/AddServerFragment.kt @@ -89,6 +89,18 @@ class AddServerFragment : Fragment() { if (formValid) { + mServer?.apply { + mBinding.let { + serverName = it.serverLabel.text.toString() + serverHost = it.serverUrl.text.toString() + username = it.serverUsername.text.toString() + password = it.serverPassword.text.toString() + + mServerViewModel.update(this) + } + return@setOnClickListener + } + mBinding.apply { val server = Server(serverName = serverLabel.text.toString())