From 4f12fd94ff56a5eba013c83221f82a823c639e20 Mon Sep 17 00:00:00 2001 From: kosharskiy Date: Thu, 14 Jan 2021 02:01:39 +0200 Subject: [PATCH] Converted ServerAddressBookActivity to kotlin and refactored this activity --- .../activity/ServerAddressBookActivity.java | 165 ------------------ .../activity/ServerAddressBookActivity.kt | 128 ++++++++++++++ .../layout/activity_server_address_book.xml | 20 ++- .../layout/content_server_address_book.xml | 23 --- 4 files changed, 147 insertions(+), 189 deletions(-) delete mode 100644 app/src/main/java/net/schueller/peertube/activity/ServerAddressBookActivity.java create mode 100644 app/src/main/java/net/schueller/peertube/activity/ServerAddressBookActivity.kt delete mode 100644 app/src/main/res/layout/content_server_address_book.xml diff --git a/app/src/main/java/net/schueller/peertube/activity/ServerAddressBookActivity.java b/app/src/main/java/net/schueller/peertube/activity/ServerAddressBookActivity.java deleted file mode 100644 index 2e637f7..0000000 --- a/app/src/main/java/net/schueller/peertube/activity/ServerAddressBookActivity.java +++ /dev/null @@ -1,165 +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.activity; - -import android.app.AlertDialog; -import android.os.Bundle; -import android.util.Log; - -import com.google.android.material.floatingactionbutton.FloatingActionButton; - -import androidx.annotation.NonNull; - -import androidx.appcompat.widget.Toolbar; -import androidx.fragment.app.FragmentManager; -import androidx.fragment.app.FragmentTransaction; -import androidx.lifecycle.ViewModelProvider; -import androidx.recyclerview.widget.ItemTouchHelper; -import androidx.recyclerview.widget.LinearLayoutManager; -import androidx.recyclerview.widget.RecyclerView; - -import net.schueller.peertube.R; -import net.schueller.peertube.adapter.ServerListAdapter; -import net.schueller.peertube.database.Server; -import net.schueller.peertube.database.ServerViewModel; -import net.schueller.peertube.fragment.AddServerFragment; - - -import java.util.Objects; - -public class ServerAddressBookActivity extends CommonActivity { - - private String TAG = "ServerAddressBookActivity"; - public static final String EXTRA_REPLY = "net.schueller.peertube.room.REPLY"; - - private ServerViewModel mServerViewModel; - private AddServerFragment addServerFragment; - private FloatingActionButton floatingActionButton; - private FragmentManager fragmentManager; - - @Override - public boolean onSupportNavigateUp() { - finish(); // close this activity as oppose to navigating up - return false; - } - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_server_address_book); - - // Attaching the layout to the toolbar object - Toolbar toolbar = findViewById(R.id.tool_bar_server_address_book); - // Setting toolbar as the ActionBar with setSupportActionBar() call - setSupportActionBar(toolbar); - Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true); - getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_baseline_close_24); - - mServerViewModel = new ViewModelProvider(this).get(ServerViewModel.class); - - showServers(); - - floatingActionButton = findViewById(R.id.add_server); - floatingActionButton.setOnClickListener(view -> { - - Log.d(TAG, "Click"); - - fragmentManager = getSupportFragmentManager(); - FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); - - addServerFragment = new AddServerFragment(); - fragmentTransaction.replace(R.id.server_book, addServerFragment); - fragmentTransaction.commit(); - - floatingActionButton.hide(); - - }); - - } - - - @Override - public void onPointerCaptureChanged(boolean hasCapture) { - - } - - - public void showServers() - { - RecyclerView recyclerView = findViewById(R.id.server_list_recyclerview); - final ServerListAdapter adapter = new ServerListAdapter(this); - recyclerView.setAdapter(adapter); - - // Delete items on swipe - ItemTouchHelper helper = new ItemTouchHelper( - new ItemTouchHelper.SimpleCallback(0, - ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) { - @Override - public boolean onMove(@NonNull RecyclerView recyclerView, - @NonNull RecyclerView.ViewHolder viewHolder, - @NonNull RecyclerView.ViewHolder target) { - return false; - } - - @Override - public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, - int direction) { - - - new AlertDialog.Builder(ServerAddressBookActivity.this) - .setTitle(getString(R.string.server_book_del_alert_title)) - .setMessage(getString(R.string.server_book_del_alert_msg)) - .setPositiveButton(android.R.string.yes, (dialog, which) -> { - int position = viewHolder.getAdapterPosition(); - Server server = adapter.getServerAtPosition(position); -// Toast.makeText(ServerAddressBookActivity.this, "Deleting " + -// server.getServerName(), Toast.LENGTH_LONG).show(); - // Delete the server - mServerViewModel.delete(server); - }) - .setNegativeButton(android.R.string.no, (dialog, which) -> { - adapter.notifyItemChanged(viewHolder.getAdapterPosition()); - }) - .setIcon(android.R.drawable.ic_dialog_alert) - .show(); - - } - }); - helper.attachToRecyclerView(recyclerView); - - - // Update the cached copy of the words in the adapter. - mServerViewModel.getAllServers().observe(this, servers -> { - adapter.setServers(servers); - - if (addServerFragment != null) { - FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); - fragmentTransaction.remove(addServerFragment); - fragmentTransaction.commit(); - - floatingActionButton.show(); - } - }); - - } - - public void testServer() - { - - } - -} diff --git a/app/src/main/java/net/schueller/peertube/activity/ServerAddressBookActivity.kt b/app/src/main/java/net/schueller/peertube/activity/ServerAddressBookActivity.kt new file mode 100644 index 0000000..e5fc96e --- /dev/null +++ b/app/src/main/java/net/schueller/peertube/activity/ServerAddressBookActivity.kt @@ -0,0 +1,128 @@ +/* + * 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.activity + +import android.app.AlertDialog +import android.content.DialogInterface +import android.os.Bundle +import android.util.Log +import androidx.activity.viewModels +import androidx.fragment.app.FragmentManager +import androidx.recyclerview.widget.ItemTouchHelper +import androidx.recyclerview.widget.RecyclerView +import net.schueller.peertube.R +import net.schueller.peertube.adapter.ServerListAdapter +import net.schueller.peertube.database.Server +import net.schueller.peertube.database.ServerViewModel +import net.schueller.peertube.databinding.ActivityServerAddressBookBinding +import net.schueller.peertube.fragment.AddServerFragment +import java.util.* + +class ServerAddressBookActivity : CommonActivity() { + + private val TAG = "ServerAddressBookActivity" + + private val mServerViewModel: ServerViewModel by viewModels() + private var addServerFragment: AddServerFragment? = null + + private val fragmentManager: FragmentManager by lazy { supportFragmentManager } + + + private lateinit var mBinding: ActivityServerAddressBookBinding + + + override fun onSupportNavigateUp(): Boolean { + finish() // close this activity as oppose to navigating up + return false + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + mBinding = ActivityServerAddressBookBinding.inflate(layoutInflater) + setContentView(mBinding.root) + + // Setting toolbar as the ActionBar with setSupportActionBar() call + setSupportActionBar(mBinding.toolBarServerAddressBook) + supportActionBar?.apply { + setDisplayHomeAsUpEnabled(true) + setHomeAsUpIndicator(R.drawable.ic_baseline_close_24) + } + + + showServers() + + mBinding.addServer.setOnClickListener { + Log.d(TAG, "Click") + + val fragmentTransaction = fragmentManager.beginTransaction() + addServerFragment = AddServerFragment().also { + fragmentTransaction.replace(R.id.server_book, it) + fragmentTransaction.commit() + mBinding.addServer.hide() + } + } + } + + private fun showServers() { + val adapter = ServerListAdapter(this).also { + mBinding.serverListRecyclerview.adapter = it + } + + // Delete items on swipe + val helper = ItemTouchHelper( + object : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT) { + override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean { + return false + } + + override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) { + AlertDialog.Builder(this@ServerAddressBookActivity) + .setTitle(getString(R.string.server_book_del_alert_title)) + .setMessage(getString(R.string.server_book_del_alert_msg)) + .setPositiveButton(android.R.string.yes) { _: DialogInterface?, _: Int -> + val position = viewHolder.adapterPosition + val server = adapter.getServerAtPosition(position) +// Toast.makeText(ServerAddressBookActivity.this, "Deleting " + +// server.getServerName(), Toast.LENGTH_LONG).show(); + // Delete the server + mServerViewModel.delete(server) + } + .setNegativeButton(android.R.string.no) { _: DialogInterface?, _: Int -> adapter.notifyItemChanged(viewHolder.adapterPosition) } + .setIcon(android.R.drawable.ic_dialog_alert) + .show() + } + }) + helper.attachToRecyclerView(mBinding.serverListRecyclerview) + + + // Update the cached copy of the words in the adapter. + mServerViewModel.allServers.observe(this, { servers: List? -> + adapter.setServers(servers) + + addServerFragment?.let { + val fragmentTransaction = fragmentManager.beginTransaction() + fragmentTransaction.remove(it) + fragmentTransaction.commit() + mBinding.addServer.show() + } + }) + } + + companion object { + const val EXTRA_REPLY = "net.schueller.peertube.room.REPLY" + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_server_address_book.xml b/app/src/main/res/layout/activity_server_address_book.xml index 5de5eb8..d09f244 100644 --- a/app/src/main/res/layout/activity_server_address_book.xml +++ b/app/src/main/res/layout/activity_server_address_book.xml @@ -31,7 +31,25 @@ android:layout_margin="@dimen/fab_margin" app:srcCompat="@drawable/ic_baseline_add_24" /> - + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/content_server_address_book.xml b/app/src/main/res/layout/content_server_address_book.xml deleted file mode 100644 index 98667eb..0000000 --- a/app/src/main/res/layout/content_server_address_book.xml +++ /dev/null @@ -1,23 +0,0 @@ - - - - - - - - - \ No newline at end of file