@momo-kits/calculator-keyboard 0.167.1-beta.7 → 0.167.1-beta.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,9 +3,12 @@ package com.momokits.calculatorkeyboard.native
3
3
  import android.annotation.SuppressLint
4
4
  import android.content.Context
5
5
  import android.content.res.ColorStateList
6
+ import android.graphics.Bitmap
7
+ import android.graphics.BitmapFactory
6
8
  import android.graphics.Color
7
9
  import android.graphics.drawable.GradientDrawable
8
10
  import android.text.Editable
11
+ import android.util.DisplayMetrics
9
12
  import android.view.Gravity
10
13
  import android.view.MotionEvent
11
14
  import android.widget.Button
@@ -17,6 +20,8 @@ import androidx.core.graphics.toColorInt
17
20
  import com.momokits.calculatorkeyboard.native.utils.CalculatorEngine
18
21
  import com.momokits.calculatorkeyboard.native.utils.CalculatorFormatter
19
22
  import com.momokits.calculatorkeyboard.R
23
+ import java.net.URL
24
+ import kotlin.concurrent.thread
20
25
 
21
26
  @SuppressLint("SetTextI18n", "ViewConstructor")
22
27
  class CustomKeyboardView(
@@ -181,6 +186,26 @@ class CustomKeyboardView(
181
186
  setImageTintList(ColorStateList.valueOf(Color.BLACK))
182
187
  setOnClickListener { onKeyPress(key, false) }
183
188
  applyImagePressedOpacity()
189
+ loadBackIcon()
190
+ }
191
+ }
192
+
193
+ // Swap the bundled fallback icon for the design-system icon once it's downloaded
194
+ private fun ImageButton.loadBackIcon() {
195
+ backIconCache?.let { return setImageBitmap(it) }
196
+
197
+ thread {
198
+ val bitmap = runCatching {
199
+ URL(BACK_ICON_URL).openConnection().apply {
200
+ connectTimeout = BACK_ICON_TIMEOUT_MS
201
+ readTimeout = BACK_ICON_TIMEOUT_MS
202
+ }.getInputStream().use(BitmapFactory::decodeStream)
203
+ }.getOrNull() ?: return@thread
204
+
205
+ // Density makes the drawable render at BACK_ICON_SIZE_DP without resampling the bitmap
206
+ bitmap.density = bitmap.width * DisplayMetrics.DENSITY_DEFAULT / BACK_ICON_SIZE_DP
207
+ backIconCache = bitmap
208
+ post { setImageBitmap(bitmap) }
184
209
  }
185
210
  }
186
211
 
@@ -400,4 +425,13 @@ class CustomKeyboardView(
400
425
  }
401
426
  customKeyButton?.setTextColor(textColor)
402
427
  }
428
+
429
+ companion object {
430
+ private const val BACK_ICON_URL = "https://img.mservice.com.vn/app/img/kits/chat_back_space.png"
431
+ private const val BACK_ICON_SIZE_DP = 24
432
+ private const val BACK_ICON_TIMEOUT_MS = 10_000
433
+
434
+ @Volatile
435
+ private var backIconCache: Bitmap? = null
436
+ }
403
437
  }
@@ -1,6 +1,10 @@
1
1
  #import "CalculatorKeyboardView.h"
2
2
  #import "Utils.h"
3
3
 
4
+ static NSString *const kBackIconURL = @"https://img.mservice.com.vn/app/img/kits/chat_back_space.png";
5
+ static const CGFloat kBackIconSize = 24;
6
+ static UIImage *sBackIcon;
7
+
4
8
  @interface CalculatorKeyboardView ()
5
9
  @property (nonatomic, strong) NSArray<NSArray<NSString *> *> *numWithCTAKeys;
6
10
  @property (nonatomic, strong) NSArray<NSArray<NSString *> *> *defaultKeys;
@@ -140,6 +144,7 @@
140
144
  UIImage *image = [UIImage systemImageNamed:@"delete.backward" withConfiguration:config];
141
145
  [button setImage:image forState:UIControlStateNormal];
142
146
  button.tintColor = UIColor.blackColor;
147
+ [self loadBackIconForButton:button];
143
148
  }
144
149
 
145
150
  if ([_specialKeys containsObject:key] || isMainKey) {
@@ -161,6 +166,32 @@
161
166
  }
162
167
  }
163
168
 
169
+ - (void)loadBackIconForButton:(UIButton *)button
170
+ {
171
+ if (sBackIcon) {
172
+ [button setImage:sBackIcon forState:UIControlStateNormal];
173
+ return;
174
+ }
175
+
176
+ __weak UIButton *weakButton = button;
177
+ [[NSURLSession.sharedSession dataTaskWithURL:[NSURL URLWithString:kBackIconURL]
178
+ completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
179
+ CGImageRef cgImage = [UIImage imageWithData:data].CGImage;
180
+ if (!cgImage) return;
181
+
182
+ // Scale makes the image size kBackIconSize points without redrawing it
183
+ UIImage *icon = [[UIImage imageWithCGImage:cgImage
184
+ scale:CGImageGetWidth(cgImage) / kBackIconSize
185
+ orientation:UIImageOrientationUp]
186
+ imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
187
+
188
+ dispatch_async(dispatch_get_main_queue(), ^{
189
+ sBackIcon = icon;
190
+ [weakButton setImage:icon forState:UIControlStateNormal];
191
+ });
192
+ }] resume];
193
+ }
194
+
164
195
  - (void)updateCustomKeyTitle
165
196
  {
166
197
  if (!self.customKeyButton || !_customKeyText) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@momo-kits/calculator-keyboard",
3
- "version": "0.167.1-beta.7",
3
+ "version": "0.167.1-beta.8",
4
4
  "description": "react native calculator keyboard",
5
5
  "main": "./src/index.tsx",
6
6
  "files": [