Class ImageDB
This class is responsible for managing image records stored in the
images collection. It supports generating new image document IDs,
saving image data, loading image data, deleting images, and listening for
real-time updates from Firestore.
All methods are static because this class acts as a utility-style database access layer for image-related operations.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceCallback interface for image write and delete operations.static interfaceCallback interface for real-time image collection updates.static interfaceCallback interface for loading a single image. -
Method Summary
Modifier and TypeMethodDescriptionstatic voiddeleteImage(String imageDocId, ImageDB.ImageMutationCallback callback) Deletesimages/{imageDocId}from the Firestore images collection.static StringgenNewId()Generates a new document ID from the Firestore images collection.static voidloadImage(String imageDocId, ImageDB.LoadImageCallback callback) Loads a Base64 image string fromimages/{imageDocId}and constructs anImageobject from the retrieved data.static voidsaveImage(Image image, ImageDB.ImageMutationCallback callback) Saves an image intoimages/{imageDocId}.static voidAttaches a real-time listener to theimagescollection.static voidDetaches the real-time images listener if one is currently active.
-
Method Details
-
genNewId
Generates a new document ID from the Firestore images collection.- Returns:
- the newly generated document ID
-
saveImage
Saves an image intoimages/{imageDocId}.The image data is stored under the
datafield, and the current timestamp is stored underupdatedTimestamp. Existing fields are preserved because the write uses merge semantics.- Parameters:
image- the image object to savecallback- the callback to invoke after the Firestore write succeeds or fails
-
loadImage
Loads a Base64 image string fromimages/{imageDocId}and constructs anImageobject from the retrieved data.- Parameters:
imageDocId- the document ID in the images collectioncallback- the callback that receives the loaded image or an error
-
startImagesListen
Attaches a real-time listener to theimagescollection.For each document change received from Firestore, the corresponding callback method is invoked based on whether the image was added, modified, or removed.
- Parameters:
callback- receives individual change events or listener errors
-
stopImagesListen
public static void stopImagesListen()Detaches the real-time images listener if one is currently active.This should be called when the associated screen or component is being torn down in order to avoid memory leaks and unnecessary Firestore updates.
-
deleteImage
Deletesimages/{imageDocId}from the Firestore images collection.- Parameters:
imageDocId- the document ID in the images collectioncallback- the callback to invoke after the delete succeeds or fails
-