diff --git a/app/build.gradle b/app/build.gradle index 1112cf7..352ef63 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -54,6 +54,8 @@ dependencies { api "org.jetbrains.kotlinx:kotlinx-coroutines-android:$rootProject.coroutines" // UI implementation "com.google.android.material:material:$rootProject.materialVersion" + implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0" + // TESTING diff --git a/app/src/main/java/it/unisannio/ding/ids/wedroid/app/view/BoardsListsActivity.kt b/app/src/main/java/it/unisannio/ding/ids/wedroid/app/view/BoardsListsActivity.kt index 6c36ccd..0e1064f 100644 --- a/app/src/main/java/it/unisannio/ding/ids/wedroid/app/view/BoardsListsActivity.kt +++ b/app/src/main/java/it/unisannio/ding/ids/wedroid/app/view/BoardsListsActivity.kt @@ -4,7 +4,9 @@ import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import androidx.lifecycle.Observer import androidx.lifecycle.ViewModelProvider +import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView import it.unisannio.ding.ids.wedroid.app.R import it.unisannio.ding.ids.wedroid.app.view.adapter.BoardsListAdapter import it.unisannio.ding.ids.wedroid.app.viewmodel.BoardsListViewModel @@ -25,14 +27,45 @@ class BoardsListsActivity : AppCompatActivity() { boardList.adapter = adapter boardList.layoutManager = LinearLayoutManager(this) + swipeLeftToDelete() + + pullToRefresh.setColorSchemeColors(getColor(R.color.colorAccent)) + viewModel.allBoards.observe(this, Observer { it.let { adapter.setBoards(it) } + pullToRefresh.isRefreshing = false }) - fab.setOnClickListener { view -> + fab.setOnClickListener { viewModel.insertBoard("New board") } + + pullToRefresh.setOnRefreshListener { + viewModel.refresh() + } + } + private fun swipeLeftToDelete() { + val swipeToDelete = ItemTouchHelper( + object : ItemTouchHelper.SimpleCallback( + 0, ItemTouchHelper.LEFT + ) { + override fun onMove( + recyclerView: RecyclerView, + viewHolder: RecyclerView.ViewHolder, + target: RecyclerView.ViewHolder + ): Boolean { + return false + } + + override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) { + val pos = viewHolder.adapterPosition + viewModel.deleteBoard(pos) + } + }) + + swipeToDelete.attachToRecyclerView(boardList) + } } diff --git a/app/src/main/java/it/unisannio/ding/ids/wedroid/app/viewmodel/BoardsListViewModel.java b/app/src/main/java/it/unisannio/ding/ids/wedroid/app/viewmodel/BoardsListViewModel.java index f8f3a9f..79ab9c2 100644 --- a/app/src/main/java/it/unisannio/ding/ids/wedroid/app/viewmodel/BoardsListViewModel.java +++ b/app/src/main/java/it/unisannio/ding/ids/wedroid/app/viewmodel/BoardsListViewModel.java @@ -38,7 +38,11 @@ public class BoardsListViewModel extends AndroidViewModel { repository.insertBoard(title); } - public void deleteBoard(String id) { - repository.deleteBoard(id); + public void deleteBoard(int position) { + repository.deleteBoard(allBoards.getValue().get(position).getId()); + } + + public void refresh() { + repository.synchronize(); } } diff --git a/app/src/main/res/layout/content_boards_lists.xml b/app/src/main/res/layout/content_boards_lists.xml index cb2ba98..dc3adf9 100644 --- a/app/src/main/res/layout/content_boards_lists.xml +++ b/app/src/main/res/layout/content_boards_lists.xml @@ -7,6 +7,14 @@ app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context=".view.BoardsListsActivity" tools:showIn="@layout/activity_boards_lists"> + + + \ No newline at end of file