Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added permission helper functions #13

Merged
merged 3 commits into from
Jan 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
@@ -1,3 1,7 @@
# v8.1.0
## New
- Added permission checker/requester helpers (`withPermission` and `withPermissions`) in BasePresenter class

# v8.0.0
## New
- Removed behaviour-based injection
Expand Down
4 changes: 2 additions & 2 deletions events/src/main/java/com/nerdscorner/mvplib/events/bus/Bus.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 25,7 @@ class Bus private constructor(private val eventBus: EventBus) {
}
} catch (exception: Exception) {
//No @Subscribe annotations detected
Log.e(TAG, exception.message)
Log.e(TAG, exception.message ?: "")
}
}

Expand All @@ -45,7 45,7 @@ class Bus private constructor(private val eventBus: EventBus) {
}
} catch (exception: Exception) {
//No @Subscribe annotations detected
Log.e(TAG, exception.message)
Log.e(TAG, exception.message ?: "")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 16,5 @@ open class BaseActivityPresenter<V : BaseActivityView, M : BaseEventsModel>

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {}

override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {}

override fun onRestoreInstanceState(savedInstanceState: Bundle?) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 7,30 @@ import android.os.Bundle
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import androidx.annotation.CallSuper
import androidx.annotation.IdRes
import androidx.core.app.ActivityCompat
import androidx.core.content.PermissionChecker
import androidx.fragment.app.Fragment
import com.nerdscorner.mvplib.events.model.BaseEventsModel
import com.nerdscorner.mvplib.events.utils.permissions.PermissionListener
import com.nerdscorner.mvplib.events.view.BaseView

abstract class BasePresenter<V : BaseView, M : BaseEventsModel>(
@JvmField protected var view: V,
@JvmField protected var model: M
) {

open fun onStart() {
}
private var currentRequestCode = 0x0100
private val permissionRequestListeners = hashMapOf<Int, PermissionListener>()

open fun onResume() {
}
open fun onStart() {}

open fun onPause() {
}
open fun onResume() {}

open fun onStop() {
}
open fun onPause() {}

open fun onStop() {}

open fun onDestroyView() {
view.unbind()
Expand All @@ -47,7 50,10 @@ abstract class BasePresenter<V : BaseView, M : BaseEventsModel>(

open fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {}

open fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {}
@CallSuper
open fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
permissionRequestListeners.remove(requestCode)?.onResult()
}

open fun onRestoreInstanceState(savedInstanceState: Bundle?) {}

Expand Down Expand Up @@ -82,4 88,58 @@ abstract class BasePresenter<V : BaseView, M : BaseEventsModel>(
}
}
}

fun withPermission(permission: String, onGranted: () -> Unit) {
withPermissions(arrayOf(permission), onGranted, {})
}

fun withPermission(permission: String, onGranted: () -> Unit, onDenied: (List<String>) -> Unit) {
withPermissions(arrayOf(permission), onGranted, onDenied)
}

fun withPermissions(permissions: Array<out String>, onGranted: () -> Unit) {
withPermissions(permissions, onGranted, {})
}

fun withPermissions(permissions: Array<out String>, onGranted: () -> Unit, onDenied: (List<String>) -> Unit) {
view.withActivity {
if (allPermissionsGranted(permissions)) {
onGranted()
} else {
permissionRequestListeners[currentRequestCode] = object : PermissionListener {
override fun onResult() {
if (allPermissionsGranted(permissions)) {
onGranted()
} else {
onDenied(getDeniedPermissions(permissions))
}
}
}
ActivityCompat.requestPermissions(this, permissions, currentRequestCode)
currentRequestCode
}
}
}

fun isPermissionGranted(name: String): Boolean {
return view.activity?.let {
PermissionChecker.checkSelfPermission(it, name) == PermissionChecker.PERMISSION_GRANTED
} ?: false
}

fun allPermissionsGranted(permissions: Array<out String>): Boolean {
return view.activity?.let { activity ->
permissions.all { permission ->
PermissionChecker.checkSelfPermission(activity, permission) == PermissionChecker.PERMISSION_GRANTED
}
} ?: false
}

fun getDeniedPermissions(permissions: Array<out String>): List<String> {
return view.activity?.let { activity ->
permissions.filter { permission ->
PermissionChecker.checkSelfPermission(activity, permission) != PermissionChecker.PERMISSION_GRANTED
}
} ?: emptyList()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 1,5 @@
package com.nerdscorner.mvplib.events.utils.permissions

interface PermissionListener {
fun onResult()
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 17,6 @@ org.gradle.jvmargs=-Xmx1536m
# org.gradle.parallel=true
#android.enableUnitTestBinaryResources=true

eventsLibVersion=8.0.1
eventsLibVersion=8.1.0
android.useAndroidX=true
android.enableJetifier=true
4 changes: 4 additions & 0 deletions testapp/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 3,9 @@
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nerdscorner.mvplib.testapp">

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
Expand All @@ -23,6 26,7 @@
</activity>
<activity android:name=".events.ui.activities.InheritanceEventsMainActivity"/>
<activity android:name=".events.ui.activities.WidgetsExampleActivity"/>
<activity android:name=".events.ui.activities.AttributeEventsForFragmentsMainActivity"/>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -1,6 1,5 @@
package com.nerdscorner.mvplib.testapp.events.ui.mvp.presenter

import androidx.fragment.app.Fragment
import com.nerdscorner.mvplib.events.bus.Bus
import com.nerdscorner.mvplib.events.presenter.BaseActivityPresenter
import com.nerdscorner.mvplib.testapp.events.ui.fragments.Fragment1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 1,7 @@
package com.nerdscorner.mvplib.testapp.events.ui.mvp.presenter

import android.Manifest
import android.widget.Toast
import com.nerdscorner.mvplib.events.bus.Bus
import com.nerdscorner.mvplib.events.presenter.BaseFragmentPresenter
import com.nerdscorner.mvplib.testapp.events.ui.mvp.model.Fragment1Model
Expand All @@ -12,6 14,16 @@ class Fragment1Presenter(view: Fragment1View, model: Fragment1Model, bus: Bus) :
BaseFragmentPresenter<Fragment1View, Fragment1Model>(view, model, bus) {
init {
view.setText("Fragment 1")

withPermission(
Manifest.permission.READ_EXTERNAL_STORAGE,
onGranted = {
Toast.makeText(view.context, "READ_EXTERNAL_STORAGE Granted!", Toast.LENGTH_SHORT).show()
},
onDenied = {
Toast.makeText(view.context, "READ_EXTERNAL_STORAGE Denied :(", Toast.LENGTH_SHORT).show()
}
)
}

@Subscribe
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 1,7 @@
package com.nerdscorner.mvplib.testapp.events.ui.mvp.presenter

import android.Manifest
import android.widget.Toast
import com.nerdscorner.mvplib.events.presenter.BaseActivityPresenter
import com.nerdscorner.mvplib.testapp.events.ui.mvp.model.InheritanceMainModel
import com.nerdscorner.mvplib.testapp.events.ui.mvp.model.InheritanceMainModel.BackgroundTaskCompletedEvent
Expand All @@ -10,6 12,18 @@ import org.greenrobot.eventbus.Subscribe
class InheritanceMainPresenter(view: InheritanceMainView, model: InheritanceMainModel) :
BaseActivityPresenter<InheritanceMainView, InheritanceMainModel>(view, model) {

init {
withPermissions(
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.READ_EXTERNAL_STORAGE),
onGranted = {
Toast.makeText(view.activity, "ACCESS_FINE_LOCATION and READ_EXTERNAL_STORAGE Granted!", Toast.LENGTH_SHORT).show()
},
onDenied = { list ->
Toast.makeText(view.activity, "Permissions ${list.joinToString()} Denied :(", Toast.LENGTH_SHORT).show()
}
)
}

@Subscribe
fun onActionClicked(event: ActionClickedEvent) {
view.setTextValue("Executing background task...")
Expand Down