Skip to content

Commit dcb32a1

Browse files
committed
Added account selector to devices screen.
1 parent 851ba9b commit dcb32a1

6 files changed

Lines changed: 160 additions & 4 deletions

File tree

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package org.telegram.messenger.forkgram
2+
3+
import android.graphics.drawable.Drawable
4+
import android.view.Gravity
5+
import android.view.MotionEvent
6+
import android.view.View
7+
8+
import org.telegram.messenger.AndroidUtilities
9+
import org.telegram.messenger.ImageLocation
10+
import org.telegram.messenger.UserConfig
11+
import org.telegram.messenger.Utilities
12+
import org.telegram.tgnet.TLRPC
13+
import org.telegram.ui.ActionBar.ActionBarMenu
14+
import org.telegram.ui.ActionBar.ActionBarMenuItem
15+
import org.telegram.ui.ActionBar.BaseFragment
16+
import org.telegram.ui.ActionBar.Theme
17+
import org.telegram.ui.Components.AvatarDrawable
18+
import org.telegram.ui.Components.BackupImageView
19+
import org.telegram.ui.Components.ItemOptions
20+
import org.telegram.ui.Components.LayoutHelper
21+
22+
object AccountSelector {
23+
24+
@JvmStatic
25+
fun hasAccountsToSwitch(currentAccount: Int): Boolean {
26+
val accounts = ArrayList<Int>()
27+
HiddenAccountHelper.collectVisibleAccountNumbers(accounts, currentAccount)
28+
return !accounts.isEmpty()
29+
}
30+
31+
@JvmStatic
32+
fun addToMenu(
33+
fragment: BaseFragment,
34+
menu: ActionBarMenu?,
35+
id: Int,
36+
onSelected: Utilities.Callback<Int>
37+
): ActionBarMenuItem? {
38+
val currentAccount = fragment.currentAccount
39+
if (menu == null || !hasAccountsToSwitch(currentAccount)) {
40+
return null
41+
}
42+
43+
val item = menu.addItemWithWidth(id, 0, AndroidUtilities.dp(56f))
44+
45+
val user: TLRPC.User? = UserConfig.getInstance(currentAccount).currentUser
46+
val avatarDrawable = AvatarDrawable()
47+
avatarDrawable.setTextSize(AndroidUtilities.dp(12f))
48+
avatarDrawable.setInfo(currentAccount, user)
49+
50+
val imageView = BackupImageView(menu.context)
51+
imageView.setRoundRadius(AndroidUtilities.dp(18f))
52+
imageView.imageReceiver.setCurrentAccount(currentAccount)
53+
val thumb: Drawable = user?.photo?.strippedBitmap ?: avatarDrawable
54+
imageView.setImage(
55+
ImageLocation.getForUserOrChat(currentAccount, user, ImageLocation.TYPE_SMALL), "50_50",
56+
ImageLocation.getForUserOrChat(user, ImageLocation.TYPE_STRIPPED), "50_50", thumb, user
57+
)
58+
item.addView(imageView, LayoutHelper.createFrame(36, 36, Gravity.CENTER))
59+
60+
var dragOpen: View.OnTouchListener? = null
61+
val rearm = Runnable { AndroidUtilities.runOnUIThread { item.setOnTouchListener(dragOpen) } }
62+
dragOpen = View.OnTouchListener { view, event ->
63+
if (event.actionMasked == MotionEvent.ACTION_MOVE && event.y > view.height) {
64+
view.cancelLongPress()
65+
showPopup(fragment, view, onSelected, rearm)
66+
true
67+
} else {
68+
false
69+
}
70+
}
71+
item.setOnTouchListener(dragOpen)
72+
item.setOnClickListener { view -> showPopup(fragment, view, onSelected, rearm) }
73+
item.setOnLongClickListener { view ->
74+
showPopup(fragment, view, onSelected, rearm)
75+
true
76+
}
77+
return item
78+
}
79+
80+
@JvmStatic
81+
fun showPopup(fragment: BaseFragment, anchor: View, onSelected: Utilities.Callback<Int>) {
82+
showPopup(fragment, anchor, onSelected, null)
83+
}
84+
85+
private fun showPopup(
86+
fragment: BaseFragment,
87+
anchor: View,
88+
onSelected: Utilities.Callback<Int>,
89+
onDismiss: Runnable?
90+
) {
91+
val currentAccount = fragment.currentAccount
92+
93+
val accounts = ArrayList<Int>()
94+
HiddenAccountHelper.collectVisibleAccountNumbers(accounts)
95+
if (!accounts.contains(currentAccount)) {
96+
accounts.add(0, currentAccount)
97+
}
98+
99+
val options = ItemOptions.makeOptions(fragment, anchor)
100+
options.setMinWidth(230)
101+
for (account in accounts) {
102+
options.addAccount(account, account == currentAccount) {
103+
options.dismiss()
104+
if (account != currentAccount) {
105+
onSelected.run(account)
106+
}
107+
}
108+
}
109+
110+
val background = Theme.createRoundRectDrawable(
111+
AndroidUtilities.avatarCornerRadius(AndroidUtilities.dp(48f).toFloat()).toInt(),
112+
fragment.getThemedColor(Theme.key_windowBackgroundWhite)
113+
)
114+
background.paint.setShadowLayer(
115+
AndroidUtilities.dp(6f).toFloat(),
116+
0f,
117+
AndroidUtilities.dp(1f).toFloat(),
118+
Theme.multAlpha(0xFF000000.toInt(), 0.15f)
119+
)
120+
options.setViewAdditionalOffsets(
121+
-AndroidUtilities.dp(4f),
122+
-AndroidUtilities.dp(4f),
123+
-AndroidUtilities.dp(4f),
124+
-AndroidUtilities.dp(4f)
125+
)
126+
options.setScrimViewBackground(background)
127+
options.translate(0f, -AndroidUtilities.dp(4f).toFloat())
128+
options.setGravity(Gravity.RIGHT)
129+
if (onDismiss != null) {
130+
options.setOnDismiss(onDismiss)
131+
}
132+
options.show()
133+
}
134+
}

‎TMessagesProj/src/main/java/org/telegram/ui/ActionBar/Theme.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4575,7 +4575,7 @@ public static Drawable createOutlineCircleDrawable(int size, int color, int stro
45754575
@Override
45764576
public void draw(@NonNull Canvas canvas) {
45774577
final Rect b = getBounds();
4578-
canvas.drawCircle(b.centerX(), b.centerY(), size / 2.0f, paint);
4578+
AndroidUtilities.drawAvatarRoundRect(canvas, b.centerX(), b.centerY(), size / 2.0f, paint);
45794579
}
45804580
@Override
45814581
public void setAlpha(int alpha) {

‎TMessagesProj/src/main/java/org/telegram/ui/Cells/SessionCell.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,14 @@ public class SessionCell extends FrameLayout {
7373
private int currentAccount = UserConfig.selectedAccount;
7474

7575
public SessionCell(Context context, int type) {
76+
this(context, type, UserConfig.selectedAccount);
77+
}
78+
79+
public SessionCell(Context context, int type, int account) {
7680
super(context);
7781

82+
currentAccount = account;
83+
7884
linearLayout = new LinearLayout(context);
7985
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
8086
linearLayout.setWeightSum(1);

‎TMessagesProj/src/main/java/org/telegram/ui/Components/ItemOptions.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ public ItemOptions addChat(TLObject obj, boolean checked, Runnable onClickListen
605605
BackupImageView imageView = new BackupImageView(context);
606606
AvatarDrawable avatarDrawable = new AvatarDrawable();
607607
avatarDrawable.setInfo(obj);
608-
imageView.setRoundRadius(dp(34));
608+
imageView.setRoundRadius(dp(17));
609609
imageView.setForUserOrChat(obj, avatarDrawable);
610610
imageView.setScaleX(checked ? 0.84f : 1.0f);
611611
imageView.setScaleY(checked ? 0.84f : 1.0f);
@@ -657,7 +657,7 @@ public ItemOptions addAccount(int account, boolean checked, Runnable onClickList
657657
imageView.getImageReceiver().setCurrentAccount(account);
658658
AvatarDrawable avatarDrawable = new AvatarDrawable();
659659
avatarDrawable.setInfo(user);
660-
imageView.setRoundRadius(dp(34));
660+
imageView.setRoundRadius(dp(17));
661661
imageView.setForUserOrChat(user, avatarDrawable);
662662
imageView.setScaleX(checked ? 0.84f : 1.0f);
663663
imageView.setScaleY(checked ? 0.84f : 1.0f);

‎TMessagesProj/src/main/java/org/telegram/ui/SessionBottomSheet.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public class SessionBottomSheet extends BottomSheet {
4646
public SessionBottomSheet(BaseFragment fragment, TLRPC.TL_authorization session, boolean isCurrentSession, Callback callback) {
4747
super(fragment.getParentActivity(), false);
4848
setOpenNoDelay(true);
49+
currentAccount = fragment.getCurrentAccount();
4950
Context context = fragment.getParentActivity();
5051
this.session = session;
5152
this.parentFragment = fragment;

‎TMessagesProj/src/main/java/org/telegram/ui/SessionsActivity.java‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import org.telegram.messenger.SvgHelper;
5656
import org.telegram.messenger.UserConfig;
5757
import org.telegram.messenger.UserObject;
58+
import org.telegram.messenger.forkgram.AccountSelector;
5859
import org.telegram.tgnet.ConnectionsManager;
5960
import org.telegram.tgnet.TLObject;
6061
import org.telegram.tgnet.TLRPC;
@@ -96,6 +97,8 @@ public class SessionsActivity extends BaseFragment implements NotificationCenter
9697
public static final int TYPE_DEVICES = 0;
9798
public static final int TYPE_WEB_SESSIONS = 1;
9899

100+
private static final int accountSelectorId = 1;
101+
99102
private ListAdapter listAdapter;
100103
private RecyclerListView listView;
101104
private EmptyTextProgressView emptyView;
@@ -214,6 +217,9 @@ public void onItemClick(int id) {
214217
if (parentLayout != null && parentLayout.isRightLayout()) {
215218
actionBar.setBackButtonImage(R.drawable.ic_ab_close);
216219
}
220+
if (currentType == TYPE_DEVICES) {
221+
AccountSelector.addToMenu(this, actionBar.createMenu(), accountSelectorId, this::switchAccount);
222+
}
217223

218224
listAdapter = new ListAdapter(context);
219225

@@ -871,7 +877,7 @@ public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType
871877
break;
872878
case VIEW_TYPE_SESSION:
873879
default:
874-
view = new SessionCell(mContext, currentType);
880+
view = new SessionCell(mContext, currentType, currentAccount);
875881
break;
876882
}
877883
return new RecyclerListView.Holder(view);
@@ -1216,6 +1222,15 @@ private void setSticker() {
12161222
}
12171223
}
12181224

1225+
private void switchAccount(int account) {
1226+
if (account == currentAccount || parentLayout == null) {
1227+
return;
1228+
}
1229+
final SessionsActivity fragment = new SessionsActivity(currentType);
1230+
fragment.setCurrentAccount(account);
1231+
presentFragment(fragment, true, true);
1232+
}
1233+
12191234
private void openCameraScanActivity() {
12201235
CameraScanActivity.showAsSheet(SessionsActivity.this, false, CameraScanActivity.TYPE_QR_LOGIN, new CameraScanActivity.CameraScanActivityDelegate() {
12211236

0 commit comments

Comments
 (0)