Skip to content

Commit

Permalink
Update demo app
Browse files Browse the repository at this point in the history
  • Loading branch information
ChanTsune authored Apr 17, 2021
1 parent 5d3e67d commit 6c150af
Show file tree
Hide file tree
Showing 18 changed files with 237 additions and 186 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.1.2"
classpath "com.android.tools.build:gradle:4.1.3"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.github.chantsune.swipetoaction.demo.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.github.chantsune.swipetoaction.demo.databinding.FragmentListBinding

open class BaseListFragment : Fragment() {

protected lateinit var binding: FragmentListBinding

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = FragmentListBinding.inflate(inflater, container, false)
return binding.root
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,18 @@ package com.github.chantsune.swipetoaction.demo.custom

import androidx.lifecycle.ViewModelProvider
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.get
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.github.chantsune.swipetoaction.demo.databinding.FragmentCustomSwipeLayoutBinding
import com.github.chantsune.swipetoaction.demo.base.BaseListFragment

class CustomSwipeLayoutFragment : Fragment() {
class CustomSwipeLayoutFragment : BaseListFragment() {

private lateinit var binding: FragmentCustomSwipeLayoutBinding
private lateinit var viewModel: CustomSwipeLayoutViewModel

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = FragmentCustomSwipeLayoutBinding.inflate(inflater, container, false)
return binding.root
}

override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModel = ViewModelProvider(this).get(CustomSwipeLayoutViewModel::class.java)
viewModel = ViewModelProvider(this).get()
binding.recyclerView.apply {
adapter = RecyclerAdapter()
layoutManager = LinearLayoutManager(requireContext())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.github.chantsune.swipetoaction.demo.grid

import android.os.Bundle
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.get
import androidx.recyclerview.widget.GridLayoutManager
import com.github.chantsune.swipetoaction.demo.base.BaseListFragment
import com.github.chantsune.swipetoaction.demo.simple.RecyclerAdapter

class GridLayoutFragment : BaseListFragment() {

private lateinit var viewModel: GridLayoutViewModel

override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModel = ViewModelProvider(this).get()
binding.recyclerView.apply {
adapter = RecyclerAdapter()
layoutManager = GridLayoutManager(requireContext(), 2).apply {
spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
override fun getSpanSize(position: Int): Int {
return if (position in 0..3) {
2
} else {
1
}
}
}
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.github.chantsune.swipetoaction.demo.grid

import androidx.lifecycle.ViewModel

class GridLayoutViewModel : ViewModel() {
// TODO: Implement the ViewModel
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.github.chantsune.swipetoaction.demo.main

import androidx.lifecycle.ViewModelProvider
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.get
import androidx.navigation.fragment.findNavController
import com.github.chantsune.swipetoaction.demo.databinding.FragmentMainBinding

Expand All @@ -23,13 +24,17 @@ class MainFragment : Fragment() {

override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModel = ViewModelProvider(this).get(MainViewModel::class.java)
viewModel = ViewModelProvider(this).get()

binding.goToSimpleButton.setOnClickListener {
findNavController().navigate(MainFragmentDirections.actionMainFragmentToSimpleSwipeLayoutFragment())
}
binding.goToCustomButton.setOnClickListener {
findNavController().navigate(MainFragmentDirections.actionMainFragmentToCustomSwipeLayoutFragment())
}
binding.goToSimpleButton.setOnClickListener {
findNavController().navigate(MainFragmentDirections.actionMainFragmentToSimpleSwipeLayoutFragment())
binding.goToGridButton.setOnClickListener {
findNavController().navigate(MainFragmentDirections.actionMainFragmentToGridLayoutFragment())
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import com.github.chantsune.swipetoaction.views.SwipeLayout
import com.github.chantsune.swipetoaction.views.SwipeLayout.OnSwipeItemClickListener

class RecyclerAdapter : RecyclerView.Adapter<RecyclerAdapter.ViewHolder>() {
class ViewHolder(val binding: ViewSimpleSwipeLayoutItemBinding) :
RecyclerView.ViewHolder(binding.root) {
val contentBinding = SampleItemBinding.bind(binding.swipeLayout.contentView)
}
private val strings = MutableList(30) { it.toString() }
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val v =
Expand All @@ -21,67 +25,73 @@ class RecyclerAdapter : RecyclerView.Adapter<RecyclerAdapter.ViewHolder>() {

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.contentBinding.recyclerItemTv.text = "Item # ${strings[position]}"
holder.binding.swipeLayout.setItemState(SwipeLayout.ITEM_STATE_COLLAPSED, false)
}

override fun getItemCount(): Int = strings.size

inner class ViewHolder(val binding: ViewSimpleSwipeLayoutItemBinding) :
RecyclerView.ViewHolder(binding.root),
View.OnClickListener, OnLongClickListener, OnSwipeItemClickListener {
val contentBinding = SampleItemBinding.bind(binding.swipeLayout.contentView)

init {
binding.swipeLayout.setOnClickListener(this)
binding.swipeLayout.setOnLongClickListener(this)
binding.swipeLayout.setOnSwipeItemClickListener(this)
when (position % 3) {
0 -> {
holder.binding.swipeLayout.canFullSwipeLeftToRight = false
holder.binding.swipeLayout.canFullSwipeRightToLeft = false
}
1 -> {
holder.binding.swipeLayout.canFullSwipeLeftToRight = true
holder.binding.swipeLayout.canFullSwipeRightToLeft = false
}
else -> {
holder.binding.swipeLayout.canFullSwipeLeftToRight = false
holder.binding.swipeLayout.canFullSwipeRightToLeft = true
}
}

override fun onClick(view: View) {
holder.binding.swipeLayout.setItemState(SwipeLayout.ITEM_STATE_COLLAPSED, false)
holder.binding.swipeLayout.setOnClickListener { view ->
Toast.makeText(
view.context,
"Clicked at ${strings[adapterPosition]}",
"Clicked at ${strings[holder.adapterPosition]}",
Toast.LENGTH_SHORT
).show()
}

override fun onLongClick(view: View): Boolean {
holder.binding.swipeLayout.setOnLongClickListener { view ->
Toast.makeText(
view.context,
"Long Clicked at ${strings[adapterPosition]}",
"Long Clicked at ${strings[holder.adapterPosition]}",
Toast.LENGTH_SHORT
).show()
return true
true
}

override fun onSwipeItemClick(left: Boolean, index: Int) {
if (left) {
binding.swipeLayout.also { swipeLayout ->
if (swipeLayout.isEnabledAtIndex(true, index)) {
swipeLayout.setAlphaAtIndex(true, index, 0.5f)
swipeLayout.setEnableAtIndex(true, index, false)
swipeLayout.collapseAll(true)
} else {
swipeLayout.setAlphaAtIndex(true, index, 1f)
swipeLayout.setEnableAtIndex(true, index, true)
}
}
} else {
when (index) {
0 -> {
Toast.makeText(itemView.context, "Reload", Toast.LENGTH_SHORT).show()
}
1 -> {
Toast.makeText(itemView.context, "Settings", Toast.LENGTH_SHORT).show()
holder.binding.swipeLayout.setOnSwipeItemClickListener(object : OnSwipeItemClickListener {
override fun onSwipeItemClick(left: Boolean, index: Int) {
if (left) {
holder.binding.swipeLayout.also { swipeLayout ->
if (swipeLayout.isEnabledAtIndex(true, index)) {
swipeLayout.setAlphaAtIndex(true, index, 0.5f)
swipeLayout.setEnableAtIndex(true, index, false)
swipeLayout.collapseAll(true)
} else {
swipeLayout.setAlphaAtIndex(true, index, 1f)
swipeLayout.setEnableAtIndex(true, index, true)
}
}
2 -> {
val pos = adapterPosition
strings.removeAt(pos)
notifyItemRemoved(pos)
Toast.makeText(itemView.context, "Trash", Toast.LENGTH_SHORT).show()
} else {
when (index) {
0 -> {
Toast.makeText(holder.itemView.context, "Reload", Toast.LENGTH_SHORT)
.show()
}
1 -> {
Toast.makeText(holder.itemView.context, "Settings", Toast.LENGTH_SHORT)
.show()
}
2 -> {
val pos = holder.adapterPosition
strings.removeAt(pos)
notifyItemRemoved(pos)
Toast.makeText(holder.itemView.context, "Trash", Toast.LENGTH_SHORT)
.show()
}
}
}
}
}
})
}
}

override fun getItemCount(): Int = strings.size
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,20 @@ package com.github.chantsune.swipetoaction.demo.simple

import androidx.lifecycle.ViewModelProvider
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.get
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.github.chantsune.swipetoaction.demo.databinding.FragmentSimpleSwipeLayoutBinding
import com.github.chantsune.swipetoaction.demo.base.BaseListFragment
import com.github.chantsune.swipetoaction.views.SwipeLayout

class SimpleSwipeLayoutFragment : Fragment() {
class SimpleSwipeLayoutFragment : BaseListFragment() {

private lateinit var binding: FragmentSimpleSwipeLayoutBinding
private lateinit var viewModel: SimpleSwipeLayoutViewModel

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = FragmentSimpleSwipeLayoutBinding.inflate(inflater, container, false)
return binding.root
}

override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModel = ViewModelProvider(this).get(SimpleSwipeLayoutViewModel::class.java)
viewModel = ViewModelProvider(this).get()
binding.recyclerView.also { recyclerView ->
recyclerView.layoutManager = LinearLayoutManager(context)
recyclerView.adapter = RecyclerAdapter()
Expand Down
File renamed without changes.
9 changes: 9 additions & 0 deletions demo/src/main/res/layout/fragment_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@
android:layout_height="wrap_content"
android:text="@string/custom_swipe_layout"
app:layout_constraintTop_toBottomOf="@id/go_to_simple_button"
app:layout_constraintBottom_toTopOf="@id/go_to_grid_button" />

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/go_to_grid_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Simple With Grid"
app:layout_constraintTop_toBottomOf="@id/go_to_custom_button"
app:layout_constraintBottom_toBottomOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>
18 changes: 0 additions & 18 deletions demo/src/main/res/layout/fragment_simple_swipe_layout.xml

This file was deleted.

14 changes: 2 additions & 12 deletions demo/src/main/res/layout/view_custom_swipe_layout_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,7 @@
android:id="@+id/swipe_layout"
android:layout_width="match_parent"
android:layout_height="80dp"
app:layout_swipeCanFullSwipeRightToLeft="true"
app:layout_swipeCanFullSwipeLeftToRight="true"
app:layout_swipeCanFullSwipeRightToLeft="true"
app:layout_swipeContentLayout="@layout/sample_item"
app:layout_swipeIconSize="40dp"
app:layout_swipeLeftIconColors="@array/leftDrawableColors"
app:layout_swipeLeftItemColors="@array/leftColors"
app:layout_swipeLeftItemIcons="@array/leftDrawables"
app:layout_swipeRightIconColors="@array/rightDrawableColors"
app:layout_swipeRightItemColors="@array/rightColors"
app:layout_swipeRightItemIcons="@array/rightDrawables"
app:layout_swipeRightStrings="@array/rightTexts"
app:layout_swipeRightTextColors="@array/rightTextColors"
app:layout_swipeSwipeItemWidth="80dp"
app:layout_swipeTextSize="18sp" />
app:layout_swipeSwipeItemWidth="80dp" />
14 changes: 12 additions & 2 deletions demo/src/main/res/layout/view_simple_swipe_layout_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@
android:id="@+id/swipe_layout"
android:layout_width="match_parent"
android:layout_height="80dp"
app:layout_swipeContentLayout="@layout/sample_item"
app:layout_swipeCanFullSwipeLeftToRight="true"
app:layout_swipeCanFullSwipeRightToLeft="true"
app:layout_swipeSwipeItemWidth="80dp" />
app:layout_swipeContentLayout="@layout/sample_item"
app:layout_swipeIconSize="40dp"
app:layout_swipeLeftIconColors="@array/leftDrawableColors"
app:layout_swipeLeftItemColors="@array/leftColors"
app:layout_swipeLeftItemIcons="@array/leftDrawables"
app:layout_swipeRightIconColors="@array/rightDrawableColors"
app:layout_swipeRightItemColors="@array/rightColors"
app:layout_swipeRightItemIcons="@array/rightDrawables"
app:layout_swipeRightStrings="@array/rightTexts"
app:layout_swipeRightTextColors="@array/rightTextColors"
app:layout_swipeSwipeItemWidth="80dp"
app:layout_swipeTextSize="18sp" />
Loading

0 comments on commit 6c150af

Please sign in to comment.