-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseActivity.kt
More file actions
1281 lines (1153 loc) · 41.5 KB
/
BaseActivity.kt
File metadata and controls
1281 lines (1153 loc) · 41.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.go.sport.base
import android.Manifest
import android.annotation.SuppressLint
import android.app.*
import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.content.pm.ActivityInfo
import android.graphics.*
import android.graphics.drawable.ColorDrawable
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.VibrationEffect
import android.os.Vibrator
import android.text.Html
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.util.Log
import android.util.TypedValue
import android.view.*
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import android.view.inputmethod.InputMethodManager
import android.widget.*
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.widget.NestedScrollView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.gdacciaro.iOSDialog.iOSDialogBuilder
import com.go.sport.R
import com.go.sport.constants.Constants
import com.go.sport.sharedpref.MySharedPreference
import com.go.sport.ui.walkthrough.WalkThroughActivity
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.tabs.TabLayout
import com.google.android.material.textfield.TextInputLayout
import com.google.gson.JsonParser
import com.jakewharton.rxbinding2.view.RxView
import com.karumi.dexter.Dexter
import com.karumi.dexter.MultiplePermissionsReport
import com.karumi.dexter.PermissionToken
import com.karumi.dexter.listener.PermissionRequest
import com.karumi.dexter.listener.multi.MultiplePermissionsListener
import com.skydoves.powermenu.PowerMenu
import com.skydoves.powermenu.PowerMenuItem
import es.dmoral.toasty.Toasty
import kotlinx.android.synthetic.main.bottom_sheet_are_you_sure.view.*
import okhttp3.MediaType
import okhttp3.RequestBody
import retrofit2.HttpException
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.text.DateFormat
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*
import java.util.concurrent.TimeUnit
import kotlin.collections.HashMap
open class BaseActivity : AppCompatActivity() {
companion object {
private const val TAG = "MyBase"
const val POPUPDISPLAY_MATCHCONT = 1
const val POPUPDISPLAY_WRAPCONT = 2
}
lateinit var areYouSureBottomSheet: BottomSheetDialog
interface OnBottomSheetDialogClickListener {
fun onDismissButtonClick()
}
fun getDateTime(s: String): String? {
var timeStamp = if (s.contains("."))
s.split(".")[0]
else
s
return try {
val sdf = SimpleDateFormat("MM/dd/yyyy")
val netDate = Date((timeStamp.toLong()) * 1000)
sdf.format(netDate)
} catch (e: Exception) {
e.toString()
}
}
fun getTime(s: String): String? {
var timeStamp = if (s.contains("."))
s.split(".")[0]
else
s
return try {
val sdf = SimpleDateFormat("h:mm a")
val netDate = Date((timeStamp.toLong()) * 1000)
sdf.format(netDate)
} catch (e: Exception) {
e.toString()
}
}
fun getConvoDateTime(s: String): String? {
var timeStamp = if (s.contains("."))
s.split(".")[0]
else
s
return try {
val sdf = SimpleDateFormat("dd-MMM-yyyy h:mm a")
val netDate = Date((timeStamp.toLong()) * 1000)
sdf.format(netDate)
} catch (e: Exception) {
e.toString()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
this.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
}
fun returnUserAuthToken(): HashMap<String, String> {
val auth = HashMap<String, String>()
auth["Authorization"] = MySharedPreference(this).getUserObject()?.token ?: ""
return auth
}
@SuppressLint("CheckResult")
fun initBottomSheet(
msgText: String,
btnText: String,
listener: OnBottomSheetDialogClickListener
) {
try {
areYouSureBottomSheet = BottomSheetDialog(this)
val view = LayoutInflater.from(this)
.inflate(R.layout.bottom_sheet_are_you_sure, null)
areYouSureBottomSheet.setContentView(view)
view.tv_msg.text = msgText
view.tv_btn.text = btnText
RxView.clicks(view.cont_delete_account).throttleFirst(2, TimeUnit.SECONDS).subscribe {
listener.onDismissButtonClick()
}
setupDialogBackground()
} catch (ex: NullPointerException) {
}
}
private fun setupDialogBackground() {
areYouSureBottomSheet.setOnShowListener(DialogInterface.OnShowListener { dialog ->
val d = dialog as BottomSheetDialog
val bottomSheet = d.findViewById<View>(R.id.design_bottom_sheet) as FrameLayout?
?: return@OnShowListener
bottomSheet.background = null
})
}
fun fullScreen() {
window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_FULLSCREEN
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_IMMERSIVE
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION)
}
fun openBrowser(url: String) {
var mUrl = url
if (!mUrl.contains("https://") || !mUrl.contains("http://"))
mUrl = "https://$mUrl"
val browserIntent =
Intent(
Intent.ACTION_VIEW,
Uri.parse(mUrl)
)
startActivity(browserIntent)
}
fun openWhatsApp(number: String) {
val url = "https://api.whatsapp.com/send?phone=$number"
val i = Intent(Intent.ACTION_VIEW)
i.data = Uri.parse(url)
startActivity(i)
}
/*fun openLoginActivity(
from: String,
shouldFinishAll: Boolean,
code: Int = 1,
isFinish: Boolean = false
) {
val b = Bundle()
b.putString("from", from)
if (shouldFinishAll)
startActivityFinishAll(
this,
LoginActivity::class.java,
false,
-1,
b
)
else
startActivity(
this,
LoginActivity::class.java,
isFinish,
code,
b
)
}*/
/*@SuppressLint("ClickableViewAccessibility")
fun makeViewSmallOnClicked(view: View) {
view.setOnTouchListener { _, event ->
Log.d(TAG, "setOnTouchListener")
if (event.action == MotionEvent.ACTION_DOWN) {
val x = 0.95.toFloat()
val y = 0.95.toFloat()
view.scaleX = x
view.scaleY = y
view.setBackgroundResource(R.color.green_1)
} else if (event.action == MotionEvent.ACTION_UP) {
val x = 1f
val y = 1f
view.scaleX = x
view.scaleY = y
view.setBackgroundResource(R.color.green_1)
}
false
}
}*/
/* fun getUserOrTemporaryId(completion: (String?) -> Unit) {
if (MySharedPreference(this).getUserObject() != null) {
MySharedPreference(this).getUserObject()?.user_id.let {
completion(it!!)
}
} else {
completion(MySharedPreference(this).getTemporaryId())
}
}*/
fun showDatePicker(
activity: Activity,
textView: TextView?,
format: String = "dd-MM-yyyy",
completion: (String?) -> Unit
) {
val c = Calendar.getInstance()
val year = c.get(Calendar.YEAR)
val month = c.get(Calendar.MONTH)
val day = c.get(Calendar.DAY_OF_MONTH)
val dpd =
activity.let {
DatePickerDialog(
it,
DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth ->
run {
textView?.text = formatDate(year, monthOfYear, dayOfMonth, format)
completion(formatDate(year, monthOfYear, dayOfMonth, format))
}
},
year,
month,
day
)
}
dpd.show()
}
fun showDateOfBirthPicker(
activity: Activity,
editText: EditText?,
format: String = "dd-MM-yyyy"
) {
val c = Calendar.getInstance()
val year = c.get(Calendar.YEAR) - 12
val month = c.get(Calendar.MONTH)
val day = c.get(Calendar.DAY_OF_MONTH)
val dpd =
activity.let {
DatePickerDialog(
it,
DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth ->
run {
val dob = formatDate(year, monthOfYear, dayOfMonth, format)
val sdf = SimpleDateFormat(format, Locale.getDefault())
val strDate = sdf.parse(dob)
if (System.currentTimeMillis() <= strDate.time)
warningToast("Please select date before current date")
else
editText?.setText(dob)
}
},
year,
month,
day
)
}
dpd.show()
}
/*fun showPassword(isShow: Boolean, editText: EditText, imageView: ImageView): Boolean {
val typeface = Typeface.createFromAsset(assets, Constants.MEDIUM)
return if (isShow) {
imageView.setImageResource(R.drawable.icon_show_password)
editText.inputType =
InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
editText.typeface = typeface
editText.setSelection(editText.text.length)
false
} else {
imageView.setImageResource(R.drawable.icon_hide_password)
editText.inputType =
InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_NORMAL
editText.typeface = typeface
editText.setSelection(editText.text.length)
true
}
}*/
fun formatDate(year: Int, month: Int, day: Int, format: String): String {
val myCalendar = Calendar.getInstance()
myCalendar.set(year, month, day)
val formatter = SimpleDateFormat(format, Locale.getDefault())
return formatter.format(myCalendar.time)
}
fun shareAppExternally(message: String) {
val shareIntent = Intent(Intent.ACTION_SEND)
shareIntent.type = "text/plain"
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "GoPlay")
var shareMessage = message
shareIntent.putExtra(Intent.EXTRA_TEXT, shareMessage)
startActivity(Intent.createChooser(shareIntent, "choose one"))
}
fun showTimePickerDialog(
activity: Activity,
textView: TextView,
completion: (time: Long) -> Unit
) {
val cal = Calendar.getInstance()
val timeSetListener = TimePickerDialog.OnTimeSetListener { timePicker, hour, minute ->
cal.set(Calendar.HOUR_OF_DAY, hour)
cal.set(Calendar.MINUTE, minute)
textView.text = SimpleDateFormat("hh:mm a").format(cal.time)
completion((cal.timeInMillis) / 1000 / 60)
}
TimePickerDialog(
activity,
timeSetListener,
cal.get(Calendar.HOUR_OF_DAY),
cal.get(Calendar.MINUTE),
false
).show()
}
open fun getURLForResource(resourceId: Int): String? {
//use BuildConfig.APPLICATION_ID instead of R.class.getPackage().getName() if both are not same
return Uri.parse(
"android.resource://" + R::class.java.getPackage()!!.name + "/" + resourceId
).toString()
}
fun callPhone(activity: Activity) {
Dexter.withActivity(activity)
.withPermissions(
Manifest.permission.CALL_PHONE
).withListener(object : MultiplePermissionsListener {
@SuppressLint("MissingPermission")
override fun onPermissionsChecked(report: MultiplePermissionsReport?) {
if (report!!.areAllPermissionsGranted()) {
val intent = Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "1122334455"))
startActivity(intent)
}
}
override fun onPermissionRationaleShouldBeShown(
permissions: MutableList<PermissionRequest>?,
token: PermissionToken?
) {
token!!.continuePermissionRequest()
}
}
).check()
}
open fun setFont(font: String, textView: TextView) {
val typeface = Typeface.createFromAsset(assets, font)
textView.typeface = typeface
}
open fun setFont(font: String, editText: EditText) {
val typeface = Typeface.createFromAsset(assets, font)
editText.typeface = typeface
}
open fun setFont(font: String, textInputLayout: TextInputLayout) {
val typeface = Typeface.createFromAsset(assets, font)
textInputLayout.typeface = typeface
}
open fun setFont(font: String, radioButton: RadioButton) {
val typeface = Typeface.createFromAsset(assets, font)
radioButton.typeface = typeface
}
open fun setFont(font: String, cb: CheckBox) {
val typeface = Typeface.createFromAsset(assets, font)
cb.typeface = typeface
}
/**
-1 id for LTR, 1 is for RTL, 0 is for fade, 2 id for pushinright,, 3 bottom to top, 4 top to bottom
*/
open fun startActivity(
context: Context,
activity: Class<*>,
isFinish: Boolean?,
code: Int
) {
//-1 id for LTR, 1 is for RTL, 0 is for fade
startActivity(Intent(context, activity))
when (code) {
0 -> (context as Activity).overridePendingTransition(R.anim.fadein, R.anim.fadeout)
1 -> (context as Activity).overridePendingTransition(
R.anim.slide_in_left,
R.anim.slide_out_left
)
-1 -> (context as Activity).overridePendingTransition(
R.anim.slide_in_right,
R.anim.slide_out_right
)
2 -> (context as Activity).overridePendingTransition(
R.anim.push_in_right,
R.anim.push_out_left
)
3 -> (context as Activity).overridePendingTransition(
R.anim.slide_in_bottom,
R.anim.slide_out_bottom
)
4 -> (context as Activity).overridePendingTransition(
R.anim.fadein_splash,
R.anim.fadeout_splash
)
}
if (isFinish!!)
(context as Activity).finish()
}
open fun startActivity(
context: Context,
activity: Class<*>,
isFinish: Boolean?,
code: Int,
bundle: Bundle
) {
//-1 id for LTR, 1 is for RTL, 0 is for fade
val intent = Intent(context, activity)
intent.putExtras(bundle)
/*for (i in 0 until keyvalue.size){
intent.putExtra(keyvalue[i].key,keyvalue[i].value)
}*/
startActivity(intent)
when (code) {
0 -> (context as Activity).overridePendingTransition(R.anim.fadein, R.anim.fadeout)
1 -> (context as Activity).overridePendingTransition(
R.anim.slide_in_left,
R.anim.slide_out_left
)
-1 -> (context as Activity).overridePendingTransition(
R.anim.slide_in_right,
R.anim.slide_out_right
)
2 -> (context as Activity).overridePendingTransition(
R.anim.push_in_right,
R.anim.push_out_left
)
3 -> (context as Activity).overridePendingTransition(
R.anim.slide_in_bottom,
R.anim.slide_out_bottom
)
}
if (isFinish!!)
(context as Activity).finish()
}
open fun startActivityForResult(
context: Context,
activity: Class<*>,
isFinish: Boolean?,
code: Int,
bundle: Bundle?,
requestCode: Int
) {
//-1 id for LTR, 1 is for RTL, 0 is for fade
val intent = Intent(context, activity)
bundle?.let { intent.putExtras(it) }
startActivityForResult(intent, requestCode)
when (code) {
0 -> (context as Activity).overridePendingTransition(R.anim.fadein, R.anim.fadeout)
1 -> (context as Activity).overridePendingTransition(
R.anim.slide_in_left,
R.anim.slide_out_left
)
-1 -> (context as Activity).overridePendingTransition(
R.anim.slide_in_right,
R.anim.slide_out_right
)
2 -> (context as Activity).overridePendingTransition(
R.anim.push_in_right,
R.anim.push_out_left
)
3 -> (context as Activity).overridePendingTransition(
R.anim.slide_in_bottom,
R.anim.slide_out_bottom
)
}
if (isFinish!!)
(context as Activity).finish()
}
@SuppressLint("CheckResult")
protected fun startActivityFinishAllWithRx(
view: View,
duration: Long,
context: Context,
activity: Class<*>,
isFinish: Boolean?,
code: Int = 1
) {
RxView.clicks(view)
.throttleFirst(duration, TimeUnit.SECONDS)
.subscribe {
startActivityFinishAll(context, activity, isFinish, code)
}
}
open fun startActivityFinishAll(
context: Context,
activity: Class<*>,
isFinish: Boolean?,
code: Int
) {
//-1 id for LTR, 1 is for RTL, 0 is for fade
startActivity(Intent(context, activity))
when (code) {
0 -> (context as Activity).overridePendingTransition(R.anim.fadein, R.anim.fadeout)
1 -> (context as Activity).overridePendingTransition(
R.anim.slide_in_left,
R.anim.slide_out_left
)
-1 -> (context as Activity).overridePendingTransition(
R.anim.slide_in_right,
R.anim.slide_out_right
)
2 -> (context as Activity).overridePendingTransition(
R.anim.push_in_right,
R.anim.push_out_left
)
3 -> (context as Activity).overridePendingTransition(
R.anim.slide_in_bottom,
R.anim.slide_out_bottom
)
}
if (isFinish!!)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
(context as Activity).finishAffinity()
}
}
open fun startActivityFinishAll(
context: Context,
activity: Class<*>,
isFinish: Boolean?,
code: Int,
bundle: Bundle
) {
//-1 id for LTR, 1 is for RTL, 0 is for fade
val intent = Intent(context, activity)
intent.putExtras(bundle)
/*for (i in 0 until keyvalue.size){
intent.putExtra(keyvalue[i].key,keyvalue[i].value)
}*/
startActivity(intent)
when (code) {
0 -> (context as Activity).overridePendingTransition(R.anim.fadein, R.anim.fadeout)
1 -> (context as Activity).overridePendingTransition(
R.anim.slide_in_left,
R.anim.slide_out_left
)
-1 -> (context as Activity).overridePendingTransition(
R.anim.slide_in_right,
R.anim.slide_out_right
)
2 -> (context as Activity).overridePendingTransition(
R.anim.push_in_right,
R.anim.push_out_left
)
3 -> (context as Activity).overridePendingTransition(
R.anim.slide_in_bottom,
R.anim.slide_out_bottom
)
}
if (isFinish!!)
(context as Activity).finishAffinity()
}
//1 top to bottom
open fun finish(
context: Context,
code: Int
) {
(context as Activity).finish()
if (code == 1) {
context.overridePendingTransition(
R.anim.slide_in_top,
R.anim.slide_out_top
)
} else if (code == -1) {
context.overridePendingTransition(
R.anim.slide_in_right,
R.anim.slide_out_right
)
}
}
@SuppressLint("CheckResult")
open fun finishWithRx(
view: View,
duration: Long,
context: Context,
code: Int = -1
) {
RxView.clicks(view)
.throttleFirst(duration, TimeUnit.SECONDS)
.subscribe {
finish(context, code)
}
}
override fun onBackPressed() {
finish(this, -1)
}
open fun finish(
context: Context,
code: Int,
resultCode: Int,
bundle: Bundle
) {
val intent = Intent()
intent.putExtras(bundle)
(context as Activity).setResult(resultCode, intent)
(context).finish()
if (code == 1) {
(context).overridePendingTransition(
R.anim.slide_in_top,
R.anim.slide_out_top
)
} else if (code == -1) {
(context).overridePendingTransition(
R.anim.slide_in_right,
R.anim.slide_out_right
)
}
}
@SuppressLint("CheckResult")
open fun startActivityWithRx(
view: View,
duration: Long,
context: Context,
activity: Class<*>,
isFinish: Boolean?,
code: Int = 1
) {
RxView.clicks(view)
.throttleFirst(duration, TimeUnit.SECONDS)
.subscribe {
startActivity(context, activity, isFinish, code)
}
}
protected fun changeTabsFont(context: Context, tablayout: TabLayout) {
val childTabLayout = tablayout.getChildAt(0) as ViewGroup
for (i in 0 until childTabLayout.childCount) {
val viewTab = childTabLayout.getChildAt(i) as ViewGroup
for (j in 0 until viewTab.childCount) {
val tabTextView = viewTab.getChildAt(j)
if (tabTextView is TextView) {
val typeface = Typeface.createFromAsset(context.assets, "TTNorms-Regular.otf")
tabTextView.typeface = typeface
tabTextView.setTextSize(
TypedValue.COMPLEX_UNIT_DIP,
12f
)
}
}
}
}
fun popupDisplay(
context: Context,
optionsCont: View,
tv: View?,
showAtTop: Boolean,
list: ArrayList<PowerMenuItem>,
width: Int? = POPUPDISPLAY_MATCHCONT,
index: Int,
completion: (String) -> Unit
) {
val powerMenu = PowerMenu.Builder(context)
.addItemList(list)
//.setAnimation(MenuAnimation.SHOWUP_TOP_RIGHT)
.setMenuRadius(20f)
.setMenuShadow(10f)
.setTextColor(ContextCompat.getColor(context, R.color.black))
.setTextGravity(Gravity.START)
.setTextSize(15)
.setBackgroundAlpha(0.05F)
.setTextTypeface(
Typeface.createFromAsset(
context.assets,
Constants.MEDIUM
)
)
.setDividerHeight(3)
.setDivider(
ColorDrawable(ContextCompat.getColor(context, R.color.grey))
)
.setSelectedTextColor(R.color.grey)
.setMenuColor(Color.WHITE)
.setSelectedMenuColor(ContextCompat.getColor(context, R.color.colorPrimary))
.build()
powerMenu.setOnMenuItemClickListener { i: Int, powerMenuItem: PowerMenuItem ->
if (tv is EditText)
tv.setText(powerMenuItem.title)
else if (tv is TextView)
tv.setText(powerMenuItem.title)
powerMenu.dismiss()
when (index) {
0 -> {
completion(powerMenuItem.title)
}
1 -> {
completion(powerMenuItem.title)
}
}
}
if (width == POPUPDISPLAY_MATCHCONT)
powerMenu.setWidth(optionsCont.measuredWidth)
if (showAtTop) {
powerMenu.showAsAnchorRightTop(optionsCont)
} else {
powerMenu.showAsDropDown(optionsCont)
}
}
open fun nameRegex(input: String): Boolean {
val regex = Regex("[a-zA-Z]+")
return input.matches(regex)
}
interface onDialogDone {
fun onDoneClicked(dialog: Dialog)
}
protected fun getScaledBitmap(
b: Bitmap,
reqWidth: Int,
reqHeight: Int
): Bitmap? {
val m = Matrix()
m.setRectToRect(
RectF(0f, 0f, b.width.toFloat(), b.height.toFloat()),
RectF(0f, 0f, reqWidth.toFloat(), reqHeight.toFloat()),
Matrix.ScaleToFit.CENTER
)
return Bitmap.createBitmap(b, 0, 0, b.width, b.height, m, true)
}
protected fun vibratePhone(context: Context, vibrateAmount: Long) {
val vibrator = context.getSystemService(VIBRATOR_SERVICE) as Vibrator
if (Build.VERSION.SDK_INT >= 26) {
vibrator.vibrate(
VibrationEffect.createOneShot(
vibrateAmount,
VibrationEffect.DEFAULT_AMPLITUDE
)
)
} else {
vibrator.vibrate(vibrateAmount)
}
}
fun openKeyboard(editText: EditText) {
val imm: InputMethodManager =
getSystemService(Service.INPUT_METHOD_SERVICE) as InputMethodManager
imm.showSoftInput(editText, 0);
}
fun closeKeyboard() {
// Check if no view has focus:
val view = currentFocus
if (view != null) {
val imm =
getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(view.windowToken, 0)
}
}
protected fun transparentStatusBar() {
window.setFlags(
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION
)
}
fun getStatusBarHeight(): Int {
var result = 0
val resourceId = resources.getIdentifier("status_bar_height", "dimen", "android")
if (resourceId > 0) {
result = resources.getDimensionPixelSize(resourceId)
}
Log.i(
"*** Elenasys :: ",
"Height is= $result"
)
return result
}
private var progressbar: AlertDialog? = null
fun pBar(showOrHide: Int) {
if (progressbar == null) {
progressbar =
AlertDialog.Builder(this).setView(R.layout.dialog_loader).setCancelable(false)
.create()
progressbar?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
}
if (progressbar != null)
if (showOrHide == 1) {
progressbar?.show()
} else if (showOrHide == 0) {
progressbar?.dismiss()
}
}
open fun getFileFromBitmap(bitmap: Bitmap, index: Int): File {
val f = File(cacheDir, "something+$index")
f.createNewFile()
val bos = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.PNG, 25 /*ignored for PNG*/, bos)
val bitmapdata = bos.toByteArray()
val fos = FileOutputStream(f)
fos.write(bitmapdata)
fos.flush()
fos.close()
return f
}
fun scrollNestedScrollToBottom(root: NestedScrollView) {
root.post { root.fullScroll(View.FOCUS_DOWN) }
}
open fun isLastItemDisplaying(recyclerView: RecyclerView): Boolean {
if (recyclerView.adapter != null && recyclerView.adapter!!.itemCount != 0) {
val lastItem =
(recyclerView.layoutManager as LinearLayoutManager?)!!.findLastCompletelyVisibleItemPosition()
if (lastItem != RecyclerView.NO_POSITION && lastItem == recyclerView.adapter!!.itemCount - 1) return true
}
return false
}
fun changeFormatTime(
currentFormat: String,
currentTime: String,
requiredFormat: String
): String {
return try {
val dateFormat = SimpleDateFormat(currentFormat);
var sourceDate: Date? = null;
try {
sourceDate = dateFormat.parse(currentTime);
} catch (e: ParseException) {
e.printStackTrace();
}
val targetFormat = SimpleDateFormat(requiredFormat)
targetFormat.format(sourceDate);
} catch (e: Exception) {
currentTime
}
}
@RequiresApi(Build.VERSION_CODES.N)
open fun parseDateToddMMyyyy(time: String): String? {
var datetime: String? = null
val inputFormat: DateFormat = SimpleDateFormat("dd-MMM-yyyy")
val d = SimpleDateFormat("yyyy-MM-dd")
try {
val convertedDate = inputFormat.parse(time)
datetime = d.format(convertedDate)
} catch (e: ParseException) {
}
return datetime
}
fun mOnError(error: Throwable) {
var message = ""
when (error) {
is HttpException -> {
// Kotlin will smart cast at this point
val errorJsonString = error.response()?.errorBody()?.string()
try {
message = JsonParser().parse(errorJsonString).asJsonObject["message"].asString
} catch (ex: Exception) {
Log.e("mOnError", "Exception: $ex")
}
}
is IOException -> {
//message = "No Internet Connection"
}
else -> {
message = if (error.message?.contains("Unable to resolve host") == true)
"No Internet Connection"
else
error.message.toString()
}
}
if (message != "")
errorToast(message)
}
fun getDateTimeFormatted(requiredFormat: String): String {
val dateFormat: DateFormat = SimpleDateFormat(requiredFormat)
val date = Date()
return dateFormat.format(date)
}
fun warningToast(message: String, length: Int = Toasty.LENGTH_LONG) {
/*val toast: Toast = Toasty.warning(this, message, length, true)
val toastLayout = toast.view as LinearLayout
val toastTV = toastLayout.getChildAt(1) as TextView
toastTV.typeface = Typeface.createFromAsset(assets, Constants.MEDIUM)
toastTV.textSize = 14f
toast.show()*/
mytoast(message)
}
fun errorToast(message: String, length: Int = Toasty.LENGTH_LONG) {
/* val toast: Toast = Toasty.error(this, message, length, true)
val toastLayout = toast.view as LinearLayout
val toastTV = toastLayout.getChildAt(1) as TextView
toastTV.typeface = Typeface.createFromAsset(assets, Constants.MEDIUM)
toastTV.textSize = 14f