Эх сурвалжийг харах

draw status in ChooseAccountDialogFragment

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
Marcel Hibbe 3 жил өмнө
parent
commit
967b63b017

+ 139 - 0
app/src/main/java/com/nextcloud/talk/ui/StatusDrawable.java

@@ -0,0 +1,139 @@
+/*
+ * Nextcloud Android client application
+ *
+ * @author Tobias Kaminsky
+ * Copyright (C) 2020 Tobias Kaminsky
+ * Copyright (C) 2020 Nextcloud GmbH
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package com.nextcloud.talk.ui;
+
+import android.content.Context;
+import android.graphics.Canvas;
+import android.graphics.Color;
+import android.graphics.ColorFilter;
+import android.graphics.Paint;
+import android.graphics.PixelFormat;
+import android.graphics.drawable.Drawable;
+import android.text.TextUtils;
+
+import com.nextcloud.talk.R;
+
+import androidx.annotation.DrawableRes;
+import androidx.annotation.NonNull;
+import androidx.core.content.res.ResourcesCompat;
+
+/**
+ * A Drawable object that draws a status
+ */
+public class StatusDrawable extends Drawable {
+    private String text;
+    private @DrawableRes int icon = -1;
+    private Paint textPaint;
+    private Paint backgroundPaint;
+    private final float radius;
+    private Context context;
+    private final static int whiteBackground = Color.argb(200, 255, 255, 255);
+    private final static int onlineStatus = Color.argb(255, 73, 179, 130);
+
+    public StatusDrawable(String status, String statusIcon, float statusSize, Context context) {
+        backgroundPaint = new Paint();
+        backgroundPaint.setStyle(Paint.Style.FILL);
+        backgroundPaint.setAntiAlias(true);
+
+        radius = statusSize;
+
+        if (TextUtils.isEmpty(statusIcon)) {
+            switch (status) {
+                case "dnd":
+                    icon = R.drawable.ic_user_status_dnd;
+                    backgroundPaint.setColor(whiteBackground);
+                    this.context = context;
+                    break;
+
+                case "online":
+                    backgroundPaint.setColor(onlineStatus);
+                    break;
+
+                case "away":
+                    icon = R.drawable.ic_user_status_away;
+                    backgroundPaint.setColor(whiteBackground);
+                    this.context = context;
+                    break;
+
+                default:
+                    // do not show
+                    backgroundPaint = null;
+                    break;
+            }
+        } else {
+            text = statusIcon;
+
+            backgroundPaint.setColor(whiteBackground);
+
+            textPaint = new Paint();
+            textPaint.setColor(Color.WHITE);
+            textPaint.setTextSize(statusSize);
+            textPaint.setAntiAlias(true);
+            textPaint.setTextAlign(Paint.Align.CENTER);
+        }
+    }
+
+    /**
+     * Draw in its bounds (set via setBounds) respecting optional effects such as alpha (set via setAlpha) and color
+     * filter (set via setColorFilter) a circular background with a user's first character.
+     *
+     * @param canvas The canvas to draw into
+     */
+    @Override
+    public void draw(@NonNull Canvas canvas) {
+        if (backgroundPaint != null) {
+            canvas.drawCircle(radius, radius, radius, backgroundPaint);
+        }
+
+        if (text != null) {
+            textPaint.setTextSize(1.6f * radius);
+            canvas.drawText(text, radius, radius - ((textPaint.descent() + textPaint.ascent()) / 2), textPaint);
+        }
+
+        if (icon != -1) {
+            Drawable drawable = ResourcesCompat.getDrawable(context.getResources(), icon, null);
+
+            if (drawable != null) {
+                drawable.setBounds(0,
+                                   0,
+                                   (int) (2 * radius),
+                                   (int) (2 * radius));
+                drawable.draw(canvas);
+            }
+        }
+    }
+
+    @Override
+    public void setAlpha(int alpha) {
+        textPaint.setAlpha(alpha);
+    }
+
+    @Override
+    public void setColorFilter(ColorFilter cf) {
+        textPaint.setColorFilter(cf);
+    }
+
+    @Override
+    public int getOpacity() {
+        return PixelFormat.TRANSLUCENT;
+    }
+}

+ 23 - 1
app/src/main/java/com/nextcloud/talk/ui/dialog/ChooseAccountDialogFragment.java

@@ -49,6 +49,7 @@ import com.nextcloud.talk.models.database.UserEntity;
 import com.nextcloud.talk.models.json.participants.Participant;
 import com.nextcloud.talk.models.json.status.Status;
 import com.nextcloud.talk.models.json.status.StatusOverall;
+import com.nextcloud.talk.ui.StatusDrawable;
 import com.nextcloud.talk.utils.ApiUtils;
 import com.nextcloud.talk.utils.DisplayUtils;
 import com.nextcloud.talk.utils.database.user.UserUtils;
@@ -74,6 +75,8 @@ import io.reactivex.schedulers.Schedulers;
 public class ChooseAccountDialogFragment extends DialogFragment {
     private static final String TAG = ChooseAccountDialogFragment.class.getSimpleName();
 
+    private static final float STATUS_SIZE_IN_DP = 9f;
+
     @Inject
     UserUtils userUtils;
 
@@ -214,6 +217,7 @@ public class ChooseAccountDialogFragment extends DialogFragment {
                 public void onNext(@NonNull StatusOverall statusOverall) {
                     status = statusOverall.ocs.data;
                     binding.setStatus.setEnabled(true);
+                    drawStatus();
                 }
 
                 @Override
@@ -300,5 +304,23 @@ public class ChooseAccountDialogFragment extends DialogFragment {
             }
         };
 
-
+    private void drawStatus() {
+        float size = DisplayUtils.convertDpToPixel(STATUS_SIZE_IN_DP, getContext());
+        binding.currentAccount.ticker.setBackground(null);
+        binding.currentAccount.ticker.setImageDrawable(new StatusDrawable(
+            status.getStatus(),
+            status.getIcon(),
+            size,
+            getContext()));
+        binding.currentAccount.ticker.setVisibility(View.VISIBLE);
+
+
+        if (status.getMessage() != null && !status.getMessage().isEmpty()){
+            binding.currentAccount.status.setText(status.getMessage());
+            binding.currentAccount.status.setVisibility(View.VISIBLE);
+        } else {
+            binding.currentAccount.status.setText("");
+            binding.currentAccount.status.setVisibility(View.GONE);
+        }
+    }
 }