Skip to content
This repository has been archived by the owner on Dec 11, 2024. It is now read-only.

feat(YouTube - Searchbar): Restyle #17

Merged
merged 2 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ private static boolean anyEquals(int value, int... of) {
for (int v : of) if (value == v) return true;
return false;
}

public static String getLightBackgroundHexValue() {
return String.format("#%06X", (0xFFFFFF & getWhiteColor()));
}

public static String getDarkBackgroundHexValue() {
return String.format("#%06X", (0xFFFFFF & getBlackColor()));
}
}


Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package app.revanced.integrations.youtube.utils;

import static app.revanced.integrations.shared.utils.ResourceUtils.getColor;
import static app.revanced.integrations.shared.utils.ResourceUtils.getDrawable;
import static app.revanced.integrations.shared.utils.ResourceUtils.getStyleIdentifier;

import android.graphics.drawable.Drawable;

import static app.revanced.integrations.shared.utils.ResourceUtils.*;

@SuppressWarnings("unused")
public class ThemeUtils {
private static int themeValue;
Expand Down Expand Up @@ -66,4 +64,35 @@ public static int getToolbarBackgroundColor() {

return getColor(colorName);
}

// Convert HEX to RGB
private static int[] hexToRgb(String hex) {
int r = Integer.valueOf(hex.substring(1, 3), 16);
int g = Integer.valueOf(hex.substring(3, 5), 16);
int b = Integer.valueOf(hex.substring(5, 7), 16);
return new int[]{r, g, b};
}

// Convert RGB to HEX
private static String rgbToHex(int r, int g, int b) {
return String.format("#%02x%02x%02x", r, g, b);
}

// Darken color by percentage
public static String darkenColor(String hex, double percentage) {
int[] rgb = hexToRgb(hex);
int r = (int) (rgb[0] * (1 - percentage / 100));
int g = (int) (rgb[1] * (1 - percentage / 100));
int b = (int) (rgb[2] * (1 - percentage / 100));
return rgbToHex(r, g, b);
}

// Lighten color by percentage
public static String lightenColor(String hex, double percentage) {
int[] rgb = hexToRgb(hex);
int r = (int) (rgb[0] + (255 - rgb[0]) * (percentage / 100));
int g = (int) (rgb[1] + (255 - rgb[1]) * (percentage / 100));
int b = (int) (rgb[2] + (255 - rgb[2]) * (percentage / 100));
return rgbToHex(r, g, b);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
Expand All @@ -10,16 +12,16 @@
import android.widget.SearchView.OnQueryTextListener;
import android.widget.TextView;
import android.widget.Toolbar;

import java.lang.ref.WeakReference;
import java.util.Objects;

import app.revanced.integrations.shared.utils.Logger;
import app.revanced.integrations.shared.utils.ResourceUtils;
import app.revanced.integrations.shared.utils.Utils;
import app.revanced.integrations.youtube.patches.utils.DrawableColorPatch;
import app.revanced.integrations.youtube.settings.preference.ReVancedPreferenceFragment;
import app.revanced.integrations.youtube.utils.ThemeUtils;

import java.lang.ref.WeakReference;
import java.util.Objects;

@SuppressWarnings("deprecation")
public class VideoQualitySettingsActivity extends Activity {

Expand Down Expand Up @@ -75,6 +77,56 @@ protected void onCreate(Bundle bundle) {

// Set search view
SearchView searchView = findViewById(ResourceUtils.getIdIdentifier("search_view"));

// region SearchView dimensions
// Get the current layout parameters of the SearchView
ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) searchView.getLayoutParams();

// Set the margins (in pixels)
int margin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, getResources().getDisplayMetrics()); // for example, 10dp
layoutParams.setMargins(margin, layoutParams.topMargin, margin, layoutParams.bottomMargin);

// Apply the layout parameters to the SearchView
searchView.setLayoutParams(layoutParams);

// endregion

// region SearchView color

GradientDrawable shape = new GradientDrawable();

String currentDarkHex = DrawableColorPatch.getDarkBackgroundHexValue();
String currentLightHex = DrawableColorPatch.getLightBackgroundHexValue();

String currentHex = ThemeUtils.isDarkTheme() ? currentDarkHex : currentLightHex;
String defaultHex = ThemeUtils.isDarkTheme() ? "#1A1A1A" : "#E5E5E5";

String finalHex;
if (currentHex.equals(ThemeUtils.isDarkTheme() ? "#000000" : "#FFFFFF")) {
shape.setColor(Color.parseColor(defaultHex)); // stock black/white color
finalHex = defaultHex;
} else {
// custom color theme
String adjustedColor = ThemeUtils.isDarkTheme()
? ThemeUtils.lightenColor(currentHex, 15)
: ThemeUtils.darkenColor(currentHex, 15);
shape.setColor(Color.parseColor(adjustedColor));
finalHex = adjustedColor;
}
Logger.printInfo(() -> "searchbar color: " + finalHex);

shape.setCornerRadius(30 * getResources().getDisplayMetrics().density);
searchView.setBackground(shape);

/*
* in order to match the original app's search bar, we'd need to change the searchbar cursor color
* to white (#FFFFFF) if ThemeUtils.isDarkTheme(), and black (#000000) if not.
*/

// endregion

searchView.setPadding(20, 15, 20, 15);

searchView.setOnQueryTextListener(onQueryTextListener);
searchViewRef = new WeakReference<>(searchView);
} catch (Exception ex) {
Expand Down
Loading