mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-23 15:09:33 +01:00
219 lines
4.8 KiB
JavaScript
Executable File
219 lines
4.8 KiB
JavaScript
Executable File
import Storage from 'notes-core/api/index';
|
|
import {Dimensions} from 'react-native';
|
|
import {eSendEvent} from '../services/eventManager';
|
|
import {
|
|
eOpenSideMenu,
|
|
eCloseSideMenu,
|
|
eDisableGestures,
|
|
eEnableGestures,
|
|
eShowToast,
|
|
eHideToast,
|
|
} from '../services/events';
|
|
import {DeviceDetectionService} from './deviceDetection';
|
|
import StorageInterface from './storage';
|
|
import MMKV from 'react-native-mmkv-storage';
|
|
import {updateEvent} from '../components/DialogManager/recievers';
|
|
import {ACTIONS} from '../provider/actions';
|
|
export const DDS = new DeviceDetectionService();
|
|
export const db = new Storage(StorageInterface);
|
|
|
|
export async function setSetting(settings, name, value) {
|
|
let s = {...settings};
|
|
s[name] = value;
|
|
await MMKV.setStringAsync('settings', JSON.stringify(s));
|
|
|
|
updateEvent({type: ACTIONS.SETTINGS, settings: s});
|
|
}
|
|
|
|
export const getElevation = elevation => {
|
|
return {
|
|
elevation,
|
|
shadowColor: 'black',
|
|
shadowOffset: {width: 0.3 * elevation, height: 0.5 * elevation},
|
|
shadowOpacity: 0.2,
|
|
shadowRadius: 0.7 * elevation,
|
|
};
|
|
};
|
|
|
|
export const editing = {
|
|
currentlyEditing: false,
|
|
isFullscreen: false,
|
|
actionAfterFirstSave: {
|
|
type: null,
|
|
},
|
|
};
|
|
export const selection = {
|
|
data: [],
|
|
type: null,
|
|
selectedItems: [],
|
|
};
|
|
|
|
export const history = {
|
|
selectedItemsList: [],
|
|
};
|
|
|
|
export function timeSince(date) {
|
|
var seconds = Math.floor((new Date() - date) / 1000);
|
|
|
|
var interval = Math.floor(seconds / 31536000);
|
|
|
|
if (interval > 0.9) {
|
|
return interval < 2 ? interval + ' year ago' : interval + ' years ago';
|
|
}
|
|
interval = Math.floor(seconds / 2592000);
|
|
if (interval > 0.9) {
|
|
return interval < 2 ? interval + ' month ago' : interval + ' months ago';
|
|
}
|
|
interval = Math.floor(seconds / 86400);
|
|
if (interval > 0.9) {
|
|
return interval < 2 ? interval + ' day ago' : interval + ' days ago';
|
|
}
|
|
interval = Math.floor(seconds / 3600);
|
|
if (interval > 0.9) {
|
|
return interval < 2 ? interval + ' hour ago' : interval + ' hours ago';
|
|
}
|
|
interval = Math.floor(seconds / 60);
|
|
if (interval > 0.9) {
|
|
return interval < 2 ? interval + ' min ago' : interval + ' min ago';
|
|
}
|
|
return Math.floor(seconds) + ' secs ago';
|
|
}
|
|
|
|
export const w = Dimensions.get('window').width;
|
|
export const h = Dimensions.get('window').height;
|
|
|
|
export const ToastEvent = {
|
|
show: (
|
|
message,
|
|
type,
|
|
context = 'global',
|
|
duration = 3000,
|
|
func = null,
|
|
actionText = '',
|
|
) => {
|
|
eSendEvent(eShowToast, {
|
|
message,
|
|
type,
|
|
context,
|
|
duration,
|
|
func,
|
|
actionText,
|
|
});
|
|
},
|
|
hide: (
|
|
message,
|
|
type,
|
|
context = 'global',
|
|
duration = 3000,
|
|
func = null,
|
|
actionText = '',
|
|
) => {
|
|
eSendEvent(eHideToast, {
|
|
message,
|
|
type,
|
|
context,
|
|
duration,
|
|
func,
|
|
actionText,
|
|
});
|
|
},
|
|
};
|
|
|
|
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;
|
|
};
|
|
|
|
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)})`;
|
|
};
|