From 832da08c773b2d0946fd91693589e1cd0da86ff9 Mon Sep 17 00:00:00 2001 From: Riko Sakurauchi Date: Wed, 19 Jun 2019 19:48:50 +0800 Subject: [PATCH] sort accounts --- .../ui/Adapters/DrawerLayoutAdapter.java | 31 +++++++++- .../java/org/telegram/ui/LaunchActivity.java | 58 +++++++++++++++++++ 2 files changed, 87 insertions(+), 2 deletions(-) diff --git a/TMessagesProj/src/main/java/org/telegram/ui/Adapters/DrawerLayoutAdapter.java b/TMessagesProj/src/main/java/org/telegram/ui/Adapters/DrawerLayoutAdapter.java index cca6ff6fb..98c2f9643 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/Adapters/DrawerLayoutAdapter.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/Adapters/DrawerLayoutAdapter.java @@ -8,11 +8,14 @@ package org.telegram.ui.Adapters; +import android.app.Activity; import android.content.Context; +import android.content.SharedPreferences; import android.view.View; import android.view.ViewGroup; import org.telegram.messenger.AndroidUtilities; +import org.telegram.messenger.ApplicationLoader; import org.telegram.messenger.LocaleController; import org.telegram.messenger.MessagesController; import org.telegram.messenger.R; @@ -28,6 +31,7 @@ import org.telegram.ui.Components.RecyclerListView; import java.util.ArrayList; import java.util.Collections; +import java.util.Locale; import androidx.recyclerview.widget.RecyclerView; @@ -192,14 +196,19 @@ public class DrawerLayoutAdapter extends RecyclerListView.SelectionAdapter { private void resetItems() { accountNumbers.clear(); + SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("nekoconfig", Activity.MODE_PRIVATE); + SharedPreferences.Editor editor = preferences.edit(); for (int a = 0; a < UserConfig.MAX_ACCOUNT_COUNT; a++) { if (UserConfig.getInstance(a).isClientActivated()) { accountNumbers.add(a); + } else { + editor.remove(String.format(Locale.US, "account_pos_%d", a)); } } + editor.apply(); Collections.sort(accountNumbers, (o1, o2) -> { - long l1 = UserConfig.getInstance(o1).loginTime; - long l2 = UserConfig.getInstance(o2).loginTime; + long l1 = preferences.getLong(String.format(Locale.US, "account_pos_%d", o1), UserConfig.getInstance(o1).loginTime); + long l2 = preferences.getLong(String.format(Locale.US, "account_pos_%d", o2), UserConfig.getInstance(o2).loginTime); if (l1 > l2) { return 1; } else if (l1 < l2) { @@ -265,4 +274,22 @@ public class DrawerLayoutAdapter extends RecyclerListView.SelectionAdapter { actionCell.setTextAndIcon(text, icon); } } + + public int getAccountsCount() { + return accountNumbers.size(); + } + + public void swapAccountPosition(int currentAdapterPosition, int targetAdapterPosition) { + int currentIndex = currentAdapterPosition - 2; + int targetIndex = targetAdapterPosition - 2; + int currentElement = accountNumbers.get(currentIndex); + int targetElement = accountNumbers.get(targetIndex); + accountNumbers.set(targetIndex, currentElement); + accountNumbers.set(currentIndex, targetElement); + ApplicationLoader.applicationContext.getSharedPreferences("nekoconfig", Activity.MODE_PRIVATE).edit(). + putLong(String.format(Locale.US, "account_pos_%d", currentElement), targetIndex). + putLong(String.format(Locale.US, "account_pos_%d", targetElement), currentIndex) + .apply(); + notifyItemMoved(currentAdapterPosition, targetAdapterPosition); + } } diff --git a/TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java index e36a7696d..27cc8904f 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java @@ -17,6 +17,7 @@ import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.res.Configuration; import android.database.Cursor; +import android.graphics.Canvas; import android.graphics.Point; import android.graphics.Shader; import android.graphics.drawable.BitmapDrawable; @@ -97,7 +98,9 @@ import java.util.List; import java.util.Map; import androidx.recyclerview.widget.DefaultItemAnimator; +import androidx.recyclerview.widget.ItemTouchHelper; import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; public class LaunchActivity extends Activity implements ActionBarLayout.ActionBarLayoutDelegate, NotificationCenter.NotificationCenterDelegate, DialogsActivity.DialogsActivityDelegate { @@ -149,6 +152,57 @@ public class LaunchActivity extends Activity implements ActionBarLayout.ActionBa private Runnable lockRunnable; + private class DrawerItemTouchHelperCallback extends ItemTouchHelper.Callback { + + @Override + public boolean isLongPressDragEnabled() { + return true; + } + + @Override + public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { + if (viewHolder != null && viewHolder.getItemViewType() != 4) { + return makeMovementFlags(0, 0); + } + return makeMovementFlags(ItemTouchHelper.UP | ItemTouchHelper.DOWN, 0); + } + + @Override + public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { + super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); + } + + @Override + public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) { + if (viewHolder != null && viewHolder.getItemViewType() == 4 && actionState != ItemTouchHelper.ACTION_STATE_IDLE) { + sideMenu.cancelClickRunnables(false); + viewHolder.itemView.setPressed(true); + } + super.onSelectedChanged(viewHolder, actionState); + } + + @Override + public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { + + } + + @Override + public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { + super.clearView(recyclerView, viewHolder); + viewHolder.itemView.setPressed(false); + } + + @Override + public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { + if (viewHolder.getItemViewType() == 4 && target.getItemViewType() == 4 && + viewHolder.getAdapterPosition() != target.getAdapterPosition()) { + drawerLayoutAdapter.swapAccountPosition(viewHolder.getAdapterPosition(), target.getAdapterPosition()); + return true; + } + return false; + } + } + @Override protected void onCreate(Bundle savedInstanceState) { ApplicationLoader.postInitApplication(); @@ -370,6 +424,10 @@ public class LaunchActivity extends Activity implements ActionBarLayout.ActionBa sideMenu.setBackgroundColor(Theme.getColor(Theme.key_chats_menuBackground)); sideMenu.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)); sideMenu.setAdapter(drawerLayoutAdapter = new DrawerLayoutAdapter(this)); + + ItemTouchHelper drawerItemTouchHelper = new ItemTouchHelper(new DrawerItemTouchHelperCallback()); + drawerItemTouchHelper.attachToRecyclerView(sideMenu); + drawerLayoutContainer.setDrawerLayout(sideMenu); FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) sideMenu.getLayoutParams(); Point screenSize = AndroidUtilities.getRealScreenSize();