|
| 1 | +/** |
| 2 | + * Copyright 2022 Shreyas Patil |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package dev.shreyaspatil.permissionFlow.example.ui.fragment |
| 17 | + |
| 18 | +import android.os.Bundle |
| 19 | +import android.view.LayoutInflater |
| 20 | +import android.view.View |
| 21 | +import android.view.ViewGroup |
| 22 | +import androidx.core.view.isVisible |
| 23 | +import androidx.fragment.app.Fragment |
| 24 | +import androidx.lifecycle.lifecycleScope |
| 25 | +import dev.shreyaspatil.permissionFlow.PermissionFlow |
| 26 | +import dev.shreyaspatil.permissionFlow.example.databinding.ViewFragmentExampleBinding |
| 27 | +import dev.shreyaspatil.permissionFlow.utils.registerForPermissionFlowRequestsResult |
| 28 | +import kotlinx.coroutines.launch |
| 29 | + |
| 30 | +class ExampleFragment : Fragment() { |
| 31 | + |
| 32 | + private var _binding: ViewFragmentExampleBinding? = null |
| 33 | + private val binding: ViewFragmentExampleBinding get() = _binding!! |
| 34 | + |
| 35 | + private val permissions = arrayOf( |
| 36 | + android.Manifest.permission.CAMERA, |
| 37 | + android.Manifest.permission.READ_CALL_LOG, |
| 38 | + android.Manifest.permission.READ_CONTACTS, |
| 39 | + android.Manifest.permission.READ_PHONE_STATE, |
| 40 | + ) |
| 41 | + |
| 42 | + private val permissionFlow = PermissionFlow.getInstance() |
| 43 | + |
| 44 | + private val permissionLauncher = registerForPermissionFlowRequestsResult() |
| 45 | + |
| 46 | + override fun onCreateView( |
| 47 | + inflater: LayoutInflater, |
| 48 | + container: ViewGroup?, |
| 49 | + savedInstanceState: Bundle?, |
| 50 | + ): View { |
| 51 | + _binding = ViewFragmentExampleBinding.inflate(inflater, null, false) |
| 52 | + return binding.root |
| 53 | + } |
| 54 | + |
| 55 | + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { |
| 56 | + super.onViewCreated(view, savedInstanceState) |
| 57 | + initView() |
| 58 | + observePermissions() |
| 59 | + } |
| 60 | + |
| 61 | + private fun initView() { |
| 62 | + binding.button.setOnClickListener { |
| 63 | + permissionLauncher.launch(permissions) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private fun observePermissions() { |
| 68 | + viewLifecycleOwner.lifecycleScope.launch { |
| 69 | + permissionFlow.getMultiplePermissionState(*permissions).collect { |
| 70 | + val grantedPermissionsText = it.grantedPermissions.joinToString( |
| 71 | + separator = "\n", |
| 72 | + prefix = "Granted Permissions:\n", |
| 73 | + ) |
| 74 | + val deniedPermissionsText = it.deniedPermissions.joinToString( |
| 75 | + separator = "\n", |
| 76 | + prefix = "Denied Permissions:\n", |
| 77 | + ) |
| 78 | + |
| 79 | + binding.grantedPermissionsText.text = grantedPermissionsText |
| 80 | + binding.deniedPermissionsText.text = deniedPermissionsText |
| 81 | + |
| 82 | + binding.button.isVisible = !it.allGranted |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments