Added tabs

This commit is contained in:
leca 2024-10-07 18:17:31 +03:00
parent 47bd1ae46e
commit b3b4b1025e
6 changed files with 118 additions and 27 deletions

View File

@ -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)

View File

@ -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<Fragment> = ArrayList()
private final var fragmentTitleList1: ArrayList<String> = 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)
}
}

View File

@ -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<GridLayout>(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<GridLayout>(R.id.contentGridLayout)?.children!!.iterator() as Iterator<AbstractProductView>) {
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<GridLayout>(R.id.contentGridLayout)?.childCount
//
// for (view: AbstractProductView in storageFragment.view?.findViewById<GridLayout>(R.id.contentGridLayout)?.children!!.iterator() as Iterator<AbstractProductView>) {
// 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()
// }
}

View File

@ -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<GridLayout>(R.id.contentGridLayout)
val db = DBStorageController(requireContext())
for (view: AbstractProductView in grv?.children!!.iterator() as Iterator<AbstractProductView>) {
view.findViewById<ImageView>(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<GridLayout>(R.id.contentGridLayout)
grv?.removeAllViews()

View File

@ -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
}

View File

@ -24,9 +24,23 @@
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"/>
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="#FFF"
app:tabIndicatorHeight="3dp"
app:tabMode="fixed" />
</com.google.android.material.appbar.AppBarLayout>
<include layout="@layout/content_storage"/>
<!-- <include layout="@layout/content_storage"/>-->
<androidx.viewpager.widget.ViewPager
android:id="@+id/tab_viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/add_product_fab"