Creating a DSL

i currently have this class:

import android.app.Activity
import android.content.Context
import android.content.pm.PackageManager
import android.support.v4.app.ActivityCompat
import android.support.v4.content.ContextCompat
import android.util.Log

/**
 * Created by GHOST on 3/3/2018.
 */
class PermissionHandler {
    var permission: String? = null
    var activity: Activity? = null
    var context: Context? = activity?.baseContext
    var requestCode: Int? = 0
    var permissionExplaination: String? = null

    inline fun then(block: () -> Unit){
        // Here, thisActivity is the current activity
        if (ContextCompat.checkSelfPermission(context!!,
                permission ?: "")
                != PackageManager.PERMISSION_GRANTED) {

            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(activity!!,
                    permission ?: "")) {

                // Show an explanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.

            } else {

                // No explanation needed, we can request the permission.

                ActivityCompat.requestPermissions(activity!!,
                        arrayOf(permission),
                        requestCode!!)

                // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
                // app-defined int constant. The callback method gets the
                // result of the request.
            }
        } else {
            // Permission has already been granted
            block()
        }

    }

}
//and this function:
fun permissinaManager(block: PermissionHandler.() -> Unit) = block


//what m trying to do is:
permissinaManager {
    activity = this@MainActivity
    permission = Manifest.permission.SEND_SMS
    requestCode = REQUESTCODE
    then {
        Log.d("my library", "it's working")
    }
 }

and when i do it, the compiler shows no error but the code inside permissionManager is not executed for some reason

Well I would love to help you, but I can’t make out any of your code due to the formatting.

Try to put ``` at the start and the end of your code. And maybe also try to indent it properly. Otherwise reading that much code is nearly impossible.

1 Like

Take a look at this function of yours:

fun permissionManager(block: PermissionHandler.() -> Unit) = block

If you define the return type of this manually it would not be Unit as you might expect but instead be PermissionHandler.() -> Unit. The problem is that you don’t call block, but simply return it. What you would need to do is first get an instance of the permission handler and than call block on this handler like this:

val handler = getSomePermissionHandler()
handler.block()

If you want to ensure that there can only be one handler you could an Object Declaration instead.
Also if I understand your code correctly you always have to set activity, permission and requestCode so why not pass them into you permissionManager function as arguments and use the block directly for the when method. I feel this would make a nicer DSL but hey, thats just my personal preference :smiley:

1 Like

i will try it now