Skip to content

Commit

Permalink
account selection: refactor to accomodate last changes
Browse files Browse the repository at this point in the history
  • Loading branch information
yuliskov committed Nov 15, 2023
1 parent a68c817 commit b50a604
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.liskovsoft.mediaserviceinterfaces.MediaService;
import com.liskovsoft.sharedutils.mylogger.Log;
import com.liskovsoft.smartyoutubetv2.common.app.presenters.base.BasePresenter;
import com.liskovsoft.smartyoutubetv2.common.app.presenters.dialogs.AccountSelectionPresenter;
import com.liskovsoft.smartyoutubetv2.common.app.views.SignInView;
import com.liskovsoft.smartyoutubetv2.common.app.views.ViewManager;
import com.liskovsoft.sharedutils.rx.RxHelper;
Expand Down Expand Up @@ -72,12 +73,15 @@ private void updateUserCode() {
if (getView() != null) {
getView().close();
}
mBrowsePresenter.refresh();
mSplashPresenter.updateChannels();

// Account history might be turned off (common issue).
GeneralData.instance(getContext()).enableHistory(true);
MediaServiceManager.instance().enableHistory(true);
AccountSelectionPresenter.instance(getContext()).show(true);

//mBrowsePresenter.refresh();
//mSplashPresenter.updateChannels();
//
//// Account history might be turned off (common issue).
//GeneralData.instance(getContext()).enableHistory(true);
//MediaServiceManager.instance().enableHistory(true);
}
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@
import com.liskovsoft.smartyoutubetv2.common.app.models.playback.ui.UiOptionItem;
import com.liskovsoft.smartyoutubetv2.common.app.presenters.AppDialogPresenter;
import com.liskovsoft.smartyoutubetv2.common.app.presenters.BrowsePresenter;
import com.liskovsoft.smartyoutubetv2.common.app.presenters.SplashPresenter;
import com.liskovsoft.smartyoutubetv2.common.app.presenters.base.BasePresenter;
import com.liskovsoft.smartyoutubetv2.common.app.presenters.settings.AccountSettingsPresenter;
import com.liskovsoft.smartyoutubetv2.common.exoplayer.ExoMediaSourceFactory;
import com.liskovsoft.smartyoutubetv2.common.misc.MediaServiceManager;
import com.liskovsoft.smartyoutubetv2.common.prefs.AccountsData;
import com.liskovsoft.sharedutils.rx.RxHelper;
import com.liskovsoft.smartyoutubetv2.common.prefs.GeneralData;
import com.liskovsoft.youtubeapi.service.YouTubeMediaService;
import io.reactivex.disposables.Disposable;

Expand All @@ -24,13 +29,13 @@ public class AccountSelectionPresenter extends BasePresenter<Void> {
private static final String TAG = AccountSelectionPresenter.class.getSimpleName();
@SuppressLint("StaticFieldLeak")
private static AccountSelectionPresenter sInstance;
private final SignInService mSignInManager;
private final SignInService mSignInService;
private Disposable mAccountsAction;

public AccountSelectionPresenter(Context context) {
super(context);
MediaService service = YouTubeMediaService.instance();
mSignInManager = service.getSignInService();
mSignInService = service.getSignInService();
}

public static AccountSelectionPresenter instance(Context context) {
Expand All @@ -53,13 +58,17 @@ public void show(boolean force) {
return;
}

mAccountsAction = mSignInManager.getAccountsObserve()
mAccountsAction = mSignInService.getAccountsObserve()
.subscribe(
accounts -> createAndShowDialog(accounts, force),
error -> Log.e(TAG, "Get accounts error: %s", error.getMessage())
);
}

public void nextAccountOrDialog() {
MediaServiceManager.instance().loadAccounts(this::nextAccountOrDialog);
}

public void unhold() {
RxHelper.disposeActions(mAccountsAction);
sInstance = null;
Expand Down Expand Up @@ -99,9 +108,41 @@ private void appendAccountSelection(List<Account> accounts, AppDialogPresenter s
settingsPresenter.appendRadioCategory(getContext().getString(R.string.dialog_account_list), optionItems);
}

private void selectAccount(Account account) {
mSignInManager.selectAccount(account);
BrowsePresenter.instance(getContext()).refresh();
private void nextAccountOrDialog(List<Account> accounts) {
if (accounts == null || accounts.isEmpty()) {
AccountSettingsPresenter.instance(getContext()).show();
return;
}

Account current = null;

for (Account account : accounts) {
if (account.isSelected()) {
current = account;
break;
}
}

int index = accounts.indexOf(current);

int nextIndex = index + 1;
// null == 'without account'
selectAccount(nextIndex == accounts.size() ? null : accounts.get(nextIndex));
//selectAccount(accounts.get(nextIndex == accounts.size() ? 0 : nextIndex));
}

public void selectAccount(Account account) {
mSignInService.selectAccount(account);
ExoMediaSourceFactory.unhold();
BrowsePresenter.instance(getContext()).refresh(false);
SplashPresenter.instance(getContext()).updateChannels();
//BrowsePresenter.instance(getContext()).onViewInitialized(); // reset state

// Account history might be turned off (common issue).
GeneralData generalData = GeneralData.instance(getContext());
if (generalData.getHistoryState() != GeneralData.HISTORY_AUTO) {
MediaServiceManager.instance().enableHistory(generalData.isHistoryEnabled());
}
}

private String formatAccount(Account account) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.liskovsoft.smartyoutubetv2.common.app.presenters.BrowsePresenter;
import com.liskovsoft.smartyoutubetv2.common.app.presenters.SignInPresenter;
import com.liskovsoft.smartyoutubetv2.common.app.presenters.base.BasePresenter;
import com.liskovsoft.smartyoutubetv2.common.app.presenters.dialogs.AccountSelectionPresenter;
import com.liskovsoft.smartyoutubetv2.common.exoplayer.ExoMediaSourceFactory;
import com.liskovsoft.smartyoutubetv2.common.misc.MediaServiceManager;
import com.liskovsoft.smartyoutubetv2.common.prefs.AccountsData;
Expand Down Expand Up @@ -50,10 +51,6 @@ public void show() {
mMediaServiceManager.loadAccounts(this::createAndShowDialog);
}

public void nextAccountOrDialog() {
mMediaServiceManager.loadAccounts(this::nextAccountOrDialog);
}

private void createAndShowDialog(List<Account> accounts) {
AppDialogPresenter settingsPresenter = AppDialogPresenter.instance(getContext());

Expand All @@ -77,7 +74,7 @@ private void appendSelectAccountSection(List<Account> accounts, AppDialogPresent

optionItems.add(UiOptionItem.from(
getContext().getString(R.string.dialog_account_none), optionItem -> {
selectAccount(null);
AccountSelectionPresenter.instance(getContext()).selectAccount(null);
settingsPresenter.closeDialog();
}, true
));
Expand All @@ -87,7 +84,7 @@ private void appendSelectAccountSection(List<Account> accounts, AppDialogPresent
for (Account account : accounts) {
optionItems.add(UiOptionItem.from(
getFullName(account), option -> {
selectAccount(account);
AccountSelectionPresenter.instance(getContext()).selectAccount(account);
settingsPresenter.closeDialog();
}, account.isSelected()
));
Expand Down Expand Up @@ -152,29 +149,6 @@ private void appendSeparateSettings(AppDialogPresenter settingsPresenter) {
AppPrefs.instance(getContext()).isMultiProfilesEnabled()));
}

private void nextAccountOrDialog(List<Account> accounts) {
if (accounts == null || accounts.isEmpty()) {
createAndShowDialog(accounts);
return;
}

Account current = null;

for (Account account : accounts) {
if (account.isSelected()) {
current = account;
break;
}
}

int index = accounts.indexOf(current);

int nextIndex = index + 1;
// null == 'without account'
selectAccount(nextIndex == accounts.size() ? null : accounts.get(nextIndex));
//selectAccount(accounts.get(nextIndex == accounts.size() ? 0 : nextIndex));
}

private String getFullName(Account account) {
String format;

Expand All @@ -191,13 +165,6 @@ private String getSimpleName(Account account) {
return account.getName() != null ? account.getName() : account.getEmail();
}

private void selectAccount(Account account) {
mMediaServiceManager.getSingInService().selectAccount(account);
ExoMediaSourceFactory.unhold();
BrowsePresenter.instance(getContext()).refresh(false);
//BrowsePresenter.instance(getContext()).onViewInitialized(); // reset state
}

private void removeAccount(Account account) {
mMediaServiceManager.getSingInService().removeAccount(account);
ExoMediaSourceFactory.unhold();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

public class SignInFragment extends GuidedStepSupportFragment implements SignInView {
private static final int CONTINUE = 2;
private static final String SIGN_IN_URL = "https://youtube.com/activate";
private static final String SIGN_IN_URL = "https://yt.be/activate";
private SignInPresenter mSignInPresenter;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.liskovsoft.sharedutils.locale.LocaleUtility;
import com.liskovsoft.smartyoutubetv2.common.app.models.data.Video;
import com.liskovsoft.smartyoutubetv2.common.app.presenters.PlaybackPresenter;
import com.liskovsoft.smartyoutubetv2.common.app.presenters.dialogs.AccountSelectionPresenter;
import com.liskovsoft.smartyoutubetv2.common.app.presenters.settings.AccountSettingsPresenter;
import com.liskovsoft.smartyoutubetv2.common.app.presenters.settings.LanguageSettingsPresenter;
import com.liskovsoft.smartyoutubetv2.common.app.views.PlaybackView;
Expand Down Expand Up @@ -197,7 +198,7 @@ private void setupButtons() {

if (mainUIData.isTopButtonEnabled(MainUIData.TOP_BUTTON_BROWSE_ACCOUNTS)) {
mAccountView = (LongClickSearchOrbView) findViewById(R.id.account_orb);
mAccountView.setOnOrbClickedListener(v -> AccountSettingsPresenter.instance(getContext()).nextAccountOrDialog());
mAccountView.setOnOrbClickedListener(v -> AccountSelectionPresenter.instance(getContext()).nextAccountOrDialog());
mAccountView.setOnOrbLongClickedListener(v -> {
AccountSettingsPresenter.instance(getContext()).show();
return true;
Expand Down

0 comments on commit b50a604

Please sign in to comment.