refactor: Simplify

This commit is contained in:
LisoUseInAIKyrios 2024-04-10 00:04:33 +04:00
parent 14527a2638
commit c169b9f332

View File

@ -8,6 +8,8 @@ import android.view.View;
import androidx.annotation.Nullable;
import java.lang.ref.WeakReference;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
@ -50,29 +52,37 @@ public final class NavigationBar {
* How long to wait for the set nav button latch to be released. Maximum wait time must
* be as small as possible while still allowing enough time for the nav bar to update.
*
* YT calls it's back button handlers out of order, and litho starts
* filtering the previous screen before the navigation bar is updated.
* YT calls it's back button handlers out of order,
* and litho starts filtering before the navigation bar is updated.
*
* Fixing this situation and not needlessly wait requires somehow detecting if a back button
* key-press will cause a tab change. Typically after pressing the back button, the time
* between the first litho event and the when the nav button is updated is about 10-20ms.
* Fixing this situation and not needlessly waiting requires somehow
* detecting if a back button key-press will cause a tab change.
*
* Using 50-100ms here should be enough time and not noticeable, since YT typically takes
* 100-200ms (or more) just to update the view anyways.
* Typically after pressing the back button, the time between the first litho event and
* when the nav button is updated is about 10-20ms. Using 50-100ms here should be enough time
* and not noticeable, since YT typically takes 100-200ms (or more) to update the view anyways.
*
* This issue can also be avoided on a patch by patch basis, by avoiding calls to
* {@link NavigationButton#getSelectedNavigationButton()} unless absolutely necessary.
*/
private static final long LATCH_AWAIT_TIMEOUT_MILLISECONDS = 50;
private static final long LATCH_AWAIT_TIMEOUT_MILLISECONDS = 75;
/**
* Used as a workaround to fix the issue of YT calling back button handlers out of order.
* Used to hold calls to {@link NavigationButton#getSelectedNavigationButton()}
* until the current navigation button can be determined.
*
* Only used when the hardware back button is pressed.
*/
@Nullable
private static volatile CountDownLatch navButtonLatch;
/**
* Map of nav button layout views to Enum type.
* No synchronization is needed, and this is always accessed from the main thread.
*/
private static final Map<View, NavigationButton> viewToButtonMap = new IdentityHashMap<>();
static {
// On app startup litho can start before the navigation bar is initialized.
// Force it to wait until the nav bar is updated.
@ -100,15 +110,15 @@ public final class NavigationBar {
if (Utils.isCurrentlyOnMainThread()) {
// The latch is released from the main thread, and waiting from the main thread will always timeout.
// This situation has only been observed when navigating out of a submenu and not changing tabs.
// and for those cases the nav bar did not change anyways so it's safe to return.
// and for that use case the nav bar does not change so it's safe to return here.
Logger.printDebug(() -> "Cannot block main thread waiting for nav button. Using last known navbar button status.");
return;
}
try {
Logger.printDebug(() -> "Waiting for navbar button latch");
Logger.printDebug(() -> "Latch wait started");
if (latch.await(LATCH_AWAIT_TIMEOUT_MILLISECONDS, TimeUnit.MILLISECONDS)) {
Logger.printDebug(() -> "Latch waiting complete");
Logger.printDebug(() -> "Latch wait complete");
return;
}
@ -118,15 +128,16 @@ public final class NavigationBar {
Logger.printDebug(() -> "Latch wait timed out");
} catch (InterruptedException ex) {
Logger.printException(() -> "Wait interrupted", ex); // Will never happen.
Logger.printException(() -> "Latch wait interrupted failure", ex); // Will never happen.
}
}
/**
* Last YT navigation enum loaded. Not necessarily the active navigation tab.
* Always accessed from the main thread.
*/
@Nullable
private static volatile String lastYTNavigationEnumName;
private static String lastYTNavigationEnumName;
/**
* Injection point.
@ -147,7 +158,7 @@ public final class NavigationBar {
for (NavigationButton button : NavigationButton.values()) {
if (button.ytEnumName.equals(lastEnumName)) {
Logger.printDebug(() -> "navigationTabLoaded: " + lastEnumName);
button.buttonLayoutRef = new WeakReference<>(navigationButtonGroup);
viewToButtonMap.put(navigationButtonGroup, button);
navigationTabCreatedCallback(button, navigationButtonGroup);
return;
}
@ -185,29 +196,28 @@ public final class NavigationBar {
*/
public static void navigationTabSelected(View navButtonImageView, boolean isSelected) {
try {
for (NavigationButton button : NavigationButton.values()) {
View buttonView = button.buttonLayoutRef.get();
NavigationButton button = viewToButtonMap.get(navButtonImageView);
if (buttonView == navButtonImageView) {
if (isSelected) {
NavigationButton.selectedNavigationButton = button;
// Wake up any threads waiting to return the currently selected nav button.
releaseNavButtonLatch();
Logger.printDebug(() -> "Changed to navigation button: " + button);
} else if (NavigationButton.selectedNavigationButton == button) {
NavigationButton.selectedNavigationButton = null;
Logger.printDebug(() -> "Navigated away from button: " + button);
}
return;
if (button == null) { // An unknown tab was selected.
// Show a toast only if debug mode is enabled.
if (BaseSettings.DEBUG.get()) {
Logger.printException(() -> "Unknown navigation view selected: " + navButtonImageView);
}
NavigationButton.selectedNavigationButton = null;
return;
}
if (BaseSettings.DEBUG.get()) {
// An unknown tab was selected. Only show a toast is debug mode is enabled.
Logger.printException(() -> "Unknown navigation view selected: " + navButtonImageView);
if (isSelected) {
NavigationButton.selectedNavigationButton = button;
Logger.printDebug(() -> "Changed to navigation button: " + button);
// Release any threads waiting for the selected nav button.
releaseNavButtonLatch();
} else if (NavigationButton.selectedNavigationButton == button) {
NavigationButton.selectedNavigationButton = null;
Logger.printDebug(() -> "Navigated away from button: " + button);
}
NavigationButton.selectedNavigationButton = null;
} catch (Exception ex) {
Logger.printException(() -> "navigationTabSelected failure", ex);
}
@ -276,12 +286,13 @@ public final class NavigationBar {
*
* All code calling this method should handle a null return value.
*
* <b>Due to issues with how YT processes hardware back button events,
* this patch uses workarounds that can cause this method to take up to 50ms.</b>
* <b>Due to issues with how YT processes physical back button events,
* this patch uses workarounds that can cause this method to take up to 75ms
* if the device back button was recently pressed.</b>
*
* @return The active navigation tab.
* If the user is in the upload video UI, this returns tab currently selected
* on screen (whatever tab the user was on before tapping the upload nav button).
* If the user is in the upload video UI, this returns tab that is still visually
* selected on screen (whatever tab the user was on before tapping the upload button).
*/
@Nullable
public static NavigationButton getSelectedNavigationButton() {
@ -293,7 +304,6 @@ public final class NavigationBar {
* YouTube enum name for this tab.
*/
private final String ytEnumName;
private volatile WeakReference<View> buttonLayoutRef = new WeakReference<>(null);
NavigationButton(String ytEnumName) {
this.ytEnumName = ytEnumName;