Skip to content

Commit 86279c4

Browse files
author
Bene0202
committed
Final UI-Update Benedikt
1 parent 2c74b01 commit 86279c4

4 files changed

Lines changed: 193 additions & 0 deletions

File tree

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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.PlaceDetailsListOverview
7+
import com.nlinterface.fragments.PlaceDetailsScreen1
8+
import com.nlinterface.fragments.PlaceDetailsScreenBase
9+
10+
/**
11+
* Fragment Adapter for the Place Details Activity. On initialization only the fixed Fragments are
12+
* added to fragmentList.
13+
*/
14+
class PlaceDetailsFragmentAdapter (
15+
private val fragmentActivity: FragmentActivity
16+
): FragmentStateAdapter(fragmentActivity) {
17+
18+
val fragmentList = mutableListOf<Fragment>()
19+
20+
override fun getItemCount(): Int {
21+
return fragmentList.size
22+
}
23+
24+
init {
25+
fragmentList.add(PlaceDetailsListOverview())
26+
fragmentList.add(PlaceDetailsScreen1())
27+
}
28+
29+
override fun createFragment(position: Int): Fragment {
30+
return fragmentList[position]
31+
}
32+
33+
fun addFragment(fragment: PlaceDetailsScreenBase){
34+
fragmentList.add(fragment)
35+
}
36+
37+
/**
38+
* Removes a fragment and handles proper notification to all isntances.
39+
*/
40+
41+
fun removeFragment(fragment: PlaceDetailsScreenBase){
42+
val position = fragmentList.indexOf(fragment)
43+
fragmentList.removeAt(position)
44+
fragmentActivity.supportFragmentManager.beginTransaction().remove(fragment).commitNowAllowingStateLoss()
45+
notifyItemRemoved(position)
46+
notifyDataSetChanged()
47+
}
48+
49+
fun getCurrentFragment(position: Int): Fragment? {
50+
return fragmentList.getOrNull(position)
51+
}
52+
53+
override fun getItemId(position: Int): Long {
54+
// Return a unique ID for each fragment
55+
return fragmentList[position].hashCode().toLong()
56+
}
57+
58+
override fun containsItem(itemId: Long): Boolean {
59+
// Ensure that the item is still in the fragment list
60+
return fragmentList.any { it.hashCode().toLong() == itemId }
61+
}
62+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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.PlaceDetailsActivity
13+
import com.nlinterface.adapters.PlaceDetailsAdapter
14+
import com.nlinterface.dataclasses.PlaceDetailsItem
15+
import com.nlinterface.utility.SwipeAction
16+
import com.nlinterface.utility.SwipeNavigationListener
17+
import com.nlinterface.viewmodels.PlaceDetailsViewModel
18+
19+
class PlaceDetailsListOverview : Fragment(), SwipeAction {
20+
21+
lateinit var viewModel: PlaceDetailsViewModel
22+
private lateinit var adapter: PlaceDetailsAdapter
23+
private lateinit var placesList: ArrayList<PlaceDetailsItem>
24+
private lateinit var rvPlacesList: RecyclerView
25+
26+
/**
27+
* On Create View creates the layout and sets up the swipe Navigation. It also accesses
28+
* the grocery list
29+
* On ViewCreated accesses the shared viewmodel and displays the grocery list.
30+
*/
31+
override fun onCreateView(
32+
inflater: LayoutInflater, container: ViewGroup?,
33+
savedInstanceState: Bundle?
34+
): View? {
35+
// Inflate the layout for this fragment
36+
val view = inflater.inflate(R.layout.places_listview, container, false)
37+
view.setOnTouchListener(SwipeNavigationListener(requireContext(), this))
38+
rvPlacesList = view.findViewById<View>(R.id.places_list_rv) as RecyclerView
39+
viewModel = ViewModelProvider(this)[PlaceDetailsViewModel::class.java]
40+
41+
return view
42+
}
43+
44+
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
45+
super.onViewCreated(view, savedInstanceState)
46+
viewModel.fetchPlaceDetailsItemList()
47+
placesList = viewModel.placeDetailsItemList
48+
configureRecyclerView()
49+
}
50+
51+
override fun onResume() {
52+
super.onResume()
53+
viewModel.fetchPlaceDetailsItemList()
54+
placesList = viewModel.placeDetailsItemList
55+
configureRecyclerView()
56+
}
57+
58+
/**
59+
* Does not implement any swipe functionalities. This screen is a mere overview.
60+
*/
61+
62+
override fun onSwipeLeft() {}
63+
override fun onSwipeRight() {}
64+
override fun onSwipeUp() {}
65+
override fun onSwipeDown(){}
66+
override fun onDoubleTap() {}
67+
override fun onLongPress() {}
68+
69+
/**
70+
* Handles the RecyclerView and sets up necessary parameters for displaying the grocery list.
71+
*/
72+
private fun configureRecyclerView() {
73+
adapter = PlaceDetailsAdapter(placesList, activity as PlaceDetailsActivity)
74+
rvPlacesList.adapter = adapter
75+
rvPlacesList.layoutManager = LinearLayoutManager(activity as PlaceDetailsActivity)
76+
}
77+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:layout_width="match_parent"
3+
android:layout_height="wrap_content"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:orientation="vertical"
6+
android:padding="16dp"
7+
tools:ignore="MissingConstraints">
8+
9+
<TextView
10+
android:id="@+id/placeNameTextView"
11+
android:layout_width="wrap_content"
12+
android:layout_height="wrap_content"
13+
android:textStyle="bold"
14+
android:text="Place Name"/>
15+
16+
<TextView
17+
android:id="@+id/placeAddressTextView"
18+
android:layout_width="wrap_content"
19+
android:layout_height="wrap_content"
20+
android:text="Place Address"/>
21+
</LinearLayout>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
android:layout_width="match_parent"
5+
android:layout_height="match_parent"
6+
android:id="@+id/placeslist_listview">
7+
8+
<androidx.recyclerview.widget.RecyclerView
9+
android:id="@+id/places_list_rv"
10+
android:layout_width="0dp"
11+
android:layout_height="0dp"
12+
android:clipToPadding="false"
13+
android:overScrollMode="never"
14+
app:layout_constraintBottom_toBottomOf="parent"
15+
app:layout_constraintEnd_toEndOf="@id/v_guide2"
16+
app:layout_constraintStart_toStartOf="@id/v_guide1"
17+
app:layout_constraintTop_toTopOf="parent" />
18+
19+
<androidx.constraintlayout.widget.Guideline
20+
android:id="@+id/v_guide1"
21+
android:layout_width="wrap_content"
22+
android:layout_height="wrap_content"
23+
android:orientation="vertical"
24+
app:layout_constraintGuide_percent="0.05" />
25+
26+
<androidx.constraintlayout.widget.Guideline
27+
android:id="@+id/v_guide2"
28+
android:layout_width="wrap_content"
29+
android:layout_height="wrap_content"
30+
android:orientation="vertical"
31+
app:layout_constraintGuide_percent="0.95" />
32+
33+
</androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)