@@ -31,74 +31,67 @@ import java.util.*
31
31
@Suppress(" unused" )
32
32
@SuppressLint(" SimpleDateFormat" )
33
33
class CustomTimestamps : Plugin () {
34
- init {
35
- settingsTab = SettingsTab (PluginSettings ::class .java).withArgs(settings)
36
- }
34
+ init {
35
+ settingsTab = SettingsTab (PluginSettings ::class .java).withArgs(settings)
36
+ }
37
37
38
- class PluginSettings (val settings : SettingsAPI ) : SettingsPage() {
39
- override fun onViewBound (view : View ) {
40
- super .onViewBound(view)
38
+ class PluginSettings (val settings : SettingsAPI ) : SettingsPage() {
39
+ override fun onViewBound (view : View ) {
40
+ super .onViewBound(view)
41
41
42
- setActionBarTitle(" CustomTimestamps" )
43
- setPadding(0 )
42
+ setActionBarTitle(" CustomTimestamps" )
43
+ setPadding(0 )
44
44
45
- val context = view.context
46
- val format = settings.getString(" format" , defaultFormat)
45
+ val context = view.context
46
+ val format = settings.getString(" format" , defaultFormat)
47
47
48
- val guide = TextView (context, null , 0 , R .h.UiKit_Settings_Item_SubText ).apply {
49
- setPreview(format, this )
50
- movementMethod = LinkMovementMethod .getInstance()
51
- }
52
- linearLayout.addView(TextInput (context).apply {
53
- val padding = DimenUtils .getDefaultPadding()
54
- layoutParams = LinearLayout .LayoutParams (LinearLayout .LayoutParams .MATCH_PARENT , LinearLayout .LayoutParams .WRAP_CONTENT )
55
- .apply { setMargins(padding, padding, padding, 0 ) }
56
- hint = " Custom Timestamp Format"
57
- editText.apply editText@ {
58
- if (this == null ) return @editText
59
- maxLines = 1
60
- setText(format)
61
- addTextChangedListener(object : TextWatcher {
62
- override fun afterTextChanged (s : Editable ) = s.toString().let {
63
- settings.setString(" format" , it)
64
- setPreview(it, guide)
48
+ val guide = TextView (context, null , 0 , R .i.UiKit_Settings_Item_SubText ).apply {
49
+ setPreview(format, this )
50
+ movementMethod = LinkMovementMethod .getInstance()
65
51
}
52
+ linearLayout.addView(TextInput (context, " Custom Timestamp Format" , format, object : TextWatcher {
53
+ override fun afterTextChanged (s : Editable ) = s.toString().let {
54
+ settings.setString(" format" , it)
55
+ setPreview(it, guide)
56
+ }
66
57
67
- override fun beforeTextChanged (s : CharSequence , start : Int , count : Int , after : Int ) {}
68
- override fun onTextChanged (s : CharSequence , start : Int , before : Int , count : Int ) {}
69
- })
58
+ override fun beforeTextChanged (s : CharSequence , start : Int , count : Int , after : Int ) {}
59
+ override fun onTextChanged (s : CharSequence , start : Int , before : Int , count : Int ) {}
60
+ }).apply {
61
+ val padding = DimenUtils .defaultPadding
62
+ layoutParams = LinearLayout .LayoutParams (LinearLayout .LayoutParams .MATCH_PARENT , LinearLayout .LayoutParams .WRAP_CONTENT )
63
+ .apply { setMargins(padding, padding, padding, 0 ) }
64
+ })
65
+ linearLayout.addView(guide)
70
66
}
71
- })
72
- linearLayout.addView(guide)
73
- }
74
67
75
- fun setPreview (formatStr : String , view : TextView ) {
76
- view.text = SpannableStringBuilder (" Formatting guide\n\n Preview: " ).apply {
77
- append(format(formatStr, System .currentTimeMillis()))
78
- setSpan(URLSpan (" https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html" ), 0 , 16 , Spanned .SPAN_EXCLUSIVE_EXCLUSIVE )
79
- }
68
+ private fun setPreview (formatStr : String , view : TextView ) {
69
+ view.text = SpannableStringBuilder (" Formatting guide\n\n Preview: " ).apply {
70
+ append(format(formatStr, System .currentTimeMillis()))
71
+ setSpan(URLSpan (" https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html" ), 0 , 16 , Spanned .SPAN_EXCLUSIVE_EXCLUSIVE )
72
+ }
73
+ }
80
74
}
81
- }
82
75
83
- override fun start (context : Context ? ) {
84
- patcher.patch(
85
- TimeUtils ::class .java.getDeclaredMethod(" toReadableTimeString" , Context ::class .java, Long ::class .javaPrimitiveType, Clock ::class .java),
86
- Hook { it.result = format(settings.getString(" format" , defaultFormat), it.args[1 ] as Long ) }
87
- )
88
- }
76
+ override fun start (context : Context ? ) {
77
+ patcher.patch(
78
+ TimeUtils ::class .java.getDeclaredMethod(" toReadableTimeString" , Context ::class .java, Long ::class .javaPrimitiveType, Clock ::class .java),
79
+ Hook { it.result = format(settings.getString(" format" , defaultFormat), it.args[1 ] as Long ) }
80
+ )
81
+ }
89
82
90
- override fun stop (context : Context ? ) = patcher.unpatchAll()
83
+ override fun stop (context : Context ? ) = patcher.unpatchAll()
91
84
92
- companion object {
93
- const val defaultFormat = " dd.MM.yyyy, HH:mm:ss"
85
+ companion object {
86
+ const val defaultFormat = " dd.MM.yyyy, HH:mm:ss"
94
87
95
- fun format (format : String? , time : Long ): String {
96
- return try {
97
- SimpleDateFormat (format).format(Date (time))
98
- } catch (e: Throwable ) {
99
- Main .logger.info(" Invalid format for CustomTimestamps, using default format" )
100
- SimpleDateFormat (defaultFormat).format(Date (time))
101
- }
88
+ fun format (format : String? , time : Long ): String {
89
+ return try {
90
+ SimpleDateFormat (format).format(Date (time))
91
+ } catch (e: Throwable ) {
92
+ Main .logger.info(" Invalid format for CustomTimestamps, using default format" )
93
+ SimpleDateFormat (defaultFormat).format(Date (time))
94
+ }
95
+ }
102
96
}
103
- }
104
97
}
0 commit comments