|
| 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 | +} |
0 commit comments