1
+ /*
2
+ * Copyright 2022 Stax
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ package com.hover.stax.core
17
+
18
+ import android.content.Context
19
+ import android.content.SharedPreferences
20
+ import java.text.DecimalFormat
21
+
22
+ object Utils {
23
+
24
+ private const val SHARED_PREFS = " staxprefs"
25
+ private const val SDK_PREFS = " _hoversdk"
26
+
27
+ private fun getSharedPrefs (context : Context ): SharedPreferences {
28
+ return context.getSharedPreferences(
29
+ getPackage(context) + SHARED_PREFS ,
30
+ Context .MODE_PRIVATE
31
+ )
32
+ }
33
+
34
+ fun getSdkPrefs (context : Context ): SharedPreferences {
35
+ return context.getSharedPreferences(getPackage(context) + SDK_PREFS , Context .MODE_PRIVATE )
36
+ }
37
+
38
+ fun saveString (key : String? , value : String? , c : Context ) {
39
+ val editor = getSharedPrefs(c).edit()
40
+ editor.putString(key, value)
41
+ editor.apply ()
42
+ }
43
+
44
+ fun getString (key : String? , c : Context ): String? {
45
+ return getSharedPrefs(c).getString(key, " " )
46
+ }
47
+
48
+ fun removeString (key : String , context : Context ) {
49
+ getSharedPrefs(context).edit().apply {
50
+ remove(key)
51
+ apply ()
52
+ }
53
+ }
54
+
55
+ fun getBoolean (key : String? , c : Context , returnTrueDefault : Boolean = false): Boolean {
56
+ return getSharedPrefs(c).getBoolean(key, returnTrueDefault)
57
+ }
58
+
59
+ fun saveInt (key : String? , value : Int , c : Context ) {
60
+ val editor = getSharedPrefs(c).edit()
61
+ editor.putInt(key, value)
62
+ editor.apply ()
63
+ }
64
+
65
+ fun saveBoolean (key : String? , value : Boolean , c : Context ) {
66
+ val editor = getSharedPrefs(c).edit()
67
+ editor.putBoolean(key, value)
68
+ editor.apply ()
69
+ }
70
+
71
+ fun getInt (key : String? , c : Context ? ): Int {
72
+ return getSharedPrefs(c!! ).getInt(key, 0 )
73
+ }
74
+
75
+ fun getLong (key : String? , c : Context ? ): Long {
76
+ return getSharedPrefs(c!! ).getLong(key, 0 )
77
+ }
78
+
79
+ fun saveLong (key : String? , value : Long , c : Context ? ) {
80
+ val editor = getSharedPrefs(c!! ).edit()
81
+ editor.putLong(key, value)
82
+ editor.apply ()
83
+ }
84
+
85
+ fun isFirebaseTopicInDefaultState (topic : String? , c : Context ): Boolean {
86
+ return getSharedPrefs(c).getBoolean(topic, true )
87
+ }
88
+
89
+ fun alterFirebaseTopicState (topic : String? , c : Context ) {
90
+ val editor = getSharedPrefs(c).edit()
91
+ editor.putBoolean(topic, false )
92
+ editor.apply ()
93
+ }
94
+
95
+ @JvmStatic
96
+ fun getPackage (c : Context ): String {
97
+ return try {
98
+ c.applicationContext.packageName
99
+ } catch (e: NullPointerException ) {
100
+ " fail"
101
+ }
102
+ }
103
+
104
+ @JvmStatic
105
+ fun getAppName (c : Context ? ): String {
106
+ return if (c != null && c.applicationContext.applicationInfo != null )
107
+ c.applicationContext.applicationInfo.loadLabel(c.packageManager).toString()
108
+ else " Hover"
109
+ }
110
+
111
+ @JvmStatic
112
+ fun formatAmount (number : String? ): String {
113
+ return when (number) {
114
+ " 0" -> " 00"
115
+ null -> " --"
116
+ else -> try {
117
+ formatAmount(amountToDouble(number))
118
+ } catch (e: Exception ) {
119
+ number
120
+ }
121
+ }
122
+ }
123
+
124
+ @JvmStatic
125
+ fun formatAmount (number : Double? ): String {
126
+ return try {
127
+ val formatter = DecimalFormat (" #,##0.00" )
128
+ formatter.maximumFractionDigits = 2
129
+ formatter.format(number)
130
+ } catch (e: Exception ) {
131
+ number.toString()
132
+ }
133
+ }
134
+
135
+ @JvmStatic
136
+ fun formatAmountForUSSD (number : Double? ): String {
137
+ return try {
138
+ val formatter = DecimalFormat (" ###0.##" )
139
+ formatter.maximumFractionDigits = 2
140
+ formatter.minimumFractionDigits = 0
141
+ formatter.format(number)
142
+ } catch (e: Exception ) {
143
+ number.toString()
144
+ }
145
+ }
146
+
147
+ @JvmStatic
148
+ fun amountToDouble (amount : String? ): Double? {
149
+ try {
150
+ return amount?.replace(" ," .toRegex(), " " )?.toDouble()
151
+ } catch (e: NumberFormatException ) {
152
+ return null
153
+ }
154
+ }
155
+
156
+ @JvmStatic
157
+ fun formatPercent (number : Int ): String {
158
+ return try {
159
+ val formatter = DecimalFormat (" ##0" )
160
+ formatter.maximumFractionDigits = 0
161
+ formatter.format(number)
162
+ } catch (e: Exception ) {
163
+ number.toString()
164
+ }
165
+ }
166
+ }
0 commit comments