Skip to content

Commit

Permalink
feat: The Activity now notifies when the drawer status has changed (#…
Browse files Browse the repository at this point in the history
…4560)

* The Activity now notifies when the drawer status has changed
* Drawer status changes: now with multiple listeners
  • Loading branch information
g123k committed Apr 4, 2022
1 parent 525c11f commit ef12188
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ import openfoodfacts.github.scrachx.openfood.features.productlists.ProductListsA
import openfoodfacts.github.scrachx.openfood.features.scanhistory.ScanHistoryActivity
import openfoodfacts.github.scrachx.openfood.features.searchbycode.SearchByCodeFragment
import openfoodfacts.github.scrachx.openfood.features.shared.BaseActivity
import openfoodfacts.github.scrachx.openfood.features.shared.NavigationDrawerHost
import openfoodfacts.github.scrachx.openfood.features.shared.OnNavigationDrawerStatusChanged
import openfoodfacts.github.scrachx.openfood.images.ProductImage
import openfoodfacts.github.scrachx.openfood.jobs.ProductUploaderWorker.Companion.scheduleProductUpload
import openfoodfacts.github.scrachx.openfood.listeners.CommonBottomListenerInstaller.installBottomNavigation
Expand Down Expand Up @@ -127,7 +129,7 @@ import javax.inject.Inject
import openfoodfacts.github.scrachx.openfood.features.search.ProductSearchActivity.Companion.start as startSearch

@AndroidEntryPoint
class MainActivity : BaseActivity(), NavigationDrawerListener {
class MainActivity : BaseActivity(), NavigationDrawerListener, NavigationDrawerHost {
private var _binding: ActivityMainBinding? = null
private val binding get() = _binding!!

Expand Down Expand Up @@ -162,7 +164,7 @@ class MainActivity : BaseActivity(), NavigationDrawerListener {
private var userSettingsURI: Uri? = null

private var historySyncJob: Job? = null

private val drawerStatusChangedListeners = mutableSetOf<OnNavigationDrawerStatusChanged>()

private val loginThenUpdate = registerForActivityResult(LoginContract())
{ isLoggedIn -> if (isLoggedIn) updateConnectedState() }
Expand Down Expand Up @@ -335,8 +337,18 @@ class MainActivity : BaseActivity(), NavigationDrawerListener {

withOnDrawerListener(object : Drawer.OnDrawerListener {
override fun onDrawerSlide(drawerView: View, slideOffset: Float) = hideKeyboard()
override fun onDrawerOpened(drawerView: View) = hideKeyboard()
override fun onDrawerClosed(drawerView: View) = Unit
override fun onDrawerOpened(drawerView: View) {
hideKeyboard()
drawerStatusChangedListeners.forEach { listener ->
listener.onDrawerOpened()
}
}

override fun onDrawerClosed(drawerView: View) {
drawerStatusChangedListeners.forEach { listener ->
listener.onDrawerClosed()
}
}
})

addDrawerItems(
Expand Down Expand Up @@ -648,7 +660,6 @@ class MainActivity : BaseActivity(), NavigationDrawerListener {
menuInflater.inflate(R.menu.menu_main, menu)



// Associate searchable configuration with the SearchView
val searchManager = getSystemService(SEARCH_SERVICE) as SearchManager
searchMenuItem = menu.findItem(R.id.action_search).also { menuItem ->
Expand Down Expand Up @@ -782,6 +793,7 @@ class MainActivity : BaseActivity(), NavigationDrawerListener {
override fun onDestroy() {
customTabActivityHelper.connectionCallback = null
stopListeningToKeyboardVisibilityChanges()
drawerStatusChangedListeners.clear()
_binding = null
super.onDestroy()
}
Expand Down Expand Up @@ -965,7 +977,14 @@ class MainActivity : BaseActivity(), NavigationDrawerListener {
setNegativeButton(R.string.txtNo) { d, _ -> d.cancel() }
show()
}
}

override fun addOnDrawerStatusChanged(listener: OnNavigationDrawerStatusChanged) {
drawerStatusChangedListeners.add(listener)
}

override fun removeOnDrawerStatusChanged(listener: OnNavigationDrawerStatusChanged) {
drawerStatusChangedListeners.remove(listener)
}

companion object {
Expand All @@ -978,4 +997,4 @@ class MainActivity : BaseActivity(), NavigationDrawerListener {

fun start(context: Context) = context.startActivity(Intent(context, MainActivity::class.java))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ import dagger.hilt.android.AndroidEntryPoint
import openfoodfacts.github.scrachx.openfood.R
import openfoodfacts.github.scrachx.openfood.databinding.FragmentFindProductBinding
import openfoodfacts.github.scrachx.openfood.features.shared.NavigationBaseFragment
import openfoodfacts.github.scrachx.openfood.utils.NavigationDrawerListener
import openfoodfacts.github.scrachx.openfood.features.shared.NavigationWithDrawerBaseFragment
import openfoodfacts.github.scrachx.openfood.utils.*
import openfoodfacts.github.scrachx.openfood.utils.NavigationDrawerListener.NavigationDrawerType
import openfoodfacts.github.scrachx.openfood.utils.hideKeyboard
import openfoodfacts.github.scrachx.openfood.utils.isBarcodeValid
import openfoodfacts.github.scrachx.openfood.utils.isEmpty
import java.time.Duration
import java.time.temporal.ChronoUnit
import java.util.concurrent.TimeUnit
Expand All @@ -27,7 +25,7 @@ import kotlin.time.DurationUnit
* @see R.layout.fragment_find_product
*/
@AndroidEntryPoint
class SearchByCodeFragment : NavigationBaseFragment() {
class SearchByCodeFragment : NavigationWithDrawerBaseFragment() {
private var _binding: FragmentFindProductBinding? = null
private val binding get() = _binding!!

Expand Down Expand Up @@ -101,6 +99,16 @@ class SearchByCodeFragment : NavigationBaseFragment() {
(requireActivity() as AppCompatActivity).supportActionBar?.title = getString(R.string.search_by_barcode_drawer)
}

override fun onDrawerClosed() {
super.onDrawerClosed()

// Force the keyboard to be visible
if (binding.editTextBarcode.isEmpty()) {
binding.editTextBarcode.requestFocus()
requireActivity().showKeyboard()
}
}

companion object {
const val INTENT_KEY_BARCODE = "barcode"
fun newInstance(barcode: String? = null) = SearchByCodeFragment().apply {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package openfoodfacts.github.scrachx.openfood.features.shared

import android.content.Context

/**
* A custom [NavigationBaseFragment] that can be notified of navigation drawer events.
* The host (= Activity) must contain a Navigation Drawer and implements [NavigationDrawerHost]
*/
abstract class NavigationWithDrawerBaseFragment : NavigationBaseFragment(), OnNavigationDrawerStatusChanged {

override fun onAttach(context: Context) {
super.onAttach(context)

if (context is NavigationDrawerHost) {
context.addOnDrawerStatusChanged(this)
}
}

override fun onDetach() {
if (requireContext() is NavigationDrawerHost) {
(context as NavigationDrawerHost).removeOnDrawerStatusChanged(this)
}

super.onDetach()
}

override fun onDrawerOpened() {
// No implementation by default
}

override fun onDrawerClosed() {
// No implementation by default
}
}

/**
* Interface to implement for an Activity with a NavigationDrawer
*/
interface NavigationDrawerHost {
fun addOnDrawerStatusChanged(onDrawerStatusChanged: OnNavigationDrawerStatusChanged)
fun removeOnDrawerStatusChanged(onDrawerStatusChanged: OnNavigationDrawerStatusChanged)
}

/**
* Interface to notify when the status of a NavigationDrawer has changed (opened/closed)
*/
interface OnNavigationDrawerStatusChanged {
fun onDrawerOpened()
fun onDrawerClosed()
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ fun Activity.hideKeyboard() {
.hideSoftInputFromWindow(view.windowToken, 0)
}

fun Activity.showKeyboard() {
val view = currentFocus ?: return
(getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager)
.showSoftInput(view, 0)
}

fun Activity.getProductState() = intent.getSerializableExtra(ProductEditActivity.KEY_STATE) as ProductState?
fun Activity.requireProductState() = this.getProductState()
?: error("Activity ${this::class.simpleName} started without '${ProductEditActivity.KEY_STATE}' serializable in intent.")
Expand Down

0 comments on commit ef12188

Please sign in to comment.