From d9b91d6de8b2ef38f01f209cf931e210ec2e35af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Thu, 25 Mar 2021 17:47:02 +0100 Subject: [PATCH] [UI/Settings] Add requesting camera permissions in web push fragment. --- .../ui/modules/webpush/WebPushFragment.kt | 19 +++++-------------- app/src/main/res/values/strings.xml | 1 + 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/webpush/WebPushFragment.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/webpush/WebPushFragment.kt index 4c8c34cc..0996c518 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/webpush/WebPushFragment.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/webpush/WebPushFragment.kt @@ -4,15 +4,11 @@ package pl.szczodrzynski.edziennik.ui.modules.webpush -import android.Manifest import android.annotation.SuppressLint -import android.content.pm.PackageManager import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import androidx.core.app.ActivityCompat -import androidx.core.content.ContextCompat import androidx.fragment.app.Fragment import androidx.recyclerview.widget.LinearLayoutManager import kotlinx.coroutines.CoroutineScope @@ -25,7 +21,6 @@ import pl.szczodrzynski.edziennik.data.api.szkolny.response.WebPushResponse import pl.szczodrzynski.edziennik.databinding.WebPushFragmentBinding import pl.szczodrzynski.edziennik.ui.dialogs.QrScannerDialog import pl.szczodrzynski.edziennik.utils.SimpleDividerItemDecoration -import pl.szczodrzynski.edziennik.utils.Themes import kotlin.coroutines.CoroutineContext class WebPushFragment : Fragment(), CoroutineScope { @@ -46,13 +41,13 @@ class WebPushFragment : Fragment(), CoroutineScope { SzkolnyApi(app) } + private val manager + get() = app.permissionManager + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { activity = (getActivity() as MainActivity?) ?: return null context ?: return null app = activity.application as App - context!!.theme.applyStyle(Themes.appTheme, true) - if (app.profile == null) - return inflater.inflate(R.layout.fragment_loading, container, false) // activity, context and profile is valid b = WebPushFragmentBinding.inflate(inflater) return b.root @@ -60,19 +55,15 @@ class WebPushFragment : Fragment(), CoroutineScope { @SuppressLint("DefaultLocale") override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - // TODO check if app, activity, b can be null - if (app.profile == null || !isAdded) + if (!isAdded) return b.scanQrCode.onClick { - val result = ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA) - if (result == PackageManager.PERMISSION_GRANTED) { + manager.requestCameraPermission(activity, R.string.permissions_qr_scanner) { QrScannerDialog(activity, { b.tokenEditText.setText(it.crc32().toString(36).toUpperCase()) pairBrowser(browserId = it) }) - } else { - ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.CAMERA), 1) } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f4dfd1fc..a5f8621f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1398,4 +1398,5 @@ Nazwa profilu Synchronizuj ten profil Wyloguj się + Aby móc zeskanować kod QR musisz przyznać uprawnienia dostępu do kamery.\n\nKliknij OK, aby przyznać uprawnienia.