Skip to content

Commit

Permalink
Merge pull request #29 from sagar-viradiya/bugfix/sagar/issue#18
Browse files Browse the repository at this point in the history
Bug fix #18
  • Loading branch information
sagar-viradiya authored Dec 1, 2019
2 parents 6724cab + f198812 commit 3344934
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ buildscript {
common_version_code = 3
common_version_name = '2.0.1'

coroutinespermission_version_code = 3
coroutinespermission_version_name = "2.0.1"
coroutinespermission_version_code = 4
coroutinespermission_version_name = "2.0.2"

livedatapermission_version_code = 3
livedatapermission_version_name = '2.0.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,18 @@ import kotlinx.coroutines.withContext

/**
* Permission manager which handles checking permission is granted or not and if not then will request permission.
* This is nothing but a headless fragment which wraps the boilerplate code for checking and requesting permission
* A headless fragment which wraps the boilerplate code for checking and requesting permission
* and suspends the coroutines until result is available.
* A simple [Fragment] subclass.
*/
class PermissionManager : BasePermissionManager() {

private lateinit var completableDeferred: CompletableDeferred<PermissionResult>

override fun onPermissionResult(permissionResult: PermissionResult) {
completableDeferred.complete(permissionResult)
}

companion object {

private const val TAG = "PermissionManager"

/**
* A static factory method to request permission from activity.
* A static method to request permission from activity.
*
* @param activity an instance of [AppCompatActivity]
* @param requestId Request ID for permission request
Expand All @@ -68,7 +62,7 @@ class PermissionManager : BasePermissionManager() {
}

/**
* A static factory method to request permission from fragment.
* A static method to request permission from fragment.
*
* @param fragment an instance of [Fragment]
* @param requestId Request ID for permission request
Expand Down Expand Up @@ -124,9 +118,19 @@ class PermissionManager : BasePermissionManager() {
}
}

private lateinit var completableDeferred: CompletableDeferred<PermissionResult>

override fun onPermissionResult(permissionResult: PermissionResult) {
// When fragment gets recreated due to memory constraints by OS completableDeferred would be
// uninitialized and hence check
if (::completableDeferred.isInitialized) {
completableDeferred.complete(permissionResult)
}
}

override fun onDestroy() {
super.onDestroy()
if (completableDeferred.isActive) {
if (::completableDeferred.isInitialized && completableDeferred.isActive) {
completableDeferred.cancel()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.eazypermissions.common.model.PermissionResult
import com.eazypermissions.dsl.model.PermissionRequest

/**
* A headless fragment which wraps the boilerplate code for checking and requesting permission.
* A simple [Fragment] subclass.
*/
class PermissionManager : BasePermissionManager() {
Expand All @@ -48,7 +49,7 @@ class PermissionManager : BasePermissionManager() {
private const val TAG = "PermissionManager"

/**
* A static factory inline method to request permission for activity.
* A static inline method to request permission for activity.
*
* @param activity an instance of [AppCompatActivity]
* @param permissions vararg of all permissions for request
Expand Down Expand Up @@ -78,7 +79,7 @@ class PermissionManager : BasePermissionManager() {
}

/**
* A static factory inline method to request permission for fragment.
* A static inline method to request permission for fragment.
*
* @param fragment an instance of [Fragment]
* @param permissions vararg of all permissions for request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import com.eazypermissions.common.model.PermissionResult

/**
* Permission manager which handles checking permission is granted or not and if not then will request permission.
* This is nothing but a headless fragment which wraps the boilerplate code for checking and requesting permission
* A headless fragment which wraps the boilerplate code for checking and requesting permission
* and expose the result of permission request as [LiveData].
* A simple [Fragment] subclass.
*/
Expand Down

0 comments on commit 3344934

Please sign in to comment.