mirror of
https://github.com/oxen-io/session-android.git
synced 2024-11-23 18:15:22 +00:00
Merge pull request #604 from hjubb/fix_qr_scanner_offscreen
Only Activate QR Scanner Once On-Screen
This commit is contained in:
commit
de45ec584d
@ -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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user