Skip to content

FB4D Reference TFirebaseHelpers

Christoph Schneider edited this page Dec 12, 2024 · 8 revisions

Class TFirebaseHelpers

This class offers many helper class functions for different topics. These functions are used internally by libray, but also help a lot when using the library. Only functions that are useful when using the library are described here.

Time Conversion

class function TFirebaseHelpers.CodeRFC3339DateTime(DateTimeStamp: TDateTime): string;
class function TFirebaseHelpers.DecodeRFC3339DateTime(DateTimeStamp: string): TDateTime;

class function TFirebaseHelpers.ConvertTimeStampToUTCDateTime(TimeStamp: Int64): TDateTime;

class function TFirebaseHelpers.ConvertRFC5322ToUTCDateTime(DateTime: string): TDateTime;
class function TFirebaseHelpers.ConvertRFC5322ToLocalDateTime(DateTime: string): TDateTime;

class function TFirebaseHelpers.ConvertTimeStampToLocalDateTime(Timestamp: Int64): TDateTime;
class function TFirebaseHelpers.ConvertToLocalDateTime(DateTimeStampUTC: TDateTime): TDateTime;
class function TFirebaseHelpers.ConvertToUTCDateTime(DateTimeStampLocal: TDateTime): TDateTime;

Application in Realtime Database:

var ServerTime: TDateTime;
ServerTime := TFirebaseHelpers.ConvertTimeStampToLocalDateTime(
  (IRealTimeDB.GetServerVariablesSynchronous(cServerVariableTimeStamp, [])).as TJSONNumber).AsInt64);

Array of String

class function TFirebaseHelpers.ArrStrToCommaStr(Arr: array of string): string;
class function TFirebaseHelpers.ArrStrToQuotedCommaStr(Arr: array of string): string;

Auto ID

To create an ID on the client side, the CreateAutoID function can be called up. There are different types of IDs for the two databases RTDB (FBID, PUSHID) and Firestore (FSID, FSPUSHID) and different purposes with and without TimeStamp integration. TimeStamp based IDs allow alphabetical sorting according to the most recent ID.

type TIDKind = (FBID {random 22 ID64 chars},
                PUSHID {timestamp and random: total 20 chars of PushID64},
                FSID {random 20 ID64 chars},
                FSPUSHID {timestamp and random: total 24 ID64 chars});
class function TFirebaseHelpers.CreateAutoID(IDKind: TIDKind = FBID): string;

The different character sets for ID64 and PushID64 consist of 64 characters, which are almost identical to Base64. Since the characters + and / cause problems in paths of both databases, these characters are replaced by _ and -. For PushID64 only the order of the characters is different, so that they are identical to the Post and Push operation of the Realtime DB.

cID64 =
  'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-';
// Base64 with exception of the last two chars '_' and '-' are not real Base64
// because '+' and '/' causes troubles in IDs
cPushID64 =
  '-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz';
// This notification is used in the Realtime DB for Post and Push operation

For FBID and FSID:

class function TFirebaseHelpers.ConvertGUIDtoID64(Guid: TGuid): string;
class function TFirebaseHelpers.ConvertID64toGUID(const FBID: string): TGuid;

Migration Note: Since version 1.6.4.616 FBID is renamed to ID64. You must therefore change the names ConvertGUIDtoFBID to ConvertGUIDtoID64 and ConvertFBIDtoGUID to ConvertID64toGUID.

For PUSHID:

class function TFirebaseHelpers.ConvertTimeStampAndRandomPatternToPUSHID(timestamp: TDateTime;
  Random: TBytes; TimeIsUTC: boolean = false): string;
class function TFirebaseHelpers.DecodeTimeStampFromPUSHID(const PUSHID: string;
  ConvertToLocalTime: boolean = true): TDateTime;

For FSPUSHID:

class function ConvertTimeStampAndRandomPatternToID64(timestamp: TDateTime;
  Random: TBytes; TimeIsUTC: boolean = false): string;
class function DecodeTimeStampFromID64(const FSPUSHID: string;
  ConvertToLocalTime: boolean = true): TDateTime;

Migration Note: Since version 1.6.4.616 Base64 is renamed to ID64. You must therefore change the names ConvertTimeStampAndRandomPatternToBase64 to ConvertTimeStampAndRandomPatternToID64 and DecodeTimeStampFromBase64 to DecodeTimeStampFromID64.

File

The following two functions are used to download a file from Cloud Storage, for example. The first works asynchronously by calling a callback function after the download for use in GUI applications. The second synchronous function performs the download blocking and is suitable for use from a background thread or a service or console application.

class procedure TFirebaseHelpers.SimpleDownload(const DownloadUrl: string; Stream: TStream;
  OnSuccess: TOnSimpleDownloadSuccess;
  OnError: TOnSimpleDownloadError = nil);
class procedure TFirebaseHelpers.SimpleDownloadSynchronous(const DownloadUrl: string;
  Stream: TStream);

The following function derives the file extension from the content type.

class function TFirebaseHelpers.ContentTypeToFileExt(const ContentType: string): string;

The following function determines the ContentType from a downloaded stream.

class function TFirebaseHelpers.ImageStreamToContentType(Stream: TStream): TRESTContentType;

EMail

Checks whether the entered e-mail contains a valid address format without checking whether the address really exists. For this purpose, the Auth Service contains the SendEmailVerification function, which sends an e-mail with a confirmation link.

class function TFirebaseHelpers.IsEMailAdress(const EMail: string): boolean;

Firebase Authentication service offers an optional and configurable password policy. This can be found in the Firebase Console under Authentication in the Settings tab under Password Policy. The following function enables a application-side check analogous to the settings in the console.

type
  TPasswordPolicyItem = (RequireUpperCase, RequireLowerCase, RequireSpecialChar, RequireNumericChar);
  TPasswordPolicy = set of TPasswordPolicyItem;

class function IsPasswordPolicyFulfilled(const Password: string; Policy: TPasswordPolicy = [];
  MinPasswordLength: integer = 6; MaxPasswordLength: integer = 4096): boolean;

Application

Depending on the platform, Log enables the output of debug information to the console or DebugView for Windows. This function is used both within the library and can also be used in the application.

class procedure TFirebaseHelpers.Log(msg: string);
class procedure TFirebaseHelpers.LogFmt(msg: string; const Args: array of const);

Simple check to proof if the application is terminated now.

class function TFirebaseHelpers.AppIsTerminated: boolean;

For GUI Frameworks call Application.ProcessMessages and Sleep. For Console Application call simple Sleep.

class procedure TFirebaseHelpers.SleepAndMessageLoop(SleepInMs: cardinal);

Check, if we are currently called from the main thread.

class function TFirebaseHelpers.IsMainThread: boolean;

Get a string with the build configuration and platfrom.

class function TFirebaseHelpers.GetConfigAndPlatform: string;
class function TFirebaseHelpers.GetPlatform: string;

ML helpers

Returns a language name in englsih from the language code used in ML Vision:

class function TFirebaseHelpers.GetLanguageInEnglishFromCode(const Code: string): string;
Clone this wiki locally