use of com.codename1.ui.List in project CodenameOne by codenameone.
the class ImageDownloadService method setEntryInListModel.
/**
* This method is invoked when an image finished downloading and should be set to an offset in the list
* model. This is useful for special cases with complex list model hierarchies or proxies.
*
* @param offset the offset in the list given when creating the service
* @param img the image
*/
protected void setEntryInListModel(int offset, Image img) {
Map h;
ListModel model;
if (targetModel != null) {
model = targetModel;
} else {
if (targetList instanceof List) {
model = ((List) targetList).getModel();
} else {
model = ((ContainerList) targetList).getModel();
}
}
h = (Map) model.getItemAt(targetOffset);
if (!fastScale && toScale != null) {
img = scaleImage(img, toScale, maintainAspectRatio);
}
h.put(targetKey, img);
if (model instanceof DefaultListModel) {
((DefaultListModel) model).setItem(targetOffset, h);
}
}
use of com.codename1.ui.List in project CodenameOne by codenameone.
the class ImageDownloadService method createImageToStorage.
/**
* Constructs an image request that will automatically populate the given list
* when the response arrives, it will cache the file locally as a file
* in the file storage.
* This assumes the GenericListCellRenderer style of
* list which relies on a map based model approach.
*
* @param url the image URL
* @param targetList the list that should be updated when the data arrives
* @param targetModel the model
* @param targetOffset the offset within the list to insert the image
* @param targetKey the key for the map in the target offset
* @param cacheId a unique identifier to be used to store the image into storage
* @param keep if set to true keeps the file in RAM once loaded
* @param scale the scale of the image to put in the List or null
*/
private static void createImageToStorage(final String url, final Component targetList, final ListModel targetModel, final int targetOffset, final String targetKey, final String cacheId, final boolean keep, final Dimension scale, final byte priority, final Image placeholderImage, final boolean maintainAspectRatio) {
if (Display.getInstance().isEdt()) {
Display.getInstance().scheduleBackgroundTask(new Runnable() {
public void run() {
createImageToStorage(url, targetList, targetModel, targetOffset, targetKey, cacheId, keep, scale, priority, placeholderImage, maintainAspectRatio);
}
});
return;
}
Image im = cacheImage(cacheId, keep, null, scale, placeholderImage, maintainAspectRatio);
ImageDownloadService i = new ImageDownloadService(url, targetList, targetOffset, targetKey);
i.targetModel = targetModel;
i.maintainAspectRatio = maintainAspectRatio;
if (im != null) {
i.setEntryInListModel(targetOffset, im);
targetList.repaint();
return;
}
// image not found on cache go and download from the url
i.cacheImages = true;
i.cacheId = cacheId;
i.keep = keep;
i.toScale = scale;
i.placeholder = placeholderImage;
i.setPriority(priority);
i.setFailSilently(true);
NetworkManager.getInstance().addToQueue(i);
}
use of com.codename1.ui.List in project CodenameOne by codenameone.
the class Purchase method loadReceipts.
/**
* Fetches receipts from the IAP service so that we know we are dealing
* with current data. This method should be called before checking a
* subscription expiry date so that any changes the user has made in the
* store is reflected here (e.g. cancelling or renewing subscription).
* @param ifOlderThanMs Update is only performed if more than {@code ifOlderThanMs} milliseconds has elapsed
* since the last successful fetch.
* @param callback Callback called when request is complete. Passed {@code true} if
* the data was successfully fetched. {@code false} otherwise.
*/
private final void loadReceipts(long ifOlderThanMs, final SuccessCallback<Boolean> callback) {
if (loadInProgress) {
Log.p("Did not load receipts because another load is in progress");
callback.onSucess(false);
return;
}
loadInProgress = true;
Date lastRefreshTime = getReceiptsRefreshTime();
Date now = new Date();
if (lastRefreshTime.getTime() + ifOlderThanMs > now.getTime()) {
Log.p("Receipts were last refreshed at " + lastRefreshTime + " so we won't refetch.");
loadInProgress = false;
callback.onSucess(true);
return;
}
List<Receipt> oldData = new ArrayList<Receipt>();
oldData.addAll(getReceipts());
SuccessCallback<Receipt[]> onSuccess = new SuccessCallback<Receipt[]>() {
public void onSucess(Receipt[] value) {
if (value != null) {
setReceipts(Arrays.asList(value));
setReceiptsRefreshTime(new Date());
loadInProgress = false;
callback.onSucess(Boolean.TRUE);
} else {
loadInProgress = false;
callback.onSucess(Boolean.FALSE);
}
}
};
if (receiptStore != null) {
receiptStore.fetchReceipts(onSuccess);
} else {
Log.p("No receipt store is currently registered so no receipts were fetched");
loadInProgress = false;
callback.onSucess(Boolean.FALSE);
}
}
use of com.codename1.ui.List in project CodenameOne by codenameone.
the class Purchase method getPendingPurchases.
/**
* Gets a list of purchases that haven't yet been sent to the server.
* @return
*/
private List<Receipt> getPendingPurchases() {
synchronized (PENDING_PURCHASE_KEY) {
Storage s = Storage.getInstance();
Util.register(new Receipt());
if (s.exists(PENDING_PURCHASE_KEY)) {
return (List<Receipt>) s.readObject(PENDING_PURCHASE_KEY);
} else {
return new ArrayList<Receipt>();
}
}
}
use of com.codename1.ui.List in project CodenameOne by codenameone.
the class Purchase method addPendingPurchase.
/**
* Adds a receipt to be pushed to the server.
* @param receipt
*/
private void addPendingPurchase(Receipt receipt) {
synchronized (PENDING_PURCHASE_KEY) {
Storage s = Storage.getInstance();
List<Receipt> pendingPurchases = getPendingPurchases();
pendingPurchases.add(receipt);
s.writeObject(PENDING_PURCHASE_KEY, pendingPurchases);
}
}
Aggregations