Files
notesnook/apps/mobile/src/utils/utils.js

219 lines
4.8 KiB
JavaScript
Raw Normal View History

2020-03-14 13:54:16 +05:00
import Storage from 'notes-core/api/index';
import {Dimensions} from 'react-native';
2020-01-25 20:12:47 +05:00
import {eSendEvent} from '../services/eventManager';
import {
eOpenSideMenu,
eCloseSideMenu,
eDisableGestures,
eEnableGestures,
2020-01-25 23:24:01 +05:00
eShowToast,
eHideToast,
2020-01-25 20:12:47 +05:00
} from '../services/events';
2020-03-14 13:54:16 +05:00
import {DeviceDetectionService} from './deviceDetection';
import StorageInterface from './storage';
2020-03-21 09:50:54 +05:00
import MMKV from 'react-native-mmkv-storage';
import {updateEvent} from '../components/DialogManager/recievers';
import {ACTIONS} from '../provider/actions';
2020-03-14 13:54:16 +05:00
export const DDS = new DeviceDetectionService();
export const db = new Storage(StorageInterface);
2020-03-21 09:50:54 +05:00
export async function setSetting(settings, name, value) {
let s = {...settings};
s[name] = value;
2020-03-23 19:53:12 +05:00
await MMKV.setStringAsync('settings', JSON.stringify(s));
2020-03-21 09:50:54 +05:00
updateEvent({type: ACTIONS.SETTINGS, settings: s});
}
2019-11-17 02:48:19 +05:00
export const getElevation = elevation => {
return {
elevation,
shadowColor: 'black',
2019-11-27 21:44:52 +05:00
shadowOffset: {width: 0.3 * elevation, height: 0.5 * elevation},
shadowOpacity: 0.2,
shadowRadius: 0.7 * elevation,
2019-11-17 02:48:19 +05:00
};
};
2019-11-28 21:40:37 +05:00
export const editing = {
currentlyEditing: false,
2020-03-02 10:25:38 +05:00
isFullscreen: false,
2020-03-04 13:40:47 +05:00
actionAfterFirstSave: {
type: null,
},
2020-03-02 10:25:38 +05:00
};
2020-03-18 11:58:56 +05:00
export const selection = {
data: [],
type: null,
selectedItems: [],
};
2020-03-02 10:25:38 +05:00
export const history = {
selectedItemsList: [],
};
2019-11-28 21:40:37 +05:00
export function timeSince(date) {
var seconds = Math.floor((new Date() - date) / 1000);
var interval = Math.floor(seconds / 31536000);
2020-01-17 10:56:52 +05:00
if (interval > 0.9) {
2019-11-28 21:40:37 +05:00
return interval < 2 ? interval + ' year ago' : interval + ' years ago';
}
interval = Math.floor(seconds / 2592000);
2020-01-17 10:56:52 +05:00
if (interval > 0.9) {
2019-11-28 21:40:37 +05:00
return interval < 2 ? interval + ' month ago' : interval + ' months ago';
}
interval = Math.floor(seconds / 86400);
2020-01-17 10:56:52 +05:00
if (interval > 0.9) {
2019-11-28 21:40:37 +05:00
return interval < 2 ? interval + ' day ago' : interval + ' days ago';
}
interval = Math.floor(seconds / 3600);
2020-01-17 10:56:52 +05:00
if (interval > 0.9) {
2019-11-28 21:40:37 +05:00
return interval < 2 ? interval + ' hour ago' : interval + ' hours ago';
}
interval = Math.floor(seconds / 60);
2020-01-17 10:56:52 +05:00
if (interval > 0.9) {
2019-11-28 21:40:37 +05:00
return interval < 2 ? interval + ' min ago' : interval + ' min ago';
}
return Math.floor(seconds) + ' secs ago';
}
2019-11-29 11:31:43 +05:00
export const w = Dimensions.get('window').width;
export const h = Dimensions.get('window').height;
2019-12-04 11:29:40 +05:00
export const ToastEvent = {
2020-03-14 12:59:52 +05:00
show: (
message,
type,
context = 'global',
duration = 3000,
func = null,
actionText = '',
) => {
2020-01-25 23:24:01 +05:00
eSendEvent(eShowToast, {
2019-12-05 17:40:09 +05:00
message,
type,
2020-03-14 12:59:52 +05:00
context,
2019-12-05 17:40:09 +05:00
duration,
func,
actionText,
});
2019-12-04 11:29:40 +05:00
},
2020-03-14 12:59:52 +05:00
hide: (
message,
type,
context = 'global',
duration = 3000,
func = null,
actionText = '',
) => {
2020-01-25 23:24:01 +05:00
eSendEvent(eHideToast, {
2019-12-05 17:40:09 +05:00
message,
type,
2020-03-14 12:59:52 +05:00
context,
2019-12-05 17:40:09 +05:00
duration,
func,
actionText,
});
2019-12-04 11:29:40 +05:00
},
};
2019-12-04 19:38:19 +05:00
2020-02-02 20:44:17 +05:00
export const timeConverter = timestamp => {
if (!timestamp) return;
var d = new Date(timestamp), // Convert the passed timestamp to milliseconds
yyyy = d.getFullYear(),
mm = ('0' + (d.getMonth() + 1)).slice(-2), // Months are zero based. Add leading 0.
dd = ('0' + d.getDate()).slice(-2), // Add leading 0.
currentDay = d.getDay(),
hh = d.getHours(),
h = hh,
min = ('0' + d.getMinutes()).slice(-2), // Add leading 0.
ampm = 'AM',
time;
let days = [
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
];
var months = [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec',
];
if (hh > 12) {
h = hh - 12;
ampm = 'PM';
} else if (hh === 12) {
h = 12;
ampm = 'PM';
} else if (hh == 0) {
h = 12;
}
// ie: 2013-02-18, 8:35 AM
time =
days[currentDay] +
' ' +
dd +
' ' +
months[d.getMonth()] +
', ' +
yyyy +
', ' +
h +
':' +
min +
' ' +
ampm;
return time;
};
2020-02-22 13:02:16 +05:00
const isValidHex = hex => /^#([A-Fa-f0-9]{3,4}){1,2}$/.test(hex);
const getChunksFromString = (st, chunkSize) =>
st.match(new RegExp(`.{${chunkSize}}`, 'g'));
const convertHexUnitTo256 = hexStr =>
parseInt(hexStr.repeat(2 / hexStr.length), 16);
const getAlphafloat = (a, alpha) => {
if (typeof a !== 'undefined') {
return a / 256;
}
if (typeof alpha !== 'undefined') {
if (1 < alpha && alpha <= 100) {
return alpha / 100;
}
if (0 <= alpha && alpha <= 1) {
return alpha;
}
}
return 1;
};
export const hexToRGBA = (hex, alpha) => {
if (!isValidHex(hex)) {
throw new Error('Invalid HEX');
}
const chunkSize = Math.floor((hex.length - 1) / 3);
const hexArr = getChunksFromString(hex.slice(1), chunkSize);
const [r, g, b, a] = hexArr.map(convertHexUnitTo256);
return `rgba(${r}, ${g}, ${b}, ${getAlphafloat(a, alpha)})`;
};