8
8
* LICENSE file present in the project repository where this source code is maintained.
9
9
*/
10
10
11
- package org.readium.r2.testapp
11
+ package org.readium.r2.testapp.drm
12
12
13
13
import android.app.Activity
14
14
import android.content.Intent
15
- import android.graphics.Color
16
15
import android.graphics.Typeface
17
16
import android.os.Bundle
18
17
import android.view.Gravity
19
- import android.view.View
20
- import android.view.ViewGroup
21
- import android.widget.ArrayAdapter
22
18
import android.widget.LinearLayout
23
- import android.widget.Spinner
24
19
import android.widget.TextView
25
20
import androidx.appcompat.app.AlertDialog
26
21
import androidx.appcompat.app.AppCompatActivity
27
22
import com.mcxiaoke.koi.ext.onClick
28
23
import kotlinx.coroutines.CoroutineScope
29
24
import kotlinx.coroutines.Dispatchers
30
25
import kotlinx.coroutines.launch
26
+ import kotlinx.coroutines.runBlocking
31
27
import org.jetbrains.anko.*
32
28
import org.jetbrains.anko.appcompat.v7.Appcompat
33
29
import org.jetbrains.anko.design.coordinatorLayout
34
30
import org.jetbrains.anko.design.longSnackbar
35
31
import org.joda.time.DateTime
36
32
import org.joda.time.format.DateTimeFormat
37
33
import org.readium.r2.shared.UserException
38
- import org.readium.r2.testapp.utils.extensions.color
34
+ import org.readium.r2.testapp.R
39
35
import java.io.File
40
36
import kotlin.coroutines.CoroutineContext
41
37
@@ -57,37 +53,11 @@ class DRMManagementActivity : AppCompatActivity(), CoroutineScope {
57
53
val pubPath = intent.getStringExtra(" publication" )
58
54
? : throw Exception (" publication required" )
59
55
60
- drmModel = LCPViewModel (File (pubPath), this )
61
-
62
- val daysArray = arrayOf(1 , 3 , 7 , 15 )
63
-
64
- val daysInput = Spinner (this @DRMManagementActivity)
65
- daysInput.dropDownWidth = wrapContent
66
-
67
- val adapter = object : ArrayAdapter <Int >(this @DRMManagementActivity, R .layout.item_spinner_days, daysArray) {
68
- override fun getDropDownView (position : Int , convertView : View ? , parent : ViewGroup ): View {
69
- val v: View ? = super .getDropDownView(position, null , parent)
70
- // Makes the selected font appear in dark
71
- // If this is the selected item position
72
- if (position == daysInput.selectedItemPosition) {
73
- v!! .setBackgroundColor(context.color(R .color.colorPrimaryDark))
74
- v.findViewById<TextView >(R .id.days_spinner).setTextColor(Color .WHITE )
75
- } else {
76
- // for other views
77
- v!! .setBackgroundColor(Color .WHITE )
78
- v.findViewById<TextView >(R .id.days_spinner).setTextColor(Color .BLACK )
79
- }
80
- return v
56
+ drmModel = runBlocking { LCPViewModel .from(File (pubPath), this @DRMManagementActivity) }
57
+ ? : run {
58
+ finish()
59
+ return
81
60
}
82
- }
83
-
84
- daysInput.adapter = adapter
85
- val renewDialog = alert(Appcompat , " How many days do you wish to extend your loan ?" ) {
86
- this .customView = daysInput
87
- daysInput.setSelection(2 )
88
- positiveButton(" Renew" ) { }
89
- negativeButton(" Cancel" ) { }
90
- }.build()
91
61
92
62
coordinatorLayout {
93
63
fitsSystemWindows = true
@@ -169,7 +139,7 @@ class DRMManagementActivity : AppCompatActivity(), CoroutineScope {
169
139
}.lparams(width = wrapContent, height = wrapContent, weight = 1f )
170
140
textView {
171
141
padding = dip(10 )
172
- text = drmModel.issued?.toString(DateTimeFormat .shortDateTime())
142
+ text = drmModel.issued?.let { DateTime (it) }?. toString(DateTimeFormat .shortDateTime())
173
143
textSize = 18f
174
144
gravity = Gravity .END
175
145
}.lparams(width = wrapContent, height = wrapContent, weight = 1f )
@@ -185,7 +155,7 @@ class DRMManagementActivity : AppCompatActivity(), CoroutineScope {
185
155
}.lparams(width = wrapContent, height = wrapContent, weight = 1f )
186
156
textView {
187
157
padding = dip(10 )
188
- text = drmModel.updated?.toString(DateTimeFormat .shortDateTime())
158
+ text = drmModel.updated?.let { DateTime (it) }?. toString(DateTimeFormat .shortDateTime())
189
159
textSize = 18f
190
160
gravity = Gravity .END
191
161
}.lparams(width = wrapContent, height = wrapContent, weight = 1f )
@@ -244,7 +214,7 @@ class DRMManagementActivity : AppCompatActivity(), CoroutineScope {
244
214
}.lparams(width = wrapContent, height = wrapContent, weight = 1f )
245
215
textView {
246
216
padding = dip(10 )
247
- text = drmModel.start?.toString(DateTimeFormat .shortDateTime())
217
+ text = drmModel.start?.let { DateTime (it) }?. toString(DateTimeFormat .shortDateTime())
248
218
textSize = 18f
249
219
gravity = Gravity .END
250
220
}.lparams(width = wrapContent, height = wrapContent, weight = 1f )
@@ -261,7 +231,7 @@ class DRMManagementActivity : AppCompatActivity(), CoroutineScope {
261
231
262
232
endTextView = textView {
263
233
padding = dip(10 )
264
- text = drmModel.end?.toString(DateTimeFormat .shortDateTime())
234
+ text = drmModel.end?.let { DateTime (it) }?. toString(DateTimeFormat .shortDateTime())
265
235
textSize = 18f
266
236
gravity = Gravity .END
267
237
}.lparams(width = wrapContent, height = wrapContent, weight = 1f )
@@ -277,32 +247,16 @@ class DRMManagementActivity : AppCompatActivity(), CoroutineScope {
277
247
button {
278
248
text = context.getString(R .string.drm_label_renew)
279
249
onClick {
280
- renewDialog.apply {
281
- setCancelable(false )
282
- setCanceledOnTouchOutside(false )
283
- setOnShowListener {
284
- val renewButton = getButton(AlertDialog .BUTTON_POSITIVE )
285
- renewButton.setOnClickListener {
286
-
287
- val addDays = daysInput.selectedItem.toString().toInt()
288
- val newEndDate = DateTime (drmModel.end).plusDays(addDays)
289
-
290
- launch {
291
- drmModel.renewLoan(newEndDate)
292
- .onSuccess {
293
- dismiss()
294
- endTextView.text =
295
- newEndDate?.toString(DateTimeFormat .shortDateTime())
296
- }.onFailure { exception ->
297
- dismiss()
298
- (exception as ? UserException )?.getUserMessage(this @DRMManagementActivity)
299
- ?.let { longSnackbar(it) }
300
- }
301
- }
250
+ launch {
251
+ drmModel.renewLoan()
252
+ .onSuccess { newDate ->
253
+ endTextView.text = newDate?.let { DateTime (it).toString(DateTimeFormat .shortDateTime()) }
254
+ }.onFailure { exception ->
255
+ (exception as ? UserException )?.getUserMessage(this @DRMManagementActivity)
256
+ ?.let { longSnackbar(it) }
302
257
}
303
- }
304
258
}
305
- renewDialog.show()
259
+
306
260
}
307
261
}.lparams(width = matchParent, height = wrapContent, weight = 1f )
308
262
}
0 commit comments