|
| 1 | +package ru.evotor.framework.core.action.command.open_receipt_command; |
| 2 | + |
| 3 | +import android.content.ComponentName; |
| 4 | +import android.content.Context; |
| 5 | +import android.os.Bundle; |
| 6 | +import android.os.Handler; |
| 7 | +import android.os.Looper; |
| 8 | +import android.os.Parcelable; |
| 9 | + |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Objects; |
| 13 | + |
| 14 | +import ru.evotor.framework.Utils; |
| 15 | +import ru.evotor.framework.core.ICanStartActivity; |
| 16 | +import ru.evotor.framework.core.IntegrationManagerCallback; |
| 17 | +import ru.evotor.framework.core.IntegrationManagerImpl; |
| 18 | +import ru.evotor.framework.core.action.datamapper.ChangesMapper; |
| 19 | +import ru.evotor.framework.core.action.event.receipt.changes.IChange; |
| 20 | +import ru.evotor.framework.core.action.event.receipt.changes.position.PositionAdd; |
| 21 | + |
| 22 | +/** |
| 23 | + * Created by a.kuznetsov on 26/04/2017. |
| 24 | + */ |
| 25 | + |
| 26 | +public class OpenPaybackReceiptCommand { |
| 27 | + |
| 28 | + public static final String NAME = "evo.v2.receipt.payback.openReceipt"; |
| 29 | + private static final String KEY_CHANGES = "changes"; |
| 30 | + |
| 31 | + public static OpenPaybackReceiptCommand create(Bundle bundle) { |
| 32 | + Parcelable[] changesParcelable = bundle.getParcelableArray(KEY_CHANGES); |
| 33 | + return new OpenPaybackReceiptCommand(Utils.filterByClass( |
| 34 | + ChangesMapper.INSTANCE.create(changesParcelable), |
| 35 | + PositionAdd.class |
| 36 | + )); |
| 37 | + } |
| 38 | + |
| 39 | + private final List<PositionAdd> changes; |
| 40 | + |
| 41 | + public OpenPaybackReceiptCommand(List<PositionAdd> changes) { |
| 42 | + this.changes = new ArrayList<>(); |
| 43 | + if (changes != null) { |
| 44 | + this.changes.addAll(changes); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public void process(final Context context, final ICanStartActivity activityStarter, IntegrationManagerCallback callback) { |
| 49 | + Objects.requireNonNull(activityStarter); |
| 50 | + Objects.requireNonNull(context); |
| 51 | + |
| 52 | + List<ComponentName> componentNameList = IntegrationManagerImpl.convertImplicitIntentToExplicitIntent(NAME, context.getApplicationContext()); |
| 53 | + if (componentNameList == null || componentNameList.isEmpty()) { |
| 54 | + return; |
| 55 | + } |
| 56 | + new IntegrationManagerImpl(context.getApplicationContext()) |
| 57 | + .call(OpenPaybackReceiptCommand.NAME, |
| 58 | + |
| 59 | + componentNameList.get(0), |
| 60 | + this.toBundle(), |
| 61 | + activityStarter, |
| 62 | + callback, |
| 63 | + new Handler(Looper.getMainLooper()) |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + public Bundle toBundle() { |
| 68 | + Bundle bundle = new Bundle(); |
| 69 | + Parcelable[] changesParcelable = new Parcelable[changes.size()]; |
| 70 | + for (int i = 0; i < changesParcelable.length; i++) { |
| 71 | + IChange change = changes.get(i); |
| 72 | + changesParcelable[i] = ChangesMapper.INSTANCE.toBundle(change); |
| 73 | + } |
| 74 | + bundle.putParcelableArray(KEY_CHANGES, changesParcelable); |
| 75 | + return bundle; |
| 76 | + } |
| 77 | + |
| 78 | + public List<PositionAdd> getChanges() { |
| 79 | + return changes; |
| 80 | + } |
| 81 | +} |
0 commit comments