Skip to content

Commit

Permalink
fix: display partner name in private channel tab
Browse files Browse the repository at this point in the history
  • Loading branch information
Silthus committed Feb 17, 2022
1 parent 63bf73e commit d26e0f9
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

package net.silthus.schat.commands;

import java.util.Set;
import net.silthus.schat.channel.Channel;
import net.silthus.schat.channel.ChannelRepository;
import net.silthus.schat.chatter.Chatter;
Expand Down Expand Up @@ -139,7 +140,7 @@ class given_private_channel_exists {

@BeforeEach
void setUp() {
channel = channelWith(PRIVATE, true);
channel = channelWith(Set.of(source, target).hashCode() + "", PRIVATE, true);
channel.addTarget(source);
channel.addTarget(target);
repository.add(channel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.lang.reflect.Type;
import net.silthus.schat.channel.Channel;
import net.silthus.schat.channel.ChannelRepository;
import net.silthus.schat.message.Targets;
import net.silthus.schat.pointer.Settings;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -106,7 +107,11 @@ private static class SettingsDeserializationContextStub implements JsonDeseriali
@Override
@SuppressWarnings("unchecked")
public <T> T deserialize(JsonElement jsonElement, Type type) throws JsonParseException {
return (T) Settings.createSettings();
if (type.equals(Settings.class))
return (T) Settings.createSettings();
if (type.equals(Targets.class))
return (T) new Targets();
return null;
}
}
}
26 changes: 24 additions & 2 deletions ui/src/main/java/net/silthus/schat/ui/format/ChannelFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,17 @@
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration;
import net.silthus.schat.channel.Channel;
import net.silthus.schat.chatter.Chatter;
import net.silthus.schat.identity.Identified;
import net.silthus.schat.pointer.Configured;
import net.silthus.schat.pointer.Setting;
import net.silthus.schat.pointer.Settings;
import net.silthus.schat.ui.Click;
import org.jetbrains.annotations.NotNull;

import static net.kyori.adventure.text.Component.text;
import static net.kyori.adventure.text.format.NamedTextColor.WHITE;
import static net.silthus.schat.channel.ChannelSettings.PRIVATE;

@Getter
@Accessors(fluent = true)
Expand All @@ -47,14 +51,16 @@ public class ChannelFormat implements Format<Channel>, Configured {
public static final Setting<TextDecoration> DECORATION = Setting.setting(TextDecoration.class, "style", null);
public static final Setting<Click.Channel> ON_CLICK = Setting.setting(Click.Channel.class, "on_click", null);

private final Chatter chatter;
private final Settings settings;

public ChannelFormat(Settings settings) {
public ChannelFormat(Chatter chatter, Settings settings) {
this.chatter = chatter;
this.settings = settings;
}

public Component format(Channel channel) {
TextComponent.Builder builder = text().append(channel.displayName());
TextComponent.Builder builder = text().append(channelName(channel));
if (get(COLOR) != null)
builder.color(get(COLOR));
if (get(DECORATION) != null)
Expand All @@ -63,4 +69,20 @@ public Component format(Channel channel) {
builder.clickEvent(get(ON_CLICK).onClick(channel));
return builder.build();
}

@NotNull
private Component channelName(Channel channel) {
return channel.is(PRIVATE) ? privateChannelName(channel) : channel.displayName();
}

@NotNull
private Component privateChannelName(Channel channel) {
return channel.targets().stream()
.filter(target -> target instanceof Chatter)
.filter(target -> !target.equals(chatter))
.findFirst()
.map(target -> (Chatter) target)
.map(Identified::displayName)
.orElse(channel.displayName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ private List<Component> getRenderedChannels() {
final ArrayList<Component> channels = new ArrayList<>();
for (final Channel channel : viewModel.channels()) {
if (viewModel.isActiveChannel(channel))
channels.add(new ChannelFormat(get(ACTIVE_CHANNEL)).format(channel));
channels.add(new ChannelFormat(viewModel.chatter(), get(ACTIVE_CHANNEL)).format(channel));
else
channels.add(new ChannelFormat(get(INACTIVE_CHANNEL)).format(channel));
channels.add(new ChannelFormat(viewModel.chatter(), get(INACTIVE_CHANNEL)).format(channel));
}
return channels;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import net.silthus.schat.channel.Channel;
import net.silthus.schat.chatter.Chatter;
import net.silthus.schat.chatter.ChatterMock;
import net.silthus.schat.identity.Identity;
import net.silthus.schat.message.Message;
import net.silthus.schat.ui.view.View;
Expand All @@ -42,10 +43,12 @@

import static net.kyori.adventure.text.format.NamedTextColor.RED;
import static net.silthus.schat.AssertionHelper.assertNPE;
import static net.silthus.schat.channel.ChannelSettings.PRIORITY;
import static net.silthus.schat.channel.Channel.createChannel;
import static net.silthus.schat.channel.ChannelHelper.ConfiguredSetting.set;
import static net.silthus.schat.channel.ChannelHelper.channelWith;
import static net.silthus.schat.channel.ChannelSettings.PRIORITY;
import static net.silthus.schat.channel.ChannelSettings.PRIVATE;
import static net.silthus.schat.chatter.ChatterMock.chatterMock;
import static net.silthus.schat.chatter.ChatterMock.randomChatter;
import static net.silthus.schat.identity.Identity.identity;
import static net.silthus.schat.message.Message.message;
Expand Down Expand Up @@ -178,7 +181,6 @@ void renders_both_messages() {
}

@Nested class given_single_channel {

private Channel channel;

@BeforeEach
Expand Down Expand Up @@ -225,6 +227,22 @@ void then_channel_click_executes_join_command() {
}
}

@Nested class given_private_channel {

@BeforeEach
void setUp() {
ChatterMock target = chatterMock(Identity.identity("target"));
Channel channel = channelWith(PRIVATE, true);
chatter.join(channel);
target.join(channel);
}

@Test
void renders_partner_name() {
assertTextRenders("| target |");
}
}

@Nested class given_two_channels {

private @NotNull Channel channelOne;
Expand Down

0 comments on commit d26e0f9

Please sign in to comment.