1+ package ru.evotor.integrations
2+
3+ import android.os.Bundle
4+ import android.os.Message
5+
6+ /* *
7+ * Created by nixan on 01.03.17.
8+ */
9+
10+ sealed class Event (val eventId : Int ) {
11+
12+ open fun toMessage () = Message .obtain().apply {
13+ what = eventId
14+ data = Bundle ()
15+ }
16+
17+ sealed class Receipt (eventId : Int ) : Event(eventId) {
18+
19+ class Opened (val receiptId : Long ) : Receipt(EVENT_RECEIPT_OPENED ) {
20+ override fun toMessage () = super .toMessage().apply { data.putLong(" receiptId" , receiptId) }
21+ }
22+
23+ class ProductAdded (val receiptId : Long , val productUuid : String ) : Receipt(EVENT_RECEIPT_PRODUCT_ADDED ) {
24+ override fun toMessage () = super .toMessage().apply {
25+ data.putLong(" receiptId" , receiptId)
26+ data.putString(" productUuid" , productUuid)
27+ }
28+ }
29+
30+ class ProductRemoved (val receiptId : Long , val productUuid : String ) : Receipt(EVENT_RECEIPT_PRODUCT_REMOVED ) {
31+ override fun toMessage () = super .toMessage().apply {
32+ data.putLong(" receiptId" , receiptId)
33+ data.putString(" productUuid" , productUuid)
34+ }
35+ }
36+
37+ class Closed (val isSellOperation : Boolean ) : Receipt(EVENT_RECEIPT_CLOSED ) {
38+ override fun toMessage () = super .toMessage().apply { data.putBoolean(" isSellOperation" , isSellOperation) }
39+ }
40+
41+ class Deleted (val receiptId : Long ) : Receipt(EVENT_RECEIPT_DELETED ) {
42+ override fun toMessage () = super .toMessage().apply { data.putLong(" receiptId" , receiptId) }
43+ }
44+
45+ }
46+
47+ companion object {
48+
49+ const val EVENT_RECEIPT_OPENED = 100
50+ const val EVENT_RECEIPT_CLOSED = 101
51+ const val EVENT_RECEIPT_DELETED = 102
52+ const val EVENT_RECEIPT_PRODUCT_ADDED = 103
53+ const val EVENT_RECEIPT_PRODUCT_REMOVED = 104
54+
55+ fun fromMessage (message : Message ) = when (message.what) {
56+ EVENT_RECEIPT_OPENED -> Receipt .Opened (message.data.getLong(" receiptId" , 0 ))
57+ EVENT_RECEIPT_PRODUCT_ADDED -> Receipt .ProductAdded (message.data.getLong(" receiptId" , 0 ), message.data.getString(" productUuid" ))
58+ EVENT_RECEIPT_PRODUCT_REMOVED -> Receipt .ProductRemoved (message.data.getLong(" receiptId" , 0 ), message.data.getString(" productUuid" ))
59+ EVENT_RECEIPT_CLOSED -> Receipt .Closed (message.data.getBoolean(" isSellOperation" , false ))
60+ EVENT_RECEIPT_DELETED -> Receipt .Deleted (message.data.getLong(" receiptId" , 0 ))
61+ else -> throw UnknownEventException (" ${message.what} - ${message.data} is unknown" )
62+ }
63+ }
64+
65+ }
66+
67+ sealed class PluginEvent (val eventId : Int ) {
68+
69+ open fun toMessage () = Message .obtain().apply {
70+ what = eventId
71+ data = Bundle ()
72+ }
73+
74+ sealed class Payment (eventId : Int ) : PluginEvent(eventId) {
75+
76+ class Start : Payment (EVENT_PAYMENT_START )
77+ class ReadyToPrint : Payment (EVENT_PAYMENT_READY_TO_PRINT )
78+
79+ }
80+
81+ companion object {
82+
83+ const val EVENT_PAYMENT_START = 100
84+ const val EVENT_PAYMENT_READY_TO_PRINT = 101
85+
86+ fun fromMessage (message : Message ) = when (message.what) {
87+ EVENT_PAYMENT_START -> Payment .Start ()
88+ EVENT_PAYMENT_READY_TO_PRINT -> Payment .ReadyToPrint ()
89+ else -> throw UnknownEventException (" ${message.what} - ${message.data} is unknown" )
90+ }
91+ }
92+
93+ }
94+
95+ // class PluginEventWrapper(val pluginEvent: PluginEvent, val messenger: Messenger) {
96+ // fun toIntent() = pluginEvent.toIntent().apply { putExtra("callback", messenger) }
97+ //
98+ // companion object {
99+ // fun fromIntent(intent: Intent) = PluginEventWrapper(PluginEvent.fromIntent(intent), intent.getParcelableExtra("callback"))
100+ // }
101+ // }
102+
103+ class UnknownEventException (error : String ) : Exception(error)
0 commit comments