implemented server edit; converted ServerRepository and ServerDao to kotlin
This commit is contained in:
parent
2a1d2058e3
commit
13685974fd
@ -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 {
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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<Server>, private val onClick: (Server) -> Unit) : RecyclerView.Adapter<ServerViewHolder>() {
|
||||
class ServerListAdapter(private val mServers: MutableList<Server>, private val onClick: (Server) -> Unit, private val onEditClick: (Server) -> Unit) : RecyclerView.Adapter<ServerViewHolder>() {
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ServerViewHolder {
|
||||
|
||||
@ -66,6 +66,7 @@ class ServerListAdapter(private val mServers: MutableList<Server>, private val o
|
||||
binding.sbRowHasLoginIcon.visibleIf { server.username.isNullOrBlank().not() }
|
||||
|
||||
binding.root.setOnClickListener { onClick(server) }
|
||||
binding.editIcon.setOnClickListener { onEditClick(server) }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -14,29 +14,26 @@
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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<List<Server>> getAllServers();
|
||||
@get:Query("SELECT * from server_table ORDER BY server_name DESC")
|
||||
val allServers: LiveData<List<Server>>
|
||||
}
|
@ -1,79 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2020 Stefan Schüller <sschueller@techdroid.com>
|
||||
*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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<List<Server>> mAllServers;
|
||||
|
||||
ServerRepository(Application application) {
|
||||
ServerRoomDatabase db = ServerRoomDatabase.getDatabase(application);
|
||||
mServerDao = db.serverDao();
|
||||
mAllServers = mServerDao.getAllServers();
|
||||
|
||||
}
|
||||
|
||||
LiveData<List<Server>> 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<Server, Void, Void> {
|
||||
|
||||
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<Server, Void, Void> {
|
||||
private ServerDao mAsyncTaskDao;
|
||||
|
||||
deleteServerAsyncTask(ServerDao dao) {
|
||||
mAsyncTaskDao = dao;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Void doInBackground(final Server... params) {
|
||||
mAsyncTaskDao.delete(params[0]);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright (C) 2020 Stefan Schüller <sschueller@techdroid.com>
|
||||
*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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<List<Server>>
|
||||
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)
|
||||
}
|
||||
}
|
@ -1,44 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2020 Stefan Schüller <sschueller@techdroid.com>
|
||||
*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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<List<Server>> mAllServers;
|
||||
|
||||
public ServerViewModel (Application application) {
|
||||
super(application);
|
||||
mRepository = new ServerRepository(application);
|
||||
mAllServers = mRepository.getAllServers();
|
||||
}
|
||||
|
||||
public LiveData<List<Server>> getAllServers() { return mAllServers; }
|
||||
|
||||
public void insert(Server server) { mRepository.insert(server); }
|
||||
|
||||
public void delete(Server server) {mRepository.delete(server);}
|
||||
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright (C) 2020 Stefan Schüller <sschueller@techdroid.com>
|
||||
*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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<List<Server>> = 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)
|
||||
}
|
||||
}
|
||||
}
|
@ -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())
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user