Skip to content

Commit

Permalink
refine sort
Browse files Browse the repository at this point in the history
  • Loading branch information
esdeathlove committed Jun 4, 2017
1 parent 448b334 commit a8299c2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 42 deletions.
4 changes: 4 additions & 0 deletions src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@
<action android:name="com.github.shadowsocks.ProfileManagerActivity"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
<intent-filter>
<action android:name="in.zhaoj.shadowsocksr.intent.action.SORT"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
<intent-filter>
<action android:name="in.zhaoj.shadowsocksr.intent.action.SCAN"/>
<category android:name="android.intent.category.DEFAULT"/>
Expand Down
68 changes: 27 additions & 41 deletions src/main/scala/com/github/shadowsocks/ProfileManagerActivity.scala
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,11 @@ final class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClic

private class ProfilesAdapter extends RecyclerView.Adapter[ProfileViewHolder] {
var profiles = new ArrayBuffer[Profile]
profiles ++= app.profileManager.getAllProfiles.getOrElse(List.empty[Profile])
if (is_sort) {
profiles ++= app.profileManager.getAllProfilesByElapsed.getOrElse(List.empty[Profile])
} else {
profiles ++= app.profileManager.getAllProfiles.getOrElse(List.empty[Profile])
}

def getItemCount = profiles.length

Expand Down Expand Up @@ -433,6 +437,7 @@ final class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClic
private var ssTestProcess: GuardedProcess = _

private val REQUEST_QRCODE = 1
private var is_sort: Boolean = false


def isPortAvailable (port: Int):Boolean = {
Expand All @@ -457,6 +462,10 @@ final class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClic
qrcodeScan()
}

if (action != null && action.equals("in.zhaoj.shadowsocksr.intent.action.SORT")) {
is_sort = true
}

setContentView(R.layout.layout_profiles)

val toolbar = findViewById(R.id.toolbar).asInstanceOf[Toolbar]
Expand All @@ -483,18 +492,20 @@ final class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClic
case (profile, i) if profile.id == app.profileId => i
}.getOrElse(-1))
undoManager = new UndoSnackbarManager[Profile](profilesList, profilesAdapter.undo, profilesAdapter.commit)
new ItemTouchHelper(new SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN,
ItemTouchHelper.START | ItemTouchHelper.END) {
def onSwiped(viewHolder: ViewHolder, direction: Int) = {
val index = viewHolder.getAdapterPosition
profilesAdapter.remove(index)
undoManager.remove(index, viewHolder.asInstanceOf[ProfileViewHolder].item)
}
def onMove(recyclerView: RecyclerView, viewHolder: ViewHolder, target: ViewHolder) = {
profilesAdapter.move(viewHolder.getAdapterPosition, target.getAdapterPosition)
true
}
}).attachToRecyclerView(profilesList)
if (is_sort == false) {
new ItemTouchHelper(new SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN,
ItemTouchHelper.START | ItemTouchHelper.END) {
def onSwiped(viewHolder: ViewHolder, direction: Int) = {
val index = viewHolder.getAdapterPosition
profilesAdapter.remove(index)
undoManager.remove(index, viewHolder.asInstanceOf[ProfileViewHolder].item)
}
def onMove(recyclerView: RecyclerView, viewHolder: ViewHolder, target: ViewHolder) = {
profilesAdapter.move(viewHolder.getAdapterPosition, target.getAdapterPosition)
true
}
}).attachToRecyclerView(profilesList)
}

attachService(new IShadowsocksServiceCallback.Stub {
def stateChanged(state: Int, profileName: String, msg: String) = () // ignore
Expand Down Expand Up @@ -1109,34 +1120,9 @@ final class ProfileManagerActivity extends AppCompatActivity with OnMenuItemClic
}
true
case R.id.action_sort =>
app.profileManager.getAllProfilesByElapsed match {
case Some(profiles) => {
var counter = 0
testProgressDialog = ProgressDialog.show(this, getString(R.string.tips_sorting), getString(R.string.tips_sorting), false, false)

new Thread {
override def run() {
Looper.prepare()
profiles.foreach((profile: Profile) => {
if (profile.elapsed != 0) {
profile.userOrder = counter
counter += 1
}
else
{
profile.userOrder = profiles.length
}
app.profileManager.updateProfile(profile)
})
testProgressDialog.dismiss
finish()
startActivity(new Intent(getIntent()))
Looper.loop()
}
}.start()
}
case _ => Toast.makeText(this, R.string.action_export_err, Toast.LENGTH_SHORT).show
}
finish()
val intent = new Intent("in.zhaoj.shadowsocksr.intent.action.SORT")
startActivity(intent)
true
case _ => false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,8 @@ class ProfileManager(dbHelper: DBHelper) {
def getAllProfilesByElapsed: Option[List[Profile]] = {
try {
import scala.collection.JavaConversions._
Option(dbHelper.profileDao.query(dbHelper.profileDao.queryBuilder.orderBy("elapsed", true).prepare).toList)
Option(dbHelper.profileDao.query(dbHelper.profileDao.queryBuilder.orderBy("elapsed", true).where().not().eq("elapsed", 0).prepare).toList
++ dbHelper.profileDao.query(dbHelper.profileDao.queryBuilder.orderBy("elapsed", true).where().eq("elapsed", 0).prepare).toList)
} catch {
case ex: Exception =>
Log.e(TAG, "getAllProfiles", ex)
Expand Down

0 comments on commit a8299c2

Please sign in to comment.