Skip to content

Commit

Permalink
fix(ui): render tab instead of channel and forward pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
Silthus committed Feb 23, 2022
1 parent cc4eeae commit 154e2f0
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ protected void onEnable() {
setupPrototypes();
loadFeatures();

this.channelLoader = new ChannelLoader(this);
this.channelLoader = new ChannelLoader(config(), channelRepository(), logger());
channelLoader.load();

commands = createCommands();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,41 @@
import java.util.HashMap;
import java.util.Map;
import net.silthus.schat.channel.Channel;
import net.silthus.schat.channel.ChannelRepository;
import net.silthus.schat.platform.config.ChannelConfig;
import net.silthus.schat.platform.config.ConfigKeys;
import net.silthus.schat.platform.config.SChatConfig;
import net.silthus.schat.platform.plugin.logging.PluginLogger;
import net.silthus.schat.repository.Repository;

@SuppressWarnings("checkstyle:AvoidEscapedUnicodeCharacters")
class ChannelLoader {
final class ChannelLoader {

private static final String UPDATE_SYMBOL = "\uD83D\uDDD8";
private static final String TRASH_SYMBOL = "\uD83D\uDDD1";
private static final String ERROR_SYMBOL = "✖";
private static final String SUCCESS_SYMBOL = "✔";
public static final String ARROW_SYMBOL = " ➔ ";

private final AbstractSChatServerPlugin server;
private final PluginLogger logger;
private final SChatConfig config;
private final ChannelRepository repository;
private final PluginLogger log;

private int newCount = 0;
private int updateCount = 0;
private int removedCount = 0;
private int errorCount = 0;
private Map<String, ChannelConfig> previousChannels = new HashMap<>();

ChannelLoader(AbstractSChatServerPlugin server) {
this.server = server;
logger = server.logger();
ChannelLoader(SChatConfig config, ChannelRepository repository, PluginLogger logger) {
this.config = config;
this.repository = repository;
this.log = logger;
}

void load() {
final Map<String, ChannelConfig> configs = server.config().get(ConfigKeys.CHANNELS);
logger.info("Loading channels from config...");
final Map<String, ChannelConfig> configs = config.get(ConfigKeys.CHANNELS);
log.info("Loading channels from config...");

removeOldChannels(configs);
loadOrUpdateChannels(configs);
Expand All @@ -67,10 +71,10 @@ void load() {
}

private void printSummary() {
if (newCount > 0) logger.info("... loaded (" + SUCCESS_SYMBOL + ") " + newCount + " new channel(s)");
if (updateCount > 0) logger.info("... updated (" + UPDATE_SYMBOL + ") " + updateCount + " channel(s)");
if (removedCount > 0) logger.info("... removed (" + TRASH_SYMBOL + ") " + removedCount + " channel(s)");
if (errorCount > 0) logger.info("... failed to update or load (" + ERROR_SYMBOL + ") " + errorCount + " channel(s)");
if (newCount > 0) log.info("... loaded (" + SUCCESS_SYMBOL + ") " + newCount + " new channel(s)");
if (updateCount > 0) log.info("... updated (" + UPDATE_SYMBOL + ") " + updateCount + " channel(s)");
if (removedCount > 0) log.info("... removed (" + TRASH_SYMBOL + ") " + removedCount + " channel(s)");
if (errorCount > 0) log.info("... failed to update or load (" + ERROR_SYMBOL + ") " + errorCount + " channel(s)");
resetCounter();
}

Expand All @@ -96,23 +100,23 @@ private void loadOrUpdateChannels(Map<String, ChannelConfig> configs) {
}

private void removeChannel(String oldKey) {
final Channel channel = server.channelRepository().remove(oldKey);
final Channel channel = repository.remove(oldKey);
if (channel != null) {
channel.close();
logger.info("\t" + TRASH_SYMBOL + " " + oldKey);
log.info("\t" + TRASH_SYMBOL + " " + oldKey);
removedCount++;
}
}

private void updateChannel(Map.Entry<String, ChannelConfig> entry) {
try {
server.channelRepository().get(entry.getKey())
repository.get(entry.getKey())
.settings(entry.getValue().settings());
logger.info("\t" + UPDATE_SYMBOL + " " + entry.getKey());
log.info("\t" + UPDATE_SYMBOL + " " + entry.getKey());
updateCount++;
} catch (Repository.NotFound e) {
errorCount++;
logger.info("\t" + UPDATE_SYMBOL + ARROW_SYMBOL + ERROR_SYMBOL + entry.getKey() + ": " + e.getMessage());
log.info("\t" + UPDATE_SYMBOL + ARROW_SYMBOL + ERROR_SYMBOL + entry.getKey() + ": " + e.getMessage());
}
}

Expand All @@ -121,14 +125,14 @@ private void loadChannelFromConfig(ChannelConfig channelConfig) {
tryLoadChannelFromConfig(channelConfig);
} catch (Exception e) {
errorCount++;
logger.info("\t" + ERROR_SYMBOL + " " + channelConfig.key() + ": " + e.getMessage());
log.info("\t" + ERROR_SYMBOL + " " + channelConfig.key() + ": " + e.getMessage());
}
}

private void tryLoadChannelFromConfig(ChannelConfig channelConfig) {
final Channel channel = channelConfig.toChannel();
server.channelRepository().add(channel);
logger.info("\t" + SUCCESS_SYMBOL + " " + channel.key());
repository.add(channel);
log.info("\t" + SUCCESS_SYMBOL + " " + channel.key());
newCount++;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.io.File;
import java.util.Map;
import java.util.Optional;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.minimessage.MiniMessage;
Expand All @@ -33,6 +34,7 @@
import net.silthus.schat.commands.CreatePrivateChannelCommand;
import net.silthus.schat.platform.config.adapter.ConfigurationAdapter;
import net.silthus.schat.ui.View;
import net.silthus.schat.ui.views.tabbed.Tab;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
Expand All @@ -51,7 +53,7 @@
import static net.silthus.schat.platform.config.ConfigKeys.CHANNELS;
import static net.silthus.schat.platform.config.ConfigKeys.VIEW_CONFIG;
import static net.silthus.schat.platform.config.TestConfigurationAdapter.testConfigAdapter;
import static net.silthus.schat.ui.format.Format.ACTIVE_CHANNEL_FORMAT;
import static net.silthus.schat.ui.format.Format.ACTIVE_TAB_FORMAT;
import static net.silthus.schat.ui.format.Format.MESSAGE_FORMAT;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -126,7 +128,9 @@ void uses_default_format_if_no_config_is_set() {
when(view.chatter()).thenReturn(source);
final Channel channel = createPrivateChannel(source, target).channel();
source.activeChannel(channel);
final Component format = config.get(VIEW_CONFIG).privateChatFormat().get(ACTIVE_CHANNEL_FORMAT).format(view, channel);
final Tab tab = mock(Tab.class);
when(tab.get(Tab.CHANNEL)).thenReturn(Optional.of(channel));
final Component format = config.get(VIEW_CONFIG).privateChatFormat().get(ACTIVE_TAB_FORMAT).format(view, tab);
assertThat(FORMATTER.serialize(format)).isEqualTo("<green><underlined>Karl");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package net.silthus.schat.platform.plugin;

import org.junit.jupiter.api.BeforeEach;

class ChannelLoaderTest {

@BeforeEach
void setUp() {
// TODO: Bugs
// - reload von configs überschreibt bestehende settings -> copyFrom verwenden
// - <channel.name> placeholder existiert/funktioniert nicht - default in config fixen
// - global private chat: channel uid angezeigt anstatt display name
}
}
2 changes: 1 addition & 1 deletion platform/src/testFixtures/resources/test-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ channels:
protected: true
auto_join: true
message_format: "<yellow><source.display_name><gray>: <text>"
active_channel_format: "<red><underlined><channel.name>"
active_tab_format: "<red><underlined><channel.name>"
global:
name: Global
settings:
Expand Down
22 changes: 15 additions & 7 deletions ui/src/main/java/net/silthus/schat/ui/ViewConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@
import lombok.Data;
import lombok.experimental.Accessors;
import net.kyori.adventure.text.JoinConfiguration;
import net.silthus.schat.channel.Channel;
import net.silthus.schat.message.Message;
import net.silthus.schat.pointer.Settings;
import net.silthus.schat.ui.util.ViewHelper;
import net.silthus.schat.ui.views.tabbed.Tab;

import static net.kyori.adventure.text.Component.text;
import static net.silthus.schat.ui.format.Format.ACTIVE_CHANNEL_DECORATION;
import static net.silthus.schat.ui.format.Format.ACTIVE_CHANNEL_FORMAT;
import static net.silthus.schat.ui.format.Format.INACTIVE_CHANNEL_DECORATION;
import static net.silthus.schat.ui.format.Format.INACTIVE_CHANNEL_FORMAT;
import static net.silthus.schat.ui.format.Format.ACTIVE_TAB_DECORATION;
import static net.silthus.schat.ui.format.Format.ACTIVE_TAB_FORMAT;
import static net.silthus.schat.ui.format.Format.INACTIVE_TAB_DECORATION;
import static net.silthus.schat.ui.format.Format.INACTIVE_TAB_FORMAT;
import static net.silthus.schat.ui.format.Format.MESSAGE_FORMAT;
import static net.silthus.schat.ui.util.ViewHelper.renderPrivateChannelName;

Expand All @@ -47,8 +47,16 @@ public class ViewConfig {
private Settings format = Settings.createSettings();
private Settings privateChatFormat = Settings.settingsBuilder()
.withStatic(MESSAGE_FORMAT, (view, message) -> ViewHelper.renderPrivateMessage(view.chatter(), (Message) message))
.withStatic(ACTIVE_CHANNEL_FORMAT, (view, channel) -> ACTIVE_CHANNEL_DECORATION.apply(renderPrivateChannelName(view.chatter(), (Channel) channel)))
.withStatic(INACTIVE_CHANNEL_FORMAT, (view, channel) -> INACTIVE_CHANNEL_DECORATION.apply((Channel) channel, renderPrivateChannelName(view.chatter(), (Channel) channel)))
.withStatic(ACTIVE_TAB_FORMAT, (view, tab) ->
tab.get(Tab.CHANNEL)
.map(channel -> ACTIVE_TAB_DECORATION.apply(renderPrivateChannelName(view.chatter(), channel)))
.orElse(text("Unknown"))
)
.withStatic(INACTIVE_TAB_FORMAT, (view, tab) ->
tab.get(Tab.CHANNEL)
.map(channel -> INACTIVE_TAB_DECORATION.apply((Tab) tab, renderPrivateChannelName(view.chatter(), channel)))
.orElse(text("Unknown"))
)
.create();
private JoinConfiguration channelJoinConfig = JoinConfiguration.builder()
.prefix(text("| "))
Expand Down
8 changes: 4 additions & 4 deletions ui/src/main/java/net/silthus/schat/ui/ViewModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import net.silthus.schat.eventbus.EventBus;
import net.silthus.schat.pointer.Settings;

import static net.silthus.schat.ui.format.Format.ACTIVE_CHANNEL_FORMAT;
import static net.silthus.schat.ui.format.Format.INACTIVE_CHANNEL_FORMAT;
import static net.silthus.schat.ui.format.Format.ACTIVE_TAB_FORMAT;
import static net.silthus.schat.ui.format.Format.INACTIVE_TAB_FORMAT;
import static net.silthus.schat.ui.format.Format.MESSAGE_FORMAT;
import static net.silthus.schat.ui.format.Format.SELF_MESSAGE_FORMAT;

Expand Down Expand Up @@ -60,8 +60,8 @@ private void configurePrivateChannel(ViewConfig config) {
PrivateChannel.configure(channel -> channel
.set(MESSAGE_FORMAT, format.get(MESSAGE_FORMAT))
.set(SELF_MESSAGE_FORMAT, format.get(SELF_MESSAGE_FORMAT))
.set(ACTIVE_CHANNEL_FORMAT, format.get(ACTIVE_CHANNEL_FORMAT))
.set(INACTIVE_CHANNEL_FORMAT, format.get(INACTIVE_CHANNEL_FORMAT))
.set(ACTIVE_TAB_FORMAT, format.get(ACTIVE_TAB_FORMAT))
.set(INACTIVE_TAB_FORMAT, format.get(INACTIVE_TAB_FORMAT))
);
}
}
23 changes: 11 additions & 12 deletions ui/src/main/java/net/silthus/schat/ui/format/Format.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
import java.util.function.BiFunction;
import java.util.function.Function;
import net.kyori.adventure.text.Component;
import net.silthus.schat.channel.Channel;
import net.silthus.schat.identity.Identity;
import net.silthus.schat.message.Message;
import net.silthus.schat.pointer.Pointered;
import net.silthus.schat.pointer.Setting;
import net.silthus.schat.ui.View;
import net.silthus.schat.ui.views.tabbed.Tab;

import static net.kyori.adventure.text.Component.text;
import static net.kyori.adventure.text.Component.translatable;
Expand All @@ -41,25 +41,24 @@
import static net.kyori.adventure.text.format.NamedTextColor.GREEN;
import static net.kyori.adventure.text.format.NamedTextColor.YELLOW;
import static net.kyori.adventure.text.format.TextDecoration.UNDERLINED;
import static net.silthus.schat.channel.Channel.DISPLAY_NAME;
import static net.silthus.schat.pointer.Setting.setting;

@FunctionalInterface
public interface Format {
/**
* The default decoration of an active channel.
*/
Function<Component, Component> ACTIVE_CHANNEL_DECORATION = name -> name.colorIfAbsent(GREEN).decorate(UNDERLINED);
Function<Component, Component> ACTIVE_TAB_DECORATION = name -> name.colorIfAbsent(GREEN).decorate(UNDERLINED);
/**
* The default decoration of an inactive channel.
*/
BiFunction<Channel, Component, Component> INACTIVE_CHANNEL_DECORATION = (channel, name) ->
BiFunction<Tab, Component, Component> INACTIVE_TAB_DECORATION = (tab, name) ->
name.colorIfAbsent(GRAY)
.hoverEvent(translatable("schat.hover.join-channel")
.args(channel.get(DISPLAY_NAME))
.args(tab.get(Tab.NAME))
.color(GRAY)
).clickEvent(
clickEvent(RUN_COMMAND, "/channel join " + channel.key())
clickEvent(RUN_COMMAND, "/channel join " + tab.get(Tab.KEY).orElse(""))
);
/**
* The default format of a message.
Expand All @@ -78,16 +77,16 @@ public interface Format {
.append(text(": ", GRAY))
.append(((Message) msg).text().colorIfAbsent(GRAY)));
/**
* The default format of an active channel.
* The default format of an active tab.
*/
Setting<Format> ACTIVE_CHANNEL_FORMAT = setting(Format.class, "active_channel_format", (view, channel) ->
ACTIVE_CHANNEL_DECORATION.apply(((Channel) channel).displayName())
Setting<Format> ACTIVE_TAB_FORMAT = setting(Format.class, "active_tab_format", (view, tab) ->
ACTIVE_TAB_DECORATION.apply(tab.getOrDefault(Tab.NAME, text("Unknown")))
);
/**
* The default format of an inactive channel.
* The default format of an inactive tab.
*/
Setting<Format> INACTIVE_CHANNEL_FORMAT = setting(Format.class, "inactive_channel_format", (view, channel) ->
INACTIVE_CHANNEL_DECORATION.apply((Channel) channel, ((Channel) channel).displayName())
Setting<Format> INACTIVE_TAB_FORMAT = setting(Format.class, "inactive_tab_format", (view, tab) ->
INACTIVE_TAB_DECORATION.apply((Tab) tab, tab.getOrDefault(Tab.NAME, text("Unknown")))
);

Component format(View view, Pointered entity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import net.kyori.adventure.text.TextComponent;
import net.silthus.schat.identity.Identity;
import net.silthus.schat.message.Message;
import net.silthus.schat.pointer.Configured;
import net.silthus.schat.pointer.Settings;
import org.jetbrains.annotations.NotNull;

Expand All @@ -48,7 +47,7 @@
@Getter
@Setter
@Accessors(fluent = true)
public abstract class AbstractTab implements Tab, Configured {
public abstract class AbstractTab implements Tab {
private final TabbedChannelsView view;
private final Settings settings;

Expand Down
20 changes: 15 additions & 5 deletions ui/src/main/java/net/silthus/schat/ui/views/tabbed/ChannelTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import net.kyori.adventure.text.Component;
import net.silthus.schat.channel.Channel;
import net.silthus.schat.message.Message;
import net.silthus.schat.pointer.Settings;

import static net.kyori.adventure.text.Component.empty;
import static net.kyori.adventure.text.Component.translatable;
Expand All @@ -38,8 +39,8 @@
import static net.kyori.adventure.text.format.NamedTextColor.GRAY;
import static net.kyori.adventure.text.format.NamedTextColor.RED;
import static net.silthus.schat.channel.ChannelSettings.FORCED;
import static net.silthus.schat.ui.format.Format.ACTIVE_CHANNEL_FORMAT;
import static net.silthus.schat.ui.format.Format.INACTIVE_CHANNEL_FORMAT;
import static net.silthus.schat.ui.format.Format.ACTIVE_TAB_FORMAT;
import static net.silthus.schat.ui.format.Format.INACTIVE_TAB_FORMAT;

@SuppressWarnings("CheckStyle")
@Getter
Expand All @@ -53,17 +54,26 @@ public class ChannelTab extends AbstractTab {

protected ChannelTab(@NonNull TabbedChannelsView view,
@NonNull Channel channel) {
super(view, channel.settings());
super(view, Settings.settingsBuilder()
.withForward(NAME, channel, Channel.DISPLAY_NAME)
.withForward(KEY, channel, Channel.KEY)
.withStatic(CHANNEL, channel)
.withStatic(VIEWER, view.chatter())
.create()
.copyFrom(channel.settings())
);
this.channel = channel;


}

@Override
public Component renderName() {
final Component name;
if (isActive())
name = get(ACTIVE_CHANNEL_FORMAT).format(view(), channel());
name = get(ACTIVE_TAB_FORMAT).format(view(), this);
else
name = get(INACTIVE_CHANNEL_FORMAT).format(view(), channel());
name = get(INACTIVE_TAB_FORMAT).format(view(), this);

return closeChannel().append(name);
}
Expand Down
Loading

0 comments on commit 154e2f0

Please sign in to comment.