Merge pull request #604 from hjubb/fix_qr_scanner_offscreen

Only Activate QR Scanner Once On-Screen
This commit is contained in:
Niels Andriesse 2021-06-16 15:07:10 +10:00 committed by GitHub
commit de45ec584d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,18 +3,36 @@ package org.thoughtcrime.securesms.loki.fragments
import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.core.content.ContextCompat
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import com.tbruyelle.rxpermissions2.RxPermissions
import network.loki.messenger.R
import org.thoughtcrime.securesms.qr.ScanListener
class ScanQRCodeWrapperFragment : Fragment(), ScanQRCodePlaceholderFragmentDelegate, ScanListener {
companion object {
const val FRAGMENT_TAG = "ScanQRCodeWrapperFragment_FRAGMENT_TAG"
}
var delegate: ScanQRCodeWrapperFragmentDelegate? = null
var message: CharSequence = ""
var enabled: Boolean = true
set(value) {
val shouldUpdate = field != value // update if value changes (view appears or disappears)
field = value
if (shouldUpdate) {
update()
}
}
override fun setUserVisibleHint(isVisibleToUser: Boolean) {
super.setUserVisibleHint(isVisibleToUser)
enabled = isVisibleToUser
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_scan_qr_code_wrapper, container, false)
@ -26,7 +44,17 @@ class ScanQRCodeWrapperFragment : Fragment(), ScanQRCodePlaceholderFragmentDeleg
}
private fun update() {
if (!this.isAdded) return
val fragment: Fragment
if (!enabled) {
val manager = childFragmentManager
manager.findFragmentByTag(FRAGMENT_TAG)?.let { existingFragment ->
// remove existing camera fragment (if switching back to other page)
manager.beginTransaction().remove(existingFragment).commit()
}
return
}
if (ContextCompat.checkSelfPermission(requireActivity(), Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
val scanQRCodeFragment = ScanQRCodeFragment()
scanQRCodeFragment.scanListener = this
@ -38,7 +66,7 @@ class ScanQRCodeWrapperFragment : Fragment(), ScanQRCodePlaceholderFragmentDeleg
fragment = scanQRCodePlaceholderFragment
}
val transaction = childFragmentManager.beginTransaction()
transaction.replace(R.id.fragmentContainer, fragment)
transaction.replace(R.id.fragmentContainer, fragment, FRAGMENT_TAG)
transaction.commit()
}
@ -59,6 +87,5 @@ class ScanQRCodeWrapperFragment : Fragment(), ScanQRCodePlaceholderFragmentDeleg
}
interface ScanQRCodeWrapperFragmentDelegate {
fun handleQRCodeScanned(string: String)
}