Created
November 19, 2019 02:16
-
-
Save BrantApps/2d0bef383840a94cda9a33b09b653acf to your computer and use it in GitHub Desktop.
Learning FP via arrow-kt. Could be right, could be wrong! 🤷
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MapView: Fragment(), MapReadyListener, LocationFoundListener { | |
@Inject lateinit var mappable: Mappable | |
@Inject lateinit var runtimePermissions: Set<String> | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
checkPermissions(requireActivity()) | |
} | |
private fun checkPermissions(parentActivity: Activity) { | |
runtimePermissions.map { permission -> | |
Either.cond( | |
checkSelfPermission(parentActivity, permission) != PackageManager.PERMISSION_GRANTED, | |
{ permission }, | |
{ permission } | |
).getOrElse{""} | |
}.filterNot { | |
it.isEmpty() | |
} pipe { permissionsToRequest -> | |
requestPermissions(permissionsToRequest.toTypedArray(), PERMISSION_REQUEST_CODE) | |
} | |
} | |
override fun onRequestPermissionsResult( | |
requestCode: Int, | |
permissions: Array<out String>, | |
grantResults: IntArray) { | |
super.onRequestPermissionsResult(requestCode, permissions, grantResults) | |
permissions.mapIndexed { index, requestedPermission -> | |
Either.cond( | |
grantResults[index] != PackageManager.PERMISSION_GRANTED, | |
{ when(requestedPermission) { | |
Manifest.permission.ACCESS_FINE_LOCATION -> getString(R.string.location_deny_nag_label) | |
Manifest.permission.WRITE_EXTERNAL_STORAGE -> getString(R.string.write_external_deny_nag_label) | |
else -> requestedPermission } | |
}, | |
{ mappable.refresh() } | |
).getOrElse{""} | |
}.filterNot { | |
it.isEmpty() | |
}.pipe { rejectedPermissionMessages -> | |
when(rejectedPermissionMessages.size) { | |
1 -> (activity as AbstractAppCompatActivity) | |
.showPermissionsSnackbar( | |
rejectedPermissionMessages.first(), | |
Snackbar.LENGTH_LONG, | |
getString(R.string.settings_label) | |
) | |
else -> (activity as AbstractAppCompatActivity) | |
.showPermissionsSnackbar( | |
getString(R.string.all_denied_permission_label), | |
Snackbar.LENGTH_INDEFINITE, | |
getString(R.string.settings_label) | |
) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment