Skip to content

Commit 83d1b1b

Browse files
author
Bene0202
committed
UI-Update 90%
1 parent 98b2df1 commit 83d1b1b

6 files changed

Lines changed: 266 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.nlinterface.adapters
2+
3+
import androidx.fragment.app.Fragment
4+
import androidx.fragment.app.FragmentActivity
5+
import androidx.viewpager2.adapter.FragmentStateAdapter
6+
import com.nlinterface.fragments.BarcodeSettingsScreen1
7+
import com.nlinterface.fragments.BarcodeSettingsScreen2
8+
import com.nlinterface.fragments.BarcodeSettingsScreen3
9+
10+
class BarcodeSettingsFragmentAdapter(
11+
fragmentActivity: FragmentActivity
12+
): FragmentStateAdapter(fragmentActivity) {
13+
14+
private val fragmentList = mutableListOf<Fragment>()
15+
16+
override fun getItemCount(): Int {
17+
// Return the count of static fragments plus the dynamic ones
18+
return fragmentList.size
19+
}
20+
21+
init {
22+
// Add static fragments
23+
fragmentList.add(BarcodeSettingsScreen1())
24+
fragmentList.add(BarcodeSettingsScreen2())
25+
fragmentList.add(BarcodeSettingsScreen3())
26+
}
27+
28+
override fun createFragment(position: Int): Fragment {
29+
return fragmentList[position]
30+
}
31+
32+
fun getCurrentFragment(position: Int): Fragment? {
33+
return fragmentList.getOrNull(position)
34+
}
35+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.nlinterface.adapters
2+
3+
import androidx.fragment.app.Fragment
4+
import androidx.fragment.app.FragmentActivity
5+
import androidx.viewpager2.adapter.FragmentStateAdapter
6+
import com.nlinterface.fragments.GroceryListScreen1
7+
import com.nlinterface.fragments.GroceryListScreen2
8+
import com.nlinterface.fragments.GroceryListScreenBase
9+
import com.nlinterface.fragments.GroceryListScreenListView
10+
11+
class GroceryListFragmentAdapter(
12+
fragmentActivity: FragmentActivity
13+
): FragmentStateAdapter(fragmentActivity) {
14+
15+
val fragmentList = mutableListOf<Fragment>()
16+
17+
override fun getItemCount(): Int {
18+
// Return the count of static fragments plus the dynamic ones
19+
return fragmentList.size
20+
}
21+
22+
init {
23+
// Add static fragments
24+
fragmentList.add(GroceryListScreenListView())
25+
fragmentList.add(GroceryListScreen1())
26+
fragmentList.add(GroceryListScreen2())
27+
}
28+
29+
override fun createFragment(position: Int): Fragment {
30+
return fragmentList[position]
31+
}
32+
33+
fun addFragment(fragment: GroceryListScreenBase){
34+
fragmentList.add(fragment)
35+
}
36+
37+
fun removeFragment(fragment: GroceryListScreenBase){
38+
val position = fragmentList.indexOf(fragment)
39+
fragmentList.removeAt(position)
40+
notifyItemRemoved(position)
41+
}
42+
43+
fun clearFragments(){
44+
if (fragmentList.size > 2) {
45+
val fragmentsToRemove = fragmentList.size - 2
46+
// Remove all fragments starting from the third one
47+
fragmentList.subList(2, fragmentList.size).clear()
48+
notifyItemRangeRemoved(2, fragmentsToRemove)
49+
}
50+
}
51+
52+
fun getCurrentFragment(position: Int): Fragment? {
53+
return fragmentList.getOrNull(position)
54+
}
55+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.nlinterface.adapters
2+
3+
import androidx.fragment.app.Fragment
4+
import androidx.fragment.app.FragmentActivity
5+
import androidx.viewpager2.adapter.FragmentStateAdapter
6+
import com.nlinterface.fragments.SettingsScreen1
7+
import com.nlinterface.fragments.SettingsScreen2
8+
import com.nlinterface.fragments.SettingsScreen3
9+
10+
class SettingsFragmentAdapter(
11+
fragmentActivity: FragmentActivity
12+
): FragmentStateAdapter(fragmentActivity) {
13+
14+
private val fragmentList = mutableListOf<Fragment>()
15+
16+
override fun getItemCount(): Int {
17+
// Return the count of static fragments plus the dynamic ones
18+
return fragmentList.size
19+
}
20+
21+
init {
22+
// Add static fragments
23+
fragmentList.add(SettingsScreen1())
24+
fragmentList.add(SettingsScreen2())
25+
fragmentList.add(SettingsScreen3())
26+
}
27+
28+
override fun createFragment(position: Int): Fragment {
29+
return fragmentList[position]
30+
}
31+
32+
fun getCurrentFragment(position: Int): Fragment? {
33+
return fragmentList.getOrNull(position)
34+
}
35+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.nlinterface.fragments
2+
3+
import android.os.Bundle
4+
import android.view.LayoutInflater
5+
import android.view.View
6+
import android.view.ViewGroup
7+
import androidx.fragment.app.Fragment
8+
import androidx.lifecycle.ViewModelProvider
9+
import androidx.recyclerview.widget.LinearLayoutManager
10+
import androidx.recyclerview.widget.RecyclerView
11+
import com.nlinterface.R
12+
import com.nlinterface.activities.GroceryListActivity
13+
import com.nlinterface.adapters.GroceryListAdapter
14+
import com.nlinterface.dataclasses.GroceryItem
15+
import com.nlinterface.utility.SwipeAction
16+
import com.nlinterface.utility.SwipeNavigationListener
17+
import com.nlinterface.viewmodels.GroceryListViewModel
18+
19+
class GroceryListScreenListView : Fragment(), SwipeAction {
20+
21+
lateinit var viewModel: GroceryListViewModel
22+
private lateinit var adapter: GroceryListAdapter
23+
private lateinit var groceryItemList: ArrayList<GroceryItem>
24+
private lateinit var rvGroceryList: RecyclerView
25+
26+
override fun onCreateView(
27+
inflater: LayoutInflater, container: ViewGroup?,
28+
savedInstanceState: Bundle?
29+
): View? {
30+
// Inflate the layout for this fragment
31+
val view = inflater.inflate(R.layout.grocerylist_listview, container, false)
32+
view.setOnTouchListener(SwipeNavigationListener(requireContext(), this))
33+
rvGroceryList = view.findViewById<View>(R.id.grocery_list_rv) as RecyclerView
34+
viewModel = ViewModelProvider(this)[GroceryListViewModel::class.java]
35+
36+
return view
37+
}
38+
39+
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
40+
super.onViewCreated(view, savedInstanceState)
41+
// set up add item button listener
42+
viewModel.fetchGroceryList()
43+
groceryItemList = viewModel.groceryList
44+
configureRecyclerView()
45+
}
46+
47+
override fun onSwipeLeft() {}
48+
override fun onSwipeRight() {}
49+
override fun onSwipeUp() {}
50+
override fun onSwipeDown(){}
51+
override fun onDoubleTap() {}
52+
override fun onLongPress() {}
53+
54+
private fun configureRecyclerView() {
55+
adapter = GroceryListAdapter(groceryItemList, activity as GroceryListActivity)
56+
57+
rvGroceryList.adapter = adapter
58+
rvGroceryList.layoutManager = LinearLayoutManager(activity as GroceryListActivity)
59+
}
60+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.nlinterface.utility
2+
3+
import android.util.Log
4+
import android.view.MotionEvent
5+
import androidx.recyclerview.widget.RecyclerView
6+
import kotlin.math.abs
7+
8+
class OnSwipeTouchInterceptor(
9+
private val swipeAction: SwipeAction
10+
) : RecyclerView.OnItemTouchListener {
11+
12+
13+
private var initialXValue: Float = 0f
14+
private var initialYValue: Float = 0f
15+
private val horizontalSwipeThreshold = 50
16+
private val verticalSwipeThreshold = 25
17+
18+
override fun onInterceptTouchEvent(rv: RecyclerView, e: MotionEvent): Boolean {
19+
when (e.action) {
20+
MotionEvent.ACTION_DOWN -> {
21+
initialXValue = e.x
22+
initialYValue = e.y
23+
}
24+
MotionEvent.ACTION_MOVE -> {
25+
val diffX = e.x - initialXValue
26+
val diffY = e.y - initialYValue
27+
if (abs(diffY) > abs(diffX) && abs(diffY) > verticalSwipeThreshold) {
28+
// Handle vertical swipe
29+
if (diffY > 0) {
30+
swipeAction.onSwipeDown()
31+
} else {
32+
swipeAction.onSwipeUp()
33+
}
34+
Log.d("SwipeAction","Vertical Swipe Action detected")
35+
return true
36+
} else if (abs(diffX) > horizontalSwipeThreshold) {
37+
return false
38+
}
39+
}
40+
}
41+
return false
42+
}
43+
44+
override fun onTouchEvent(rv: RecyclerView, e: MotionEvent) {}
45+
46+
override fun onRequestDisallowInterceptTouchEvent(disallowIntercept: Boolean) {}
47+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
android:id="@+id/grocerylist_listview">
8+
9+
<androidx.recyclerview.widget.RecyclerView
10+
android:id="@+id/grocery_list_rv"
11+
android:layout_width="0dp"
12+
android:layout_height="0dp"
13+
android:clipToPadding="false"
14+
android:overScrollMode="never"
15+
app:layout_constraintBottom_toBottomOf="parent"
16+
app:layout_constraintEnd_toEndOf="@id/v_guide2"
17+
app:layout_constraintStart_toStartOf="@id/v_guide1"
18+
app:layout_constraintTop_toTopOf="parent" />
19+
20+
<androidx.constraintlayout.widget.Guideline
21+
android:id="@+id/v_guide1"
22+
android:layout_width="wrap_content"
23+
android:layout_height="wrap_content"
24+
android:orientation="vertical"
25+
app:layout_constraintGuide_percent="0.05" />
26+
27+
<androidx.constraintlayout.widget.Guideline
28+
android:id="@+id/v_guide2"
29+
android:layout_width="wrap_content"
30+
android:layout_height="wrap_content"
31+
android:orientation="vertical"
32+
app:layout_constraintGuide_percent="0.95" />
33+
34+
</androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)