Skip to content

Commit

Permalink
prevent non-whitelisted ids to be affected by split chat (in theory a…
Browse files Browse the repository at this point in the history
…t least)
  • Loading branch information
FoseFx committed Nov 14, 2021
1 parent 896796a commit a9772d5
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions mod/app/src/main/java/bttv/SplitChat.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package bttv;

import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -32,31 +34,63 @@ public static void setBackgroundColor(int position, RecyclerView.ViewHolder view
return;
}
View view = viewHolder.itemView;

// make sure we only change chat message items
boolean hasChatMessageId = view.getId() == ResUtil.getResourceId(view.getContext(), "chat_message_item", "id");
boolean hasChommentRootId = view.getId() == ResUtil.getResourceId(view.getContext(), "chomment_root_view", "id");

if (!hasChatMessageId && !hasChommentRootId) {
Log.i(TAG, "view skipped, as it's not a chat message or chomment" + viewHolder.toString());
reset(view);
return;
}

int color = isDarkThemeEnabled()
? ResUtil.getColorValue("material_grey_900")
: ResUtil.getColorValue("material_grey_300");

boolean doChange = position % 2 == 1;

// fix VODs
if (view.getId() == ResUtil.getResourceId(Data.ctx, "chomment_root_view", "id")) {
// for some reason we can't set the background for the whole view for Chomments (VODs)
// so we just highlight the TextView (first child)
if (hasChommentRootId) {
LinearLayout linearLayout = (LinearLayout) view;
view = linearLayout.getChildAt(0);
}

if (doChange) {
view.setBackgroundColor(color);
} else {
view.setBackground(null);
reset(view);
}

//
// fix width
//

// MATCH_PARENT is default for Chomments, WRAP_CONTENT for Comments
boolean matchParent = doChange || viewHolder instanceof ChommentItemViewHolder;
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
layoutParams.width = matchParent ? ViewGroup.LayoutParams.MATCH_PARENT : ViewGroup.LayoutParams.WRAP_CONTENT;
view.setLayoutParams(layoutParams);
}

private static void reset(View view) {
Drawable background = view.getBackground();
if (background == null) {
return;
}
if (!(background instanceof ColorDrawable)) {
return;
}

ColorDrawable colorDrawable = (ColorDrawable) background;
if (colorDrawable.getColor() == ResUtil.getColorValue("material_grey_900")
|| colorDrawable.getColor() == ResUtil.getColorValue("material_grey_300")) {
view.setBackground(null);
}
}

private static boolean isDarkThemeEnabled() {
return Data.ctx
.getSharedPreferences("tv.twitch.bttvandroid.app_preferences", 0)
Expand Down

0 comments on commit a9772d5

Please sign in to comment.