Skip to content

Commit

Permalink
Merge pull request #6 from SDIDSA/online
Browse files Browse the repository at this point in the history
Online
  • Loading branch information
SDIDSA authored Jul 21, 2023
2 parents 1ba7316 + 9882c95 commit d40394d
Show file tree
Hide file tree
Showing 19 changed files with 187 additions and 40 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ android {
minSdk 24
targetSdk 34
versionCode 1
versionName "0.1.0"
versionName "0.1.1"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public BasicApiGet(String path, Param... params) {

public void execute(ObjectConsumer<JSONObject> onResult, String token) throws IOException, ParseException, JSONException {
try {
String uri = new URIBuilder().setPath(path).addParameters(Arrays.stream(params).map(param -> new BasicNameValuePair(param.getKey(), param.getValue())).collect(Collectors.toList())).build().toString().substring(1);
String uri = new URIBuilder().setPath(path).addParameters(Arrays.stream(params).map(param -> new BasicNameValuePair(param.getKey(), param.stringValue())).collect(Collectors.toList())).build().toString().substring(1);
HttpGet httpGet = new HttpGet(uri);
httpGet.addHeader("Accept", "application/json");

Expand Down
19 changes: 6 additions & 13 deletions app/src/main/java/org/luke/diminou/abs/api/json/Param.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,25 @@

import androidx.annotation.NonNull;

import org.apache.hc.core5.http.NameValuePair;
import org.apache.hc.core5.http.message.BasicNameValuePair;

public class Param {
private String key;
private String value;
private final String key;
private final Object value;

public Param(String key, String value) {
public Param(String key, Object value) {
this.key = key;
this.value = value;
}

public Param(String key, int value) {
this(key, Integer.toString(value));
}

public String getKey() {
return key;
}

public String getValue() {
public Object getValue() {
return value;
}

public NameValuePair norm() {
return new BasicNameValuePair(key, value);
public String stringValue() {
return String.valueOf(value);
}

@NonNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ private void navigateInto(Class<? extends Fragment> fragmentType, int direction)
}).start();
}

public boolean isEmpty() {
return loaded == null;
}

public boolean isRunning() {
return running != null && running.isRunning();
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/org/luke/diminou/app/Diminou.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected void postCreate() {
int userId = result.getInt("user");
User.getForId(userId, user -> {
putUser(user);
SessionManager.registerSocket(getMainSocket(), token, String.valueOf(user.getId()));
SessionManager.registerSocket(getMainSocket(), token, user.getId());
loadPage(Home.class);
});
} else {
Expand Down
59 changes: 52 additions & 7 deletions app/src/main/java/org/luke/diminou/app/avatar/AvatarDisplay.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
import android.graphics.drawable.GradientDrawable;

import org.luke.diminou.abs.components.controls.image.ImageProxy;
import org.luke.diminou.abs.components.controls.shape.Rectangle;
import org.luke.diminou.abs.components.layout.StackPane;

import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.LinearLayout;


import androidx.annotation.ColorInt;

import org.luke.diminou.abs.App;
import org.luke.diminou.abs.components.controls.image.Image;
import org.luke.diminou.abs.style.Style;
Expand All @@ -19,14 +25,18 @@
public class AvatarDisplay extends StackPane implements Styleable {
private final App owner;
private final GradientDrawable background;
private final GradientDrawable foreground;

private final Image img;
private final GradientDrawable onlineBack;
private final FrameLayout preOnline;
private final Rectangle online;

public static final int preSize = 64;

private final ChangeListener<String> onUrl;

private final ChangeListener<Boolean> onOnline;

private User old;

public AvatarDisplay(App owner, float sizeDp) {
Expand All @@ -37,21 +47,44 @@ public AvatarDisplay(App owner, float sizeDp) {
setLayoutParams(new LinearLayout.LayoutParams(size, size));

background = new GradientDrawable();
foreground = new GradientDrawable();

int radii = ViewUtils.dipToPx(7, owner);
background.setCornerRadius(radii);
foreground.setCornerRadius(radii);
setBackground(background);
setForeground(foreground);

img = new Image(owner);
img.setSize(sizeDp);
img.setCornerRadius(7);
ViewUtils.setPaddingUnified(img, 1, owner);
img.setCornerRadius(10);

onUrl = (obs, ov, nv) -> ImageProxy.getImage(nv, img::setImageBitmap);
float preOnlineSizeDp = sizeDp / 3f;
int preOnlineSizePx = ViewUtils.dipToPx(preOnlineSizeDp, owner);
float strokeWidthDp = preOnlineSizeDp / 4f;
float onlineSizeDp = preOnlineSizeDp - 2 * strokeWidthDp;

onlineBack = new GradientDrawable();
onlineBack.setCornerRadius(preOnlineSizePx);

preOnline = new FrameLayout(owner);
preOnline.setBackground(onlineBack);
preOnline.setLayoutParams(new LayoutParams(preOnlineSizePx, preOnlineSizePx));
ViewUtils.alignInFrame(preOnline, Gravity.BOTTOM | Gravity.END);
int by = ViewUtils.dipToPx(strokeWidthDp - 1, owner);
preOnline.setTranslationY(by);
preOnline.setTranslationX(by);

online = new Rectangle(owner);
online.setRadius(onlineSizeDp);
online.setSize(onlineSizeDp, onlineSizeDp);
ViewUtils.alignInFrame(online, Gravity.CENTER);

preOnline.addView(online);

addView(img);
addView(preOnline);

onUrl = (obs, ov, nv) -> ImageProxy.getImage(nv, img::setImageBitmap);
onOnline = (obs, ov, nv) -> applyStyle(owner.getStyle());

applyStyle(owner.getStyle());
}
Expand All @@ -74,20 +107,32 @@ public void setValue(Avatar value) {
public void setUser(User user) {
if(old != null) {
old.avatarProperty().removeListener(onUrl);
old.onlineProperty().removeListener(onOnline);
}

old = user;
user.avatarProperty().addListener(onUrl);
user.onlineProperty().addListener(onOnline);
}

public void setValue(String val) {
setValue(Avatar.valueOf(val));
}

public void setOnlineBackground(@ColorInt int color) {
onlineBack.setColor(color);
}

@Override
public void applyStyle(Style style) {
boolean isOnline = (old != null && old.isOnline());
int borderColor = isOnline ? style.getTextPositive() : style.getTextMuted();
preOnline.setVisibility(isOnline ? VISIBLE : INVISIBLE);

background.setColor(style.getBackgroundPrimary());
foreground.setStroke(ViewUtils.dipToPx(1, owner), style.getTextMuted());
background.setStroke(ViewUtils.dipToPx(1, owner), borderColor);
online.setFill(style.getTextPositive());
onlineBack.setStroke(ViewUtils.dipToPx(1, owner), borderColor);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import org.luke.diminou.abs.components.controls.scratches.Orientation;
import org.luke.diminou.abs.components.layout.linear.HBox;
import org.luke.diminou.abs.components.layout.linear.VBox;
import org.luke.diminou.abs.utils.ErrorHandler;
import org.luke.diminou.abs.utils.ViewUtils;
import org.luke.diminou.abs.utils.functional.ObjectConsumer;
import org.luke.diminou.app.cards.offline.OfflineDisplayCards;
import org.luke.diminou.app.cards.offline.OfflinePlayerCard;

Expand Down Expand Up @@ -57,4 +59,14 @@ public void unbind() {
card.unbind();
}
}

public void forEach(ObjectConsumer<PlayerCard> o) {
for(PlayerCard card : cards) {
try {
o.accept(card);
} catch (Exception e) {
ErrorHandler.handle(e, "running cards foreach");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public PlayerCard(App owner, boolean host, int index) {

removeBy = ViewUtils.dipToPx(6, owner);
remove = new ColorIcon(owner, R.drawable.close);
remove.setSize(32);
remove.setSize(28);
remove.setCornerRadius(7);
remove.setTranslationY(-removeBy);
remove.setTranslationX(removeBy);
Expand All @@ -113,7 +113,7 @@ public PlayerCard(App owner, boolean host, int index) {
confirmKick.show();
}
});
ViewUtils.setPaddingUnified(remove, 5, owner);
ViewUtils.setPaddingUnified(remove, 4, owner);
ViewUtils.alignInFrame(remove, Gravity.TOP | Gravity.END);

addView(preAvatar);
Expand All @@ -129,7 +129,7 @@ public PlayerCard(App owner, boolean host, int index) {
}else {
preAvatar.removeAllViews();
preAvatar.addView(avatarDisplay, 0);
if(host) {
if(host || (boundTo != null && boundTo.host)) {
if (nv.isSelf()) remove.setImageResource(R.drawable.owner);
else remove.setImageResource(R.drawable.close);
preAvatar.addView(remove);
Expand Down Expand Up @@ -406,6 +406,8 @@ public void applyStyle(Style style) {
avatarBack.setColor(style.getBackgroundTertiary());
avatarBack.setStroke(ViewUtils.dipToPx(1, getOwner()), style.getTextMuted());

avatarDisplay.setOnlineBackground(style.getBackgroundTertiary());

loading.setFill(style.getTextMuted());

remove.setBackgroundColor(style.getBackgroundTertiary());
Expand Down
20 changes: 18 additions & 2 deletions app/src/main/java/org/luke/diminou/app/pages/home/online/Home.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.luke.diminou.app.pages.home.online;

import android.util.Log;
import android.widget.LinearLayout;

import androidx.core.graphics.Insets;
Expand Down Expand Up @@ -33,6 +34,7 @@
import org.luke.diminou.data.property.Property;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

public class Home extends Page {
Expand Down Expand Up @@ -86,7 +88,9 @@ public void setup() {
content.setScaleX(.7f);
content.setScaleY(.7f);

content.nextInto(Play.class);
if(content.isEmpty()) {
content.nextInto(Play.class);
}


new ParallelAnimation(400)
Expand All @@ -106,12 +110,25 @@ private void registerSocket() {
User user = owner.getUser();
registeredListeners.forEach(owner.getMainSocket()::off);
registeredListeners.clear();

owner.getMainSocket().onAnyIncoming(e -> {
Log.i("received" ,Arrays.toString(e));
});

addSocketEventHandler("user_sync", obj -> {
for (Iterator<String> it = obj.keys(); it.hasNext(); ) {
String key = it.next();
user.set(key, obj.get(key));
}
});
addSocketEventHandler("user_change", obj ->
User.getForId(obj.getInt("user_id"), u -> {
for (Iterator<String> it = obj.keys(); it.hasNext(); ) {
String key = it.next();
if(!key.equals("user_id"))
u.set(key, obj.get(key));
}
}));
addSocketEventHandler("request_sent", obj -> {
int sender = obj.getInt("sender");
int receiver = obj.getInt("receiver");
Expand Down Expand Up @@ -191,7 +208,6 @@ private void registerSocket() {

addSocketEventHandler("leave", data -> {
int userId = data.getInt("user_id");
Room room = new Room(data.getJSONObject("game"));

if(userId != owner.getUser().getId()) {
Page loaded = owner.getLoaded();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ private UserDisplay(App owner, int userId) {
public void applyStyle(Style style) {
root.setBackground(style.getBackgroundTertiary());
root.setBorderColor(style.getTextMuted());
img.setOnlineBackground(style.getBackgroundTertiary());
setBackground(style.getTextMuted());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ public void setup(User user) {
public void applyStyle(Style style) {
newUn.setBackgroundColor(style.getBackgroundTertiary());
newUn.setBorderColor(Color.TRANSPARENT);
pfp.setOnlineBackground(style.getBackgroundPrimary());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.luke.diminou.abs.utils.ErrorHandler;
import org.luke.diminou.abs.utils.Platform;
import org.luke.diminou.abs.utils.Store;
import org.luke.diminou.abs.utils.ViewUtils;
import org.luke.diminou.app.avatar.Avatar;
import org.luke.diminou.app.cards.offline.OfflineDisplayCards;
import org.luke.diminou.app.cards.offline.OfflineMirorredCards;
Expand Down Expand Up @@ -154,7 +155,7 @@ public void setup() {
offlineMirorredCards.bind(cards);

start.setAlpha(0);
start.setTranslationY(40);
start.setTranslationY(ViewUtils.by(owner));
start.setScaleX(.7f);
start.setScaleY(.7f);

Expand Down
Loading

0 comments on commit d40394d

Please sign in to comment.