-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support to show the recent episodes (#24)
Co-authored-by: rick <LinuxSuRen@users.noreply.github.com>
- Loading branch information
1 parent
94d11f6
commit a9b590f
Showing
8 changed files
with
212 additions
and
20 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
src/main/java/listening/linuxsuren/github/io/componet/CachedImage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package listening.linuxsuren.github.io.componet; | ||
|
||
import listening.linuxsuren.github.io.server.CacheServer; | ||
|
||
import javax.imageio.ImageIO; | ||
import javax.swing.*; | ||
import java.awt.*; | ||
import java.awt.image.BufferedImage; | ||
import java.io.IOException; | ||
|
||
public class CachedImage { | ||
public static ImageIcon ScaledImageIcon(String url) { | ||
return ScaledImageIcon(url, 80, 80); | ||
} | ||
|
||
public static ImageIcon ScaledImageIcon(String url, int width, int height) { | ||
try { | ||
BufferedImage image = ImageIO.read(CacheServer.wrapURL(url)); | ||
|
||
return new ImageIcon(image.getScaledInstance(width, height, Image.SCALE_SMOOTH)); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
return null; // TODO provide a default image | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
src/main/java/listening/linuxsuren/github/io/componet/RecentEpisodePanel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
Copyright 2024 LinuxSuRen. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package listening.linuxsuren.github.io.componet; | ||
|
||
import listening.linuxsuren.github.io.service.Episode; | ||
import listening.linuxsuren.github.io.service.SimpleCollectionService; | ||
|
||
import javax.swing.*; | ||
import java.awt.*; | ||
import java.awt.event.MouseAdapter; | ||
import java.awt.event.MouseEvent; | ||
import java.time.ZonedDateTime; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class RecentEpisodePanel extends JPanel implements ReloadAble { | ||
private final JComboBox<RecentType> recentBox = new JComboBox<>(); | ||
private final JPanel centerPanel = new JPanel(); | ||
private List<EpisodeEvent> eventList = new ArrayList<>(); | ||
|
||
public RecentEpisodePanel() { | ||
JPanel toolbar = createToolbar(); | ||
|
||
// set the center panel | ||
centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS)); | ||
|
||
this.setLayout(new BorderLayout()); | ||
this.add(toolbar, BorderLayout.NORTH); | ||
this.add(centerPanel, BorderLayout.CENTER); | ||
} | ||
|
||
private JPanel createToolbar() { | ||
recentBox.addItem(RecentType.Week); | ||
recentBox.addItem(RecentType.BiWeek); | ||
recentBox.addItem(RecentType.Month); | ||
recentBox.addItemListener((e) -> reload()); | ||
|
||
JPanel panel = new JPanel(); | ||
panel.add(recentBox); | ||
return panel; | ||
} | ||
|
||
@Override | ||
public void reload() { | ||
centerPanel.removeAll(); | ||
|
||
new Thread(() -> { | ||
SimpleCollectionService service = new SimpleCollectionService(); | ||
|
||
RecentType recentType = (RecentType) recentBox.getSelectedItem(); | ||
final ZonedDateTime expectedRange = | ||
ZonedDateTime.now().minusDays(recentType == null ? RecentType.Week.getDays() : recentType.getDays()); | ||
service.getAll().forEach(podcast -> { | ||
service.getEpisode(podcast).stream().filter((e) -> e.getPublishDate().isAfter(expectedRange)). | ||
forEach(episode -> { | ||
EpisodeCard card = new EpisodeCard(episode); | ||
card.addTrigger(new MouseAdapter() { | ||
@Override | ||
public void mouseClicked(MouseEvent e) { | ||
eventList.forEach((episodeEvent -> { | ||
episodeEvent.trigger(episode); | ||
})); | ||
} | ||
}); | ||
centerPanel.add(card); | ||
}); | ||
|
||
revalidate(); | ||
}); | ||
}).start(); | ||
} | ||
|
||
public void addEvent(EpisodeEvent e) { | ||
eventList.add(e); | ||
} | ||
} | ||
|
||
class EpisodeCard extends JPanel { | ||
private final JLabel logo = new JLabel(); | ||
|
||
public EpisodeCard(Episode episode) { | ||
JLabel title = new JLabel(); | ||
title.setText(episode.getTitle()); | ||
BorderUtil.setInsideBorder(title, 10); | ||
|
||
logo.setCursor(new Cursor(Cursor.HAND_CURSOR)); | ||
logo.setIcon(CachedImage.ScaledImageIcon(episode.getLogoURL())); | ||
|
||
this.setLayout(new BorderLayout()); | ||
BorderUtil.setInsideBorder(this, 10); | ||
this.add(logo, BorderLayout.WEST); | ||
this.add(title, BorderLayout.CENTER); | ||
} | ||
|
||
public void addTrigger(MouseAdapter e) { | ||
logo.addMouseListener(e); | ||
} | ||
} | ||
|
||
enum RecentType { | ||
Week, BiWeek, Month; | ||
|
||
int getDays() { | ||
switch (this) { | ||
case Week: return 7; | ||
case BiWeek: return 14; | ||
default: return 30; | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/listening/linuxsuren/github/io/componet/ReloadAble.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
Copyright 2024 LinuxSuRen. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package listening.linuxsuren.github.io.componet; | ||
|
||
public interface ReloadAble { | ||
void reload(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters