sort accounts

This commit is contained in:
Riko Sakurauchi 2019-06-19 19:48:50 +08:00
parent 9aead96e1f
commit 832da08c77
No known key found for this signature in database
GPG Key ID: 25AC0345B92902AF
2 changed files with 87 additions and 2 deletions

View File

@ -8,11 +8,14 @@
package org.telegram.ui.Adapters; package org.telegram.ui.Adapters;
import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import org.telegram.messenger.AndroidUtilities; import org.telegram.messenger.AndroidUtilities;
import org.telegram.messenger.ApplicationLoader;
import org.telegram.messenger.LocaleController; import org.telegram.messenger.LocaleController;
import org.telegram.messenger.MessagesController; import org.telegram.messenger.MessagesController;
import org.telegram.messenger.R; import org.telegram.messenger.R;
@ -28,6 +31,7 @@ import org.telegram.ui.Components.RecyclerListView;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Locale;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
@ -192,14 +196,19 @@ public class DrawerLayoutAdapter extends RecyclerListView.SelectionAdapter {
private void resetItems() { private void resetItems() {
accountNumbers.clear(); 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++) { for (int a = 0; a < UserConfig.MAX_ACCOUNT_COUNT; a++) {
if (UserConfig.getInstance(a).isClientActivated()) { if (UserConfig.getInstance(a).isClientActivated()) {
accountNumbers.add(a); accountNumbers.add(a);
} else {
editor.remove(String.format(Locale.US, "account_pos_%d", a));
} }
} }
editor.apply();
Collections.sort(accountNumbers, (o1, o2) -> { Collections.sort(accountNumbers, (o1, o2) -> {
long l1 = UserConfig.getInstance(o1).loginTime; long l1 = preferences.getLong(String.format(Locale.US, "account_pos_%d", o1), UserConfig.getInstance(o1).loginTime);
long l2 = UserConfig.getInstance(o2).loginTime; long l2 = preferences.getLong(String.format(Locale.US, "account_pos_%d", o2), UserConfig.getInstance(o2).loginTime);
if (l1 > l2) { if (l1 > l2) {
return 1; return 1;
} else if (l1 < l2) { } else if (l1 < l2) {
@ -265,4 +274,22 @@ public class DrawerLayoutAdapter extends RecyclerListView.SelectionAdapter {
actionCell.setTextAndIcon(text, icon); 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);
}
} }

View File

@ -17,6 +17,7 @@ import android.content.pm.PackageInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.database.Cursor; import android.database.Cursor;
import android.graphics.Canvas;
import android.graphics.Point; import android.graphics.Point;
import android.graphics.Shader; import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.BitmapDrawable;
@ -97,7 +98,9 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import androidx.recyclerview.widget.DefaultItemAnimator; import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
public class LaunchActivity extends Activity implements ActionBarLayout.ActionBarLayoutDelegate, NotificationCenter.NotificationCenterDelegate, DialogsActivity.DialogsActivityDelegate { 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 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 @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
ApplicationLoader.postInitApplication(); ApplicationLoader.postInitApplication();
@ -370,6 +424,10 @@ public class LaunchActivity extends Activity implements ActionBarLayout.ActionBa
sideMenu.setBackgroundColor(Theme.getColor(Theme.key_chats_menuBackground)); sideMenu.setBackgroundColor(Theme.getColor(Theme.key_chats_menuBackground));
sideMenu.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)); sideMenu.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
sideMenu.setAdapter(drawerLayoutAdapter = new DrawerLayoutAdapter(this)); sideMenu.setAdapter(drawerLayoutAdapter = new DrawerLayoutAdapter(this));
ItemTouchHelper drawerItemTouchHelper = new ItemTouchHelper(new DrawerItemTouchHelperCallback());
drawerItemTouchHelper.attachToRecyclerView(sideMenu);
drawerLayoutContainer.setDrawerLayout(sideMenu); drawerLayoutContainer.setDrawerLayout(sideMenu);
FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) sideMenu.getLayoutParams(); FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) sideMenu.getLayoutParams();
Point screenSize = AndroidUtilities.getRealScreenSize(); Point screenSize = AndroidUtilities.getRealScreenSize();