hi ```when(args.approvalType) { ApprovalType.M...
# getting-started
a
hi
Copy code
when(args.approvalType) {
    ApprovalType.MULTI_SELECT_BATCH_APPROVAL.ordinal,
    ApprovalType.MULTI_SELECT_SINGLE_APPROVAL.ordinal -> {
        context?.apply {
            bgView.setBackgroundColor(getColor(R.color.sc_palette_green))
            subTitleTextView.text = getString(R.string.approvalSuccessLabelText)
        }
    }
    ApprovalType.MULTI_SELECT_SINGLE_APPROVAL.ordinal,
    ApprovalType.MULTI_SELECT_SINGLE_REJECT.ordinal -> {
        //TODO move to string resource
        titleLineTitleTextView.text = "List of Transactions"
    }
    ApprovalType.MULTI_SELECT_BATCH_APPROVAL.ordinal,
    ApprovalType.MULTI_SELECT_BATCH_REJECT.ordinal -> {
        //TODO move to string resource
        titleLineTitleTextView.text = "List of Batches"
    }
    ApprovalType.MULTI_SELECT_SINGLE_REJECT.ordinal,
    ApprovalType.MULTI_SELECT_BATCH_REJECT.ordinal -> {
        context?.apply {
            subTitleTextView.text = getString(R.string.rejectSuccessLabelText)
            bgView.setBackgroundColor(getColor(R.color.sc_palette_dark_blue))
        }
    }
}
Is this possible with when() to match two different blocks at the same time ? Here i need to match multiple blocks are the same time, in different context, to show the text "List of Transactions" and "List of Batches"
m
No, when statement will check the conditions in order and the first condition that is evaluated to true will be picked, so if
ApprovalType.MULTI_SELECT_SINGLE_REJECT.ordinal
is true, when will stop here and choose that path even if you have another branch (i.e
ApprovalType.MULTI_SELECT_BATCH_REJECT.ordinal
) that is true as well
r
wouldn't it be better to add
isApproval()
/
isReject()
and
isSingle()
/
isBatch()
to the enum, and then do something like this:
Copy code
when {
    approvalType.isApproval() -> doApprovalStuff()
    approvalType.isBatch() -> doBatchStuff()
}

when {
    approvalType.isSingle() -> doSingleStuff()
    approvalType.isBatch() -> doBatchStuff()
}
? with maybe some
else -> throw ...
or
else -> {}
added, depending on what you want. this is way more readable, and the logic is grouped together
r
Pattern matching when? 😢
a
In Java if we don't put break statement, it would match multiple blocks , if need. so was assuming kotlin when has this feature.
@Roukanken thank you, i have refactored the code these
Copy code
when {
    ApprovalType.isMultiSelectApproval(args.approvalType) -> {
        context?.apply {
            bgView.setBackgroundColor(getColor(R.color.sc_palette_green))
            subTitleTextView.text = getString(R.string.approvalSuccessLabelText)
        }
    }

    ApprovalType.isMultiSelectReject(args.approvalType) -> {
        context?.apply {
            subTitleTextView.text = getString(R.string.rejectSuccessLabelText)
            bgView.setBackgroundColor(getColor(R.color.sc_palette_dark_blue))
        }
    }
}

when {
    ApprovalType.isMultiSelectBatch(args.approvalType) -> {
        //TODO move to string resource
        titleLineTitleTextView.text = "List of Batches"
    }

    ApprovalType.isMultiSelectSingle(args.approvalType) -> {
        //TODO move to string resource
        titleLineTitleTextView.text = "List of Transactions"
    }
}
thank you @MR3Y, @Richard Gomez
Copy code
package com.alephlabs.clients.scb.s2b.ui.dashboard.approvals

enum class ApprovalType {

    SINGLE,
    BATCH,
    PAYEE,
    TEMPLATE,
    TRANSACTION,
    MULTI_SELECT_SINGLE_APPROVAL,
    MULTI_SELECT_BATCH_APPROVAL,
    MULTI_SELECT_SINGLE_REJECT,
    MULTI_SELECT_BATCH_REJECT;

    companion object {
        fun isMultiSelectApproval(ordinal: Int) =
            ordinal == MULTI_SELECT_BATCH_APPROVAL.ordinal
                    || ordinal == MULTI_SELECT_SINGLE_APPROVAL.ordinal

        fun isMultiSelectReject(ordinal: Int) =
            ordinal == MULTI_SELECT_BATCH_REJECT.ordinal
                    || ordinal == MULTI_SELECT_SINGLE_REJECT.ordinal

        fun isMultiSelectBatch(ordinal: Int) =
            ordinal == MULTI_SELECT_BATCH_REJECT.ordinal
                    || ordinal == MULTI_SELECT_BATCH_APPROVAL.ordinal

        fun isMultiSelectSingle(ordinal: Int) =
            ordinal == MULTI_SELECT_SINGLE_REJECT.ordinal
                    || ordinal == MULTI_SELECT_SINGLE_APPROVAL.ordinal

    }
}
r
even "not putting break in Java" would help help you here (please don't ever do that) if, your first code was in Java and you didn't have breaks, then for example passing
MULTI_SELECT_BATCH_APPROVAL
would get you executed all 4 branches, not just the 2 you want meaning you would have
"List of Batches"
and
R.string.rejectSuccessLabelText
set, which is definitely not
BATCH_APPROVAL
👍 1