Skip to content

Instantly share code, notes, and snippets.

View Ankit-Slnk's full-sized avatar
💭
Coding...

ANKIT SOLANKI Ankit-Slnk

💭
Coding...
View GitHub Profile
@Ankit-Slnk
Ankit-Slnk / DecimalFormat.java
Created March 15, 2023 12:00
Fix decimal format for price
public static String getDecimalFormat(double value) {
String newValue = new DecimalFormat("#.##").format(value);
if (!newValue.contains(".")) {
return newValue + ".00";
} else {
return newValue.split("\\.")[1].length() == 1 ? (newValue + "0") : newValue;
}
}
@Ankit-Slnk
Ankit-Slnk / RoundUp.java
Created March 15, 2023 11:59
Round up a value
public static double roundUp(double toRound) {
if (toRound % 100 == 0) return toRound;
double wholeNumber = toRound * 100;
double roundedValue = Math.ceil(wholeNumber / 10.0) * 10;
return roundedValue / 100;
}
@Ankit-Slnk
Ankit-Slnk / LaravelErrorHandler.java
Created March 15, 2023 11:57
Laravel default error format show in android
public static void showError(Context context, ResponseBody responseBody) {
JSONObject jObjError = null;
try {
jObjError = new JSONObject(responseBody.string());
} catch (Exception e) {
}
try {
JSONObject data = jObjError.getJSONObject("errors");
Iterator<String> errorKeys = data.keys();
@Ankit-Slnk
Ankit-Slnk / Extension.dart
Created March 15, 2023 11:43
String extension toCapitalise and toTitlecase
extension StringCasingExtension on String {
String toCapitalized() =>
length > 0 ? '${this[0].toUpperCase()}${substring(1).toLowerCase()}' : '';
String toTitleCase() => replaceAll(RegExp(' +'), ' ')
.split(' ')
.map((str) => str.toCapitalized())
.join(' ');
}
@Ankit-Slnk
Ankit-Slnk / LatlngBound.dart
Created March 15, 2023 11:40
Coordinates list to bound
static LatLngBounds getBounds(List<LatLng> polylineCoordinates) {
double x0, x1, y0, y1;
for (LatLng latLng in polylineCoordinates) {
if (latLng.latitude != null && latLng.longitude != null) {
if (x0 == null) {
x0 = x1 = latLng.latitude;
y0 = y1 = latLng.longitude;
} else {
if (latLng.latitude > x1) x1 = latLng.latitude;
if (latLng.latitude < x0) x0 = latLng.latitude;
@Ankit-Slnk
Ankit-Slnk / Polyline.dart
Created March 15, 2023 11:38
Encoded polyline to google polyline
static List<LatLng> decodeEncodedPolyline(String encoded) {
List<LatLng> poly = [];
int index = 0, len = encoded.length;
int lat = 0, lng = 0;
while (index < len) {
int b, shift = 0, result = 0;
do {
b = encoded.codeUnitAt(index++) - 63;
result |= (b & 0x1f) << shift;
@Ankit-Slnk
Ankit-Slnk / listToMap.dart
Created March 15, 2023 11:37
Interview question
String packageNames = "cat,dog,cat,cow";
var map = Map<String, int>();
List<String> ps = [];
packageNames.split(",").forEach((element) {
if (!map.containsKey(element)) {
map[element] = 1;
} else {
map[element] = map[element]! + 1;
}
});
@Ankit-Slnk
Ankit-Slnk / broadcastReceiver.java
Created May 3, 2022 18:58
android broadcast receiver code
// register receiver
IntentFilter filter = new IntentFilter("ABCD");
registerReceiver(receiver, filter);
// reveicer
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
try {
dialog.dismiss();
public static void hideKeyboard(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
//Find the currently focused view, so we can grab the correct window token from it.
View view = activity.getCurrentFocus();
//If no view currently has focus, create a new one, just so we can grab a window token from it
if (view == null) {
view = new View(activity);
}
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
public static File bitmapToFile(Bitmap bitmap) {
// File name like "image.png"
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String fileNameToSave = sdf.format(new Date());
//create a file to write bitmap data
File file = null;
try {
file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + File.separator + fileNameToSave + ".png");
file.createNewFile();