You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.8 KiB
51 lines
1.8 KiB
// Copyright 2018 The Flutter Architecture Sample Authors. All rights reserved.
|
|
// Use of this source code is governed by the MIT license that can be found
|
|
// in the LICENSE file.
|
|
|
|
import 'dart:math';
|
|
|
|
import 'package:bytedesk_kefu/util/bytedesk_utils.dart';
|
|
|
|
/// A UUID generator, useful for generating unique IDs for your Todos.
|
|
/// Shamelessly extracted from the Flutter source code.
|
|
///
|
|
/// This will generate unique IDs in the format:
|
|
///
|
|
/// f47ac10b-58cc-4372-a567-0e02b2c3d479
|
|
///
|
|
/// ### Example
|
|
///
|
|
/// final String id = Uuid().generateV4();
|
|
class BytedeskUuid {
|
|
//
|
|
static String uuid() {
|
|
String timestamp = BytedeskUtils.formatedTimestampNow();
|
|
final int special = 8 + Random().nextInt(4);
|
|
return timestamp +
|
|
'${_bitsDigits(16, 4)}${_bitsDigits(16, 4)}'
|
|
'${_bitsDigits(16, 4)}'
|
|
'4${_bitsDigits(12, 3)}'
|
|
'${_printDigits(special, 1)}${_bitsDigits(12, 3)}'
|
|
'${_bitsDigits(16, 4)}${_bitsDigits(16, 4)}${_bitsDigits(16, 4)}';
|
|
}
|
|
/// Generate a version 4 (random) uuid. This is a uuid scheme that only uses
|
|
/// random numbers as the source of the generated uuid.
|
|
static String generateV4() {
|
|
// Generate xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx / 8-4-4-4-12.
|
|
final int special = 8 + Random().nextInt(4);
|
|
return '${_bitsDigits(16, 4)}${_bitsDigits(16, 4)}-'
|
|
'${_bitsDigits(16, 4)}-'
|
|
'4${_bitsDigits(12, 3)}-'
|
|
'${_printDigits(special, 1)}${_bitsDigits(12, 3)}-'
|
|
'${_bitsDigits(16, 4)}${_bitsDigits(16, 4)}${_bitsDigits(16, 4)}';
|
|
}
|
|
|
|
static String _bitsDigits(int bitCount, int digitCount) =>
|
|
_printDigits(_generateBits(bitCount), digitCount);
|
|
|
|
static int _generateBits(int bitCount) => Random().nextInt(1 << bitCount);
|
|
|
|
static String _printDigits(int value, int count) =>
|
|
value.toRadixString(16).padLeft(count, '0');
|
|
}
|