Skip to content
This repository was archived by the owner on Jun 9, 2023. It is now read-only.

Conversation

@mvietri
Copy link

@mvietri mvietri commented Aug 18, 2019

Toggle added to Look & Feel Preferences.

toggle

Lines

@tunous-bot
Copy link

Test version of Dank has been automatically built from this pull request.
Click here to download it.

Copy link
Owner

@Tunous Tunous left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks nice so far. I've left some feedback with suggestions with what to change.

I don't think that there is a need for allowing users to change the colors to custom ones. We just have to find some nice defaults that will work with the current style of the app. @dotvhs I'll mention you here as you might be able to find good colors for that :)

@Tunous Tunous added the feature New feature label Aug 18, 2019
@tunous-bot
Copy link

Test version of Dank has been automatically built from this pull request.
Click here to download it.

@tunous-bot
Copy link

Test version of Dank has been automatically built from this pull request.
Click here to download it.

Copy link
Owner

@Tunous Tunous left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost there 👍

@tunous-bot
Copy link

Test version of Dank has been automatically built from this pull request.
Click here to download it.

msfjarvis referenced this pull request in msfjarvis/Dawn Aug 19, 2019
Identify parents of comment by using colored lines

* github.com:Tunous/Dank:
  improved legibility and fix performance issues
  Move arraylist initialization to a void method, add preference subscriber and change color pallete
  Inject IntededLayout to use user preferences
  Read value from preferences and fixed
  Add depth color to comment's children and toggle in preferences
@mvietri mvietri marked this pull request as ready for review August 19, 2019 21:25
Copy link
Owner

@Tunous Tunous left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Final run through the code, this time I've mostly checked and listed what Android Studio suggests :)

@Tunous
Copy link
Owner

Tunous commented Aug 20, 2019

I also noticed that the preference change did not work correctly. The tree was being reset on preference change but since the lines were being actually created in onLayout our change from observer only destroyed old lines. I'm attaching a patch which fixes this by only updating colors on preference change instead of recreating whole tree. It contains fixes from my previous comments too. I'm not commiting this myself because I have no idea what colors we should actually use. I'll leave that to you :)

patch
diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml
index ae3de16cfc8d429b134987d02fb1ee7375b2946a..9f7426d8407f679779e59b772d752ebe418dc2ef 100644
--- a/app/src/main/res/values/colors.xml
+++ b/app/src/main/res/values/colors.xml
@@ -94,4 +94,18 @@
   <!-- Text -->
   <color name="text_primary">@color/gray_200</color>
   <color name="text_secondary">@color/gray_500</color>
+
+  <array name="indentation_colors">
+    <item>@color/teal_200</item>
+    <item>@color/teal_300</item>
+    <item>@color/teal_400</item>
+    <item>@color/teal_500</item>
+    <item>@color/teal_600</item>
+    <item>@color/teal_700</item>
+    <item>@color/teal_800</item>
+    <item>@color/teal_A200</item>
+    <item>@color/teal_A400</item>
+    <item>@color/teal_A500</item>
+    <item>@color/teal_A700</item>
+  </array>
 </resources>
diff --git a/app/src/main/java/me/saket/dank/widgets/IndentedLayout.java b/app/src/main/java/me/saket/dank/widgets/IndentedLayout.java
index cd6c171358c196a7fb75ef6c75ed0b2d2476aa31..9a6969af2e7f9a2a9d4d7d7c50c62d145d960450 100644
--- a/app/src/main/java/me/saket/dank/widgets/IndentedLayout.java
+++ b/app/src/main/java/me/saket/dank/widgets/IndentedLayout.java
@@ -7,19 +7,17 @@ import android.graphics.Color;
 import android.graphics.DashPathEffect;
 import android.graphics.Paint;
 import android.graphics.Path;
-import androidx.annotation.IntRange;
+import android.util.AttributeSet;
+import android.util.TypedValue;
+import android.widget.LinearLayout;
+
 import androidx.annotation.Nullable;
 import androidx.annotation.Px;
 import com.f2prateek.rx.preferences2.Preference;
 import com.jakewharton.rxbinding2.view.RxView;
 
-import android.util.AttributeSet;
-import android.util.TypedValue;
-import android.widget.LinearLayout;
-
 import java.util.ArrayList;
 import java.util.List;
-
 import javax.inject.Inject;
 import javax.inject.Named;
 
@@ -30,36 +28,15 @@ public class IndentedLayout extends LinearLayout {
   private static final int DEFAULT_SPACING_PER_DEPTH_DP = 10;
   private static final int DEFAULT_LINE_WIDTH = 6;
 
-  private int indentationDepth;
   private final int spacePerDepthPx;
-  private Paint indentationLinePaint;
-  private int indentationLineWidth;
-  private int defaultIndentationLineColor;
-
-  private int originalPaddingStart;
-
-  private static final String[] INDENTATION_COLORS = {
-      "#FFC40D", // Yellow
-      "#2D89EF", // Blue
-      "#B91D47", // Dark Red
-      "#00ABA9", // Teal
-      "#E3A21A", // Orange
-      "#99B433", // Light Green
-      "#7E3878", // Purple
-      "#FFB300", // Vivid Yellow
-      "#FFFFFF", // White
-      "#00A300", // Green
-      "#2B5797", // Dark Blue
-      "#9F00A7", // Light Purple
-      "#603CBA", // Dark Purple
-      "#EE1111", // Red
-      "#EFF4FF", // Light Blue
-      "#DA532C", // Dark Orange
-      "#FF0097", // Magenta
-      "#1E7145", // Dark Green
-  };
+  private final Paint indentationLinePaint;
+  private final int defaultIndentationLineColor;
+  private final int originalPaddingStart;
+  private final int[] indentationColors;
 
+  private int indentationDepth;
   private List<ColoredTree> trees = new ArrayList<>();
+
   @Inject @Named("show_colored_comments_tree") Preference<Boolean> coloredDepthPreference;
 
   public IndentedLayout(Context context, @Nullable AttributeSet attrs) {
@@ -68,19 +45,12 @@ public class IndentedLayout extends LinearLayout {
 
     Dank.dependencyInjector().inject(this);
 
-    coloredDepthPreference.asObservable()
-        .takeUntil(RxView.detaches(this))
-        .subscribe(o -> {
-          setupDepthLines();
-          invalidate();
-        });
-
-    indentationLinePaint = new Paint();
     originalPaddingStart = getPaddingStart();
+    indentationColors = getResources().getIntArray(R.array.indentation_colors);
 
     TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.IndentedLayout);
     spacePerDepthPx = attributes.getDimensionPixelSize(R.styleable.IndentedLayout_spacePerDepth, dpToPx(DEFAULT_SPACING_PER_DEPTH_DP, context));
-    indentationLineWidth = attributes.getDimensionPixelSize(R.styleable.IndentedLayout_indentationLineWidth, dpToPx(DEFAULT_LINE_WIDTH, context));
+    int indentationLineWidth = attributes.getDimensionPixelSize(R.styleable.IndentedLayout_indentationLineWidth, dpToPx(DEFAULT_LINE_WIDTH, context));
     defaultIndentationLineColor = attributes.getColor(R.styleable.IndentedLayout_indentationLineColor, Color.LTGRAY);
     attributes.recycle();
 
@@ -89,6 +59,13 @@ public class IndentedLayout extends LinearLayout {
     indentationLinePaint.setStrokeWidth(indentationLineWidth);
     indentationLinePaint.setStyle(Paint.Style.FILL_AND_STROKE);
     indentationLinePaint.setPathEffect(new DashPathEffect(new float[] { indentationLineWidth * 2, indentationLineWidth * 2 }, 0));
+
+    coloredDepthPreference.asObservable()
+        .takeUntil(RxView.detaches(this))
+        .subscribe(colored -> {
+          updateLineColors(colored);
+          invalidate();
+        });
   }
 
   @Override
@@ -116,10 +93,10 @@ public class IndentedLayout extends LinearLayout {
     }
   }
 
-  public void setIndentationDepth(@IntRange(from = 0, to = 1) int depth) {
+  public void setIndentationDepth(int depth) {
     indentationDepth = depth;
 
-    setupDepthLines();
+    setupDepthLines(coloredDepthPreference.get());
 
     int indentationSpacing = (int) (indentationDepth * spacePerDepthPx + indentationLinePaint.getStrokeWidth());
     setPaddingRelative(originalPaddingStart + indentationSpacing, getPaddingTop(), getPaddingEnd(), getPaddingBottom());
@@ -127,24 +104,33 @@ public class IndentedLayout extends LinearLayout {
     invalidate();
   }
 
-  private void setupDepthLines() {
+  private void setupDepthLines(Boolean colored) {
     trees = new ArrayList<>();
 
     for (int i = 0; i < indentationDepth; i++) {
-      int colorIndex = i % INDENTATION_COLORS.length;
-      int depthColor = coloredDepthPreference.get() ? Color.parseColor(INDENTATION_COLORS[colorIndex]) : defaultIndentationLineColor;
-
+      int depthColor = getDepthColor(i, colored);
       trees.add(new ColoredTree(depthColor, new Path()));
     }
   }
 
+  private int getDepthColor(int index, Boolean shouldBeColored) {
+    int colorIndex = index % indentationColors.length;
+    return shouldBeColored ? indentationColors[colorIndex] : defaultIndentationLineColor;
+  }
+
+  private void updateLineColors(Boolean colored) {
+    for (int i = 0; i < trees.size(); i++) {
+      trees.get(i).color = getDepthColor(i, colored);
+    }
+  }
+
   @Px
   public static int dpToPx(float dpValue, Context context) {
     return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue, context.getResources().getDisplayMetrics());
   }
 
   private static class ColoredTree {
-    public final int color;
+    public int color;
     public final Path path;
 
     public ColoredTree(int color, Path path) {
Details

@Tunous
Copy link
Owner

Tunous commented Aug 20, 2019

One final optimization is to add skip(1) before takeUntil to the subscription to remove unnecessary view invalidation when the preference is first read as we are only interested in subsequent changes.

@mvietri
Copy link
Author

mvietri commented Aug 21, 2019

Thanks @Tunous . I'll be patching this when I can. Should I go and convert to Kotlin?

@Tunous
Copy link
Owner

Tunous commented Aug 21, 2019

I don't think there is need for that here.

@mvietri
Copy link
Author

mvietri commented Aug 22, 2019

All done

@tunous-bot
Copy link

Test version of Dank has been automatically built from this pull request.
Click here to download it.

@Tunous Tunous added this to the 0.8.0 milestone Aug 22, 2019
@tunous-bot
Copy link

Test version of Dank has been automatically built from this pull request.
Click here to download it.

@Tunous Tunous merged commit 4b21a5f into Tunous:master Aug 22, 2019
@mvietri mvietri deleted the feature/colored_comment_tree branch August 22, 2019 16:51
mvietri pushed a commit to mvietri/Dawn that referenced this pull request Aug 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

feature New feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants