From 3ef1435e1292c3c2236cfebb7bedbbed1d336816 Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Mon, 28 Dec 2015 13:24:32 +0100 Subject: [PATCH] Display latest image The latest image gets displayed for 60 seconds, after that all images get displayed randomly. --- .../server/CurrentImageURLServiceImpl.java | 50 +++++++++++++++++-- war/FotoStream.css | 1 + 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/src/net/molez/mandlm/fotostream/server/CurrentImageURLServiceImpl.java b/src/net/molez/mandlm/fotostream/server/CurrentImageURLServiceImpl.java index 37207e8..eb06a90 100644 --- a/src/net/molez/mandlm/fotostream/server/CurrentImageURLServiceImpl.java +++ b/src/net/molez/mandlm/fotostream/server/CurrentImageURLServiceImpl.java @@ -1,6 +1,10 @@ package net.molez.mandlm.fotostream.server; import java.io.File; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.Date; +import java.util.List; import java.util.Random; import com.google.gwt.user.server.rpc.RemoteServiceServlet; @@ -10,12 +14,52 @@ import net.molez.mandlm.fotostream.client.CurrentImageURLService; @SuppressWarnings("serial") public class CurrentImageURLServiceImpl extends RemoteServiceServlet implements CurrentImageURLService { + private List imageFiles = new ArrayList(); + + private void readImageFiles() + { + imageFiles.clear(); + + File imageFolder = new File("img/"); + File[] files = imageFolder.listFiles(); + + for (File imageFile : files) + { + if (imageFile.isFile() && imageFile.getName().endsWith(".jpg")) + { + imageFiles.add(imageFile); + } + } + + imageFiles.sort(new Comparator() + { + @Override + public int compare(File file0, File file1) + { + Long file0Date = file0.lastModified(); + Long file1Date = file1.lastModified(); + + return file0Date.compareTo(file1Date); + } + }); + } + public String getCurrentImageURL() { - File imageFolder = new File("img/"); + int newImageSeconds = 60; - File[] fileList = imageFolder.listFiles(); + readImageFiles(); - return fileList[new Random().nextInt(fileList.length)].getPath(); + File latestFile = imageFiles.get(imageFiles.size() - 1); + Date latestFileDate = new Date(latestFile.lastModified()); + + if (latestFileDate.before(new Date(new Date().getTime() - newImageSeconds * 1000))) + { + return imageFiles.get(new Random().nextInt(imageFiles.size())).getPath(); + } + else + { + return latestFile.getPath(); + } } } diff --git a/war/FotoStream.css b/war/FotoStream.css index 64d2075..092d347 100644 --- a/war/FotoStream.css +++ b/war/FotoStream.css @@ -3,4 +3,5 @@ div { padding: 20px; background-color: black; + color: green; } \ No newline at end of file