From a3c7616ed4709f6a5f05ab6996710c99b692195d Mon Sep 17 00:00:00 2001 From: Julius Snipes Date: Sun, 21 Jan 2024 12:32:06 -0500 Subject: [PATCH] Dynamic Tabs work in progress --- .../labs/pennmobile/DynamicTabsFragment.kt | 40 +++++++++++++ .../pennapps/labs/pennmobile/HomeFragment.kt | 10 ++-- .../pennapps/labs/pennmobile/MainActivity.kt | 12 ++-- .../pennmobile/adapters/DynamicTabsAdapter.kt | 36 ++++++++++++ .../pennmobile/adapters/MainPagerAdapter.kt | 58 +++++++++++++------ .../pennapps/labs/pennmobile/classes/Tab.kt | 5 ++ .../more_fragments/PreferenceFragment.kt | 10 ++++ .../src/main/res/layout/dynamic_tab_item.xml | 34 +++++++++++ .../main/res/layout/fragment_dynamic_tabs.xml | 25 ++++++++ PennMobile/src/main/res/values/strings.xml | 2 + PennMobile/src/main/res/xml/settings.xml | 5 ++ 11 files changed, 209 insertions(+), 28 deletions(-) create mode 100644 PennMobile/src/main/java/com/pennapps/labs/pennmobile/DynamicTabsFragment.kt create mode 100644 PennMobile/src/main/java/com/pennapps/labs/pennmobile/adapters/DynamicTabsAdapter.kt create mode 100644 PennMobile/src/main/java/com/pennapps/labs/pennmobile/classes/Tab.kt create mode 100644 PennMobile/src/main/res/layout/dynamic_tab_item.xml create mode 100644 PennMobile/src/main/res/layout/fragment_dynamic_tabs.xml diff --git a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/DynamicTabsFragment.kt b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/DynamicTabsFragment.kt new file mode 100644 index 00000000..06d6e8ef --- /dev/null +++ b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/DynamicTabsFragment.kt @@ -0,0 +1,40 @@ +package com.pennapps.labs.pennmobile + +import android.os.Bundle +import androidx.fragment.app.Fragment +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.recyclerview.widget.ItemTouchHelper +import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView +import com.pennapps.labs.pennmobile.adapters.DynamicTabsAdapter +import com.pennapps.labs.pennmobile.classes.Tab +import kotlinx.android.synthetic.main.fragment_dynamic_tabs.view.* + + +class DynamicTabsFragment : Fragment() { + + lateinit var dynamicTabsAdapter: DynamicTabsAdapter + lateinit var tabs : List + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + } + + override fun onCreateView( + inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle? + ): View? { + // Inflate the layout for this fragment + val view = inflater.inflate(R.layout.fragment_dynamic_tabs, container, false) + tabs = listOf(Tab("Dining"), Tab("GSR"), Tab("Laundry")) +// val itemTouchHelper = object : ItemTouchHelper() { +// } + dynamicTabsAdapter = DynamicTabsAdapter(tabs) + view.rvDynamicTabs.adapter = dynamicTabsAdapter + view.rvDynamicTabs.layoutManager = LinearLayoutManager(context) + return view + } + +} \ No newline at end of file diff --git a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/HomeFragment.kt b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/HomeFragment.kt index 0855a245..ab5c1acd 100644 --- a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/HomeFragment.kt +++ b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/HomeFragment.kt @@ -130,10 +130,12 @@ class HomeFragment : Fragment() { Log.i("HomeFragment", "polls $loaded") if (loaded == totalCells) { - home_cells_rv?.adapter = HomeAdapter(ArrayList(homepageCells)) - loadingPanel?.visibility = View.GONE - internetConnectionHome?.visibility = View.GONE - home_refresh_layout?.isRefreshing = false + mActivity.runOnUiThread { + home_cells_rv?.adapter = HomeAdapter(ArrayList(homepageCells)) + loadingPanel?.visibility = View.GONE + internetConnectionHome?.visibility = View.GONE + home_refresh_layout?.isRefreshing = false + } } }, { throwable -> Log.e("Poll", "Error retrieving polls", throwable) diff --git a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/MainActivity.kt b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/MainActivity.kt index c4411cb8..7f0e634a 100644 --- a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/MainActivity.kt +++ b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/MainActivity.kt @@ -106,12 +106,12 @@ class MainActivity : AppCompatActivity() { private fun onExpandableBottomNavigationItemSelected() { expandable_bottom_bar.setOnNavigationItemSelectedListener { item -> val position = when (item.itemId) { - R.id.nav_home-> MainPagerAdapter.HOME_POSITION - R.id.nav_dining-> MainPagerAdapter.DINING_POSITION - R.id.nav_gsr-> MainPagerAdapter.GSR_POSITION - R.id.nav_laundry-> MainPagerAdapter.LAUNDRY_POSITION - R.id.nav_more-> MainPagerAdapter.MORE_POSITION - else -> MainPagerAdapter.HOME_POSITION + R.id.nav_home-> 0 + R.id.nav_dining-> 1 + R.id.nav_gsr-> 2 + R.id.nav_laundry-> 3 + R.id.nav_more-> 4 + else -> 0 } main_view_pager.setCurrentItem(position, false) true diff --git a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/adapters/DynamicTabsAdapter.kt b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/adapters/DynamicTabsAdapter.kt new file mode 100644 index 00000000..a11bc365 --- /dev/null +++ b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/adapters/DynamicTabsAdapter.kt @@ -0,0 +1,36 @@ +package com.pennapps.labs.pennmobile.adapters + +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.recyclerview.widget.RecyclerView +import com.pennapps.labs.pennmobile.R +import com.pennapps.labs.pennmobile.classes.Tab +import kotlinx.android.synthetic.main.dynamic_tab_item.view.* + +class DynamicTabsAdapter(private val tabs : List) : RecyclerView.Adapter() { + + class ViewHolder(val view: View) : RecyclerView.ViewHolder(view) { + + init { + // Define click listener for the ViewHolder's View + } + fun bindTab(tab: Tab) { + view.tvDynamicTab.text = tab.name + + } + } + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { + return ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.dynamic_tab_item, parent, false)) + } + + override fun getItemCount(): Int { + return tabs.size + } + + override fun onBindViewHolder(holder: ViewHolder, position: Int) { + holder.itemView + holder.bindTab(tabs[position]) + } +} \ No newline at end of file diff --git a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/adapters/MainPagerAdapter.kt b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/adapters/MainPagerAdapter.kt index a3877776..db0f7d79 100644 --- a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/adapters/MainPagerAdapter.kt +++ b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/adapters/MainPagerAdapter.kt @@ -4,20 +4,54 @@ import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentManager import androidx.lifecycle.Lifecycle import androidx.viewpager2.adapter.FragmentStateAdapter +import com.google.gson.Gson import com.pennapps.labs.pennmobile.DiningHolderFragment import com.pennapps.labs.pennmobile.GsrTabbedFragment import com.pennapps.labs.pennmobile.HomeFragment import com.pennapps.labs.pennmobile.LaundryFragment import com.pennapps.labs.pennmobile.more_fragments.MoreFragment +import java.util.* class MainPagerAdapter(fragmentManager: FragmentManager, lifecycle: Lifecycle?) : FragmentStateAdapter(fragmentManager, lifecycle!!) { + + companion object { + + const val HOME_ID = 0 + const val DINING_ID = 1 + const val GSR_ID = 2 + const val LAUNDRY_ID = 3 + const val MORE_ID = 4 + const val COUNT = 5 + fun updateMainList() { +// val gson = Gson() +// +// // below line is to get to string present from our +// // shared prefs if not present setting it as null. +// val json = sharedPreferences.getString("courses", null) +// +// // below line is to get the type of our array list. +// val type: Type = object : TypeToken?>() {}.type +// +// // in below line we are getting data from gson +// // and saving it to our array list +// courseList = gson.fromJson(json, type) as ArrayList + } + //val mainList : List = updateMainList() // [ HOME, DINING, GSR, LAUNDRY, FITNESS] + val mainList : List = listOf(DINING_ID, GSR_ID, LAUNDRY_ID) + } override fun createFragment(position: Int): Fragment { - return when(position) { - HOME_POSITION-> HomeFragment() - DINING_POSITION-> DiningHolderFragment() - GSR_POSITION-> GsrTabbedFragment() - LAUNDRY_POSITION-> LaundryFragment() - MORE_POSITION-> MoreFragment() + if(position == 0) { + return HomeFragment() + } + if(position == 4) { + return MoreFragment() + } + return when(mainList[position - 1]) { + HOME_ID-> HomeFragment() + DINING_ID-> DiningHolderFragment() + GSR_ID-> GsrTabbedFragment() + LAUNDRY_ID-> LaundryFragment() + MORE_ID-> MoreFragment() else -> HomeFragment() } } @@ -26,16 +60,4 @@ class MainPagerAdapter(fragmentManager: FragmentManager, lifecycle: Lifecycle?) return COUNT } - override fun getItemId(position: Int): Long { - return if (position < COUNT) position.toLong() else createFragment(position).hashCode().toLong() - } - - companion object { - const val HOME_POSITION = 0 - const val DINING_POSITION = 1 - const val GSR_POSITION = 2 - const val LAUNDRY_POSITION = 3 - const val MORE_POSITION = 4 - const val COUNT = 5 - } } diff --git a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/classes/Tab.kt b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/classes/Tab.kt new file mode 100644 index 00000000..5e6b494b --- /dev/null +++ b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/classes/Tab.kt @@ -0,0 +1,5 @@ +package com.pennapps.labs.pennmobile.classes + +class Tab(val name: String) { + +} \ No newline at end of file diff --git a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/more_fragments/PreferenceFragment.kt b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/more_fragments/PreferenceFragment.kt index fab96e5c..c0e203b9 100644 --- a/PennMobile/src/main/java/com/pennapps/labs/pennmobile/more_fragments/PreferenceFragment.kt +++ b/PennMobile/src/main/java/com/pennapps/labs/pennmobile/more_fragments/PreferenceFragment.kt @@ -110,6 +110,16 @@ class PreferenceFragment : PreferenceFragmentCompat() { return@setOnPreferenceClickListener true } + val selectPreferencePref: Preference? = findPreference("pref_account_preferences") + selectPreferencePref?.setOnPreferenceClickListener { + mActivity.supportFragmentManager.beginTransaction() + .replace(R.id.more_frame, DynamicTabsFragment()) + .addToBackStack(null) + .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN) + .commit() + return@setOnPreferenceClickListener true + } + val aboutFeaturePref: Preference? = findPreference("pref_about_feature") aboutFeaturePref?.setOnPreferenceClickListener { mActivity.supportFragmentManager.beginTransaction() diff --git a/PennMobile/src/main/res/layout/dynamic_tab_item.xml b/PennMobile/src/main/res/layout/dynamic_tab_item.xml new file mode 100644 index 00000000..a2885565 --- /dev/null +++ b/PennMobile/src/main/res/layout/dynamic_tab_item.xml @@ -0,0 +1,34 @@ + + + + + + + + \ No newline at end of file diff --git a/PennMobile/src/main/res/layout/fragment_dynamic_tabs.xml b/PennMobile/src/main/res/layout/fragment_dynamic_tabs.xml new file mode 100644 index 00000000..2d37952d --- /dev/null +++ b/PennMobile/src/main/res/layout/fragment_dynamic_tabs.xml @@ -0,0 +1,25 @@ + + + + + + + + \ No newline at end of file diff --git a/PennMobile/src/main/res/values/strings.xml b/PennMobile/src/main/res/values/strings.xml index e983a940..4a0aee9a 100644 --- a/PennMobile/src/main/res/values/strings.xml +++ b/PennMobile/src/main/res/values/strings.xml @@ -233,4 +233,6 @@ Mon - Fri Saturday Capacity + + Hello blank fragment diff --git a/PennMobile/src/main/res/xml/settings.xml b/PennMobile/src/main/res/xml/settings.xml index 28917359..554a5d63 100644 --- a/PennMobile/src/main/res/xml/settings.xml +++ b/PennMobile/src/main/res/xml/settings.xml @@ -13,6 +13,11 @@ android:summary="Modify your name and email" android:title="Edit your profile" app:iconSpaceReserved="false" /> +