From b3b4b1025ecab31b8792fa87d8213bdc89068bbe Mon Sep 17 00:00:00 2001 From: leca Date: Mon, 7 Oct 2024 18:17:31 +0300 Subject: [PATCH] Added tabs --- app/build.gradle.kts | 2 +- .../ViewPagerAdapter.kt | 29 ++++++++ .../activities/MainActivity.kt | 73 +++++++++++++------ .../fragments/StorageFragment.kt | 22 +++++- .../views/AbstractProductView.kt | 3 + app/src/main/res/layout/activity_main.xml | 16 +++- 6 files changed, 118 insertions(+), 27 deletions(-) create mode 100644 app/src/main/java/org/foxarmy/barcodescannerforemployees/ViewPagerAdapter.kt diff --git a/app/build.gradle.kts b/app/build.gradle.kts index a429f80..674a5be 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -49,7 +49,7 @@ dependencies { androidTestImplementation(libs.androidx.junit) androidTestImplementation(libs.androidx.espresso.core) - implementation("com.google.android.material:1.2.0") +// implementation("com.google.android.material:1.2.0") // Barcode scanning API implementation (libs.barcode.scanning) diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/ViewPagerAdapter.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/ViewPagerAdapter.kt new file mode 100644 index 0000000..d907729 --- /dev/null +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/ViewPagerAdapter.kt @@ -0,0 +1,29 @@ +package org.foxarmy.barcodescannerforemployees + +import androidx.fragment.app.Fragment +import androidx.fragment.app.FragmentManager +import androidx.fragment.app.FragmentPagerAdapter + +class ViewPagerAdapter +public constructor(supportFragmentManager: FragmentManager) : FragmentPagerAdapter(supportFragmentManager) { + + private final var fragmentList1: ArrayList = ArrayList() + private final var fragmentTitleList1: ArrayList = ArrayList() + + public override fun getItem(position: Int): Fragment { + return fragmentList1.get(position) + } + + override fun getPageTitle(position: Int): CharSequence { + return fragmentTitleList1.get(position) + } + + override fun getCount(): Int { + return fragmentList1.size + } + + fun addFragment(fragment: Fragment, title: String) { + fragmentList1.add(fragment) + fragmentTitleList1.add(title) + } +} \ No newline at end of file diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/MainActivity.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/MainActivity.kt index 0a670a3..a5bf251 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/MainActivity.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/MainActivity.kt @@ -5,36 +5,39 @@ import android.os.Bundle import android.util.Log import android.view.Menu import android.view.MenuItem +import android.widget.ImageView import androidx.appcompat.app.AppCompatActivity import androidx.core.content.ContextCompat import androidx.core.view.children import androidx.gridlayout.widget.GridLayout -import androidx.navigation.findNavController -import androidx.navigation.ui.AppBarConfiguration -import androidx.navigation.ui.navigateUp -import androidx.navigation.ui.setupActionBarWithNavController +import androidx.viewpager.widget.ViewPager import org.foxarmy.barcodescannerforemployees.DBStorageController import org.foxarmy.barcodescannerforemployees.R -import org.foxarmy.barcodescannerforemployees.databinding.ActivityStorageBinding +import org.foxarmy.barcodescannerforemployees.ViewPagerAdapter +import org.foxarmy.barcodescannerforemployees.databinding.ActivityMainBinding +import org.foxarmy.barcodescannerforemployees.fragments.CategoriesFragment import org.foxarmy.barcodescannerforemployees.fragments.StorageFragment import org.foxarmy.barcodescannerforemployees.views.AbstractProductView class MainActivity : AppCompatActivity() { - private lateinit var appBarConfiguration: AppBarConfiguration - private lateinit var binding: ActivityStorageBinding +// private lateinit var appBarConfiguration: AppBarConfiguration + private lateinit var binding: ActivityMainBinding + public lateinit var adapter: ViewPagerAdapter override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - binding = ActivityStorageBinding.inflate(layoutInflater) + binding = ActivityMainBinding.inflate(layoutInflater) setContentView(binding.root) setSupportActionBar(binding.toolbar) + setupViewPager(binding.tabViewpager) + binding.tabTablayout.setupWithViewPager(binding.tabViewpager) - val navController = findNavController(R.id.nav_host_fragment_content_storage) - appBarConfiguration = AppBarConfiguration(navController.graph) - setupActionBarWithNavController(navController, appBarConfiguration) +// val navController = findNavController(R.id.nav_host_fragment_content_storage) +// appBarConfiguration = AppBarConfiguration(navController.graph) +// setupActionBarWithNavController(navController, appBarConfiguration) binding.addProductFab.setOnClickListener { view -> val addProductIntent = Intent(this, AddProductActivity::class.java) @@ -43,6 +46,17 @@ class MainActivity : AppCompatActivity() { } } + private fun setupViewPager(viewpager: ViewPager) { + adapter = ViewPagerAdapter(supportFragmentManager) + + adapter.addFragment(CategoriesFragment(), "Categories") + adapter.addFragment(StorageFragment(), "Storage") + //TODO: shelf and settings fragments + + // setting adapter to view pager. + viewpager.setAdapter(adapter) + } + override fun onCreateOptionsMenu(menu: Menu): Boolean { menuInflater.inflate(R.menu.menu_main, menu) return true @@ -55,26 +69,37 @@ class MainActivity : AppCompatActivity() { } R.id.action_delete -> { // if there's no selected items, write a toast about it, otherwise delete those items. - val storageFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment_content_storage)?.childFragmentManager!!.fragments[0] as StorageFragment - val amountOfViews = storageFragment.view?.findViewById(R.id.contentGridLayout)?.childCount + val currentPosition = binding.tabTablayout.selectedTabPosition + val fragment = adapter.getItem(currentPosition) + + when (fragment::class.simpleName.toString()) { + "StorageFragment" -> { + val storageFragment = fragment as StorageFragment + storageFragment.removeSelected() +// storageFragment.updateContent() - for (view: AbstractProductView in storageFragment.view?.findViewById(R.id.contentGridLayout)?.children!!.iterator() as Iterator) { - val db = DBStorageController(this) - if (view.isProductSelected) { - db.eraseAbstractProduct(db.writableDatabase , view.product.id, this) - Log.d("QWERTYUIOP", "Removing ${view.product.id}") } } - storageFragment.updateContent() +// val storageFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment_content_storage)?.childFragmentManager!!.fragments[0] as StorageFragment +// val amountOfViews = storageFragment.view?.findViewById(R.id.contentGridLayout)?.childCount +// +// for (view: AbstractProductView in storageFragment.view?.findViewById(R.id.contentGridLayout)?.children!!.iterator() as Iterator) { +// val db = DBStorageController(this) +// if (view.isProductSelected) { +// db.eraseAbstractProduct(db.writableDatabase , view.product.id, this) +// Log.d("QWERTYUIOP", "Removing ${view.product.id}") +// } +// } +// storageFragment.updateContent() true } else -> super.onOptionsItemSelected(item) } } - override fun onSupportNavigateUp(): Boolean { - val navController = findNavController(R.id.nav_host_fragment_content_storage) - return navController.navigateUp(appBarConfiguration) - || super.onSupportNavigateUp() - } +// override fun onSupportNavigateUp(): Boolean { +// val navController = findNavController(R.id.nav_host_fragment_content_storage) +// return navController.navigateUp(appBarConfiguration) +// || super.onSupportNavigateUp() +// } } \ No newline at end of file diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/StorageFragment.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/StorageFragment.kt index 76c4d82..0cf0fa0 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/StorageFragment.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/fragments/StorageFragment.kt @@ -2,9 +2,12 @@ package org.foxarmy.barcodescannerforemployees.fragments import android.os.Bundle import android.provider.BaseColumns +import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.widget.ImageView +import androidx.core.view.children import androidx.fragment.app.Fragment import androidx.gridlayout.widget.GridLayout import org.foxarmy.barcodescannerforemployees.* @@ -37,7 +40,24 @@ class StorageFragment : Fragment() { updateContent() } - public fun updateContent() { + fun removeSelected() { + val grv = getView()?.findViewById(R.id.contentGridLayout) + + val db = DBStorageController(requireContext()) + + for (view: AbstractProductView in grv?.children!!.iterator() as Iterator) { + view.findViewById(R.id.productPicture).setImageURI(null) + if (view.isProductSelected) { + db.eraseAbstractProduct(db.writableDatabase, view.product.id, requireContext()) + Log.d("QWERTYUIOP", "Removing ${view.product.id}") + } else { + Log.d("QWERTYUIOP", "Not ${view.product.id}") + } + } + updateContent() +} + + fun updateContent() { val grv = getView()?.findViewById(R.id.contentGridLayout) grv?.removeAllViews() diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/AbstractProductView.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/AbstractProductView.kt index 33234a2..cc56321 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/AbstractProductView.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/AbstractProductView.kt @@ -4,6 +4,7 @@ import android.app.Activity import android.content.Context import android.content.Intent import android.os.Bundle +import android.util.Log import android.view.LayoutInflater import android.widget.ImageView import android.widget.LinearLayout @@ -65,6 +66,8 @@ class AbstractProductView: LinearLayout { productLayout.setOnLongClickListener { isProductSelected = !isProductSelected + Log.d("QWERTYUIOP", "Changed to value $isProductSelected") + this.background = ContextCompat.getDrawable(context, if (isProductSelected) R.drawable.outline_selected else R.drawable.outline) true } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 3d96d00..ac34bc3 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -24,9 +24,23 @@ android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"/> + + - + +