Commit f1a9fd5b authored by Mygod's avatar Mygod

Refine ProfilesFragment

parent bc7c29c3
...@@ -269,8 +269,6 @@ class MainActivity extends Activity with ServiceBoundContext with Drawer.OnDrawe ...@@ -269,8 +269,6 @@ class MainActivity extends Activity with ServiceBoundContext with Drawer.OnDrawe
val intent = getIntent val intent = getIntent
if (intent != null) handleShareIntent(intent) if (intent != null) handleShareIntent(intent)
if (app.profileManager.getFirstProfile.isEmpty) app.profileId(app.profileManager.createProfile().id)
} }
override def onNewIntent(intent: Intent) { override def onNewIntent(intent: Intent) {
......
...@@ -20,11 +20,10 @@ ...@@ -20,11 +20,10 @@
package com.github.shadowsocks package com.github.shadowsocks
import android.content.{DialogInterface, Intent, SharedPreferences}
import android.content.SharedPreferences.OnSharedPreferenceChangeListener import android.content.SharedPreferences.OnSharedPreferenceChangeListener
import android.content.{DialogInterface, Intent, SharedPreferences}
import android.os.{Build, Bundle, UserManager} import android.os.{Build, Bundle, UserManager}
import android.support.v14.preference.SwitchPreference import android.support.v14.preference.SwitchPreference
import android.support.v4.content.LocalBroadcastManager
import android.support.v7.app.AlertDialog import android.support.v7.app.AlertDialog
import android.support.v7.preference.Preference import android.support.v7.preference.Preference
import android.support.v7.widget.Toolbar.OnMenuItemClickListener import android.support.v7.widget.Toolbar.OnMenuItemClickListener
...@@ -87,8 +86,6 @@ class ProfileConfigFragment extends PreferenceFragment with OnMenuItemClickListe ...@@ -87,8 +86,6 @@ class ProfileConfigFragment extends PreferenceFragment with OnMenuItemClickListe
.setTitle("Confirm?") // TODO .setTitle("Confirm?") // TODO
.setPositiveButton("Yes", ((_, _) => { .setPositiveButton("Yes", ((_, _) => {
app.profileManager.delProfile(profile.id) app.profileManager.delProfile(profile.id)
LocalBroadcastManager.getInstance(getActivity)
.sendBroadcast(new Intent(Action.PROFILE_REMOVED).putExtra(Action.EXTRA_PROFILE_ID, profile.id))
getActivity.finish() getActivity.finish()
}): DialogInterface.OnClickListener) }): DialogInterface.OnClickListener)
.setNegativeButton("No", null) .setNegativeButton("No", null)
...@@ -104,8 +101,7 @@ class ProfileConfigFragment extends PreferenceFragment with OnMenuItemClickListe ...@@ -104,8 +101,7 @@ class ProfileConfigFragment extends PreferenceFragment with OnMenuItemClickListe
def saveAndExit() { def saveAndExit() {
profile.deserialize(app.settings) profile.deserialize(app.settings)
app.profileManager.updateProfile(profile) app.profileManager.updateProfile(profile)
LocalBroadcastManager.getInstance(getActivity) if (ProfilesFragment.instance != null) ProfilesFragment.instance.profilesAdapter.refreshId(profile.id)
.sendBroadcast(new Intent(Action.PROFILE_CHANGED).putExtra(Action.EXTRA_PROFILE_ID, profile.id))
getActivity.finish() getActivity.finish()
} }
} }
...@@ -24,13 +24,12 @@ import java.util.GregorianCalendar ...@@ -24,13 +24,12 @@ import java.util.GregorianCalendar
import android.content._ import android.content._
import android.os.{Bundle, Handler} import android.os.{Bundle, Handler}
import android.support.v4.content.LocalBroadcastManager
import android.support.v7.widget._
import android.support.v7.widget.RecyclerView.ViewHolder import android.support.v7.widget.RecyclerView.ViewHolder
import android.support.v7.widget._
import android.support.v7.widget.helper.ItemTouchHelper import android.support.v7.widget.helper.ItemTouchHelper
import android.support.v7.widget.helper.ItemTouchHelper.SimpleCallback import android.support.v7.widget.helper.ItemTouchHelper.SimpleCallback
import android.view._
import android.view.View.OnLongClickListener import android.view.View.OnLongClickListener
import android.view._
import android.widget.{LinearLayout, PopupMenu, TextView, Toast} import android.widget.{LinearLayout, PopupMenu, TextView, Toast}
import com.github.shadowsocks.ShadowsocksApplication.app import com.github.shadowsocks.ShadowsocksApplication.app
import com.github.shadowsocks.database.Profile import com.github.shadowsocks.database.Profile
...@@ -41,14 +40,20 @@ import com.google.android.gms.ads.{AdRequest, AdSize, NativeExpressAdView} ...@@ -41,14 +40,20 @@ import com.google.android.gms.ads.{AdRequest, AdSize, NativeExpressAdView}
import scala.collection.mutable.ArrayBuffer import scala.collection.mutable.ArrayBuffer
import scala.util.Random import scala.util.Random
object ProfilesFragment {
var instance: ProfilesFragment = _ // used for callback from ProfileManager
}
final class ProfilesFragment extends ToolbarFragment with Toolbar.OnMenuItemClickListener { final class ProfilesFragment extends ToolbarFragment with Toolbar.OnMenuItemClickListener {
import ProfilesFragment._
private val cardButtonLongClickListener: OnLongClickListener = view => { private val cardButtonLongClickListener: OnLongClickListener = view => {
Utils.positionToast(Toast.makeText(getActivity, view.getContentDescription, Toast.LENGTH_SHORT), view, Utils.positionToast(Toast.makeText(getActivity, view.getContentDescription, Toast.LENGTH_SHORT), view,
getActivity.getWindow, 0, Utils.dpToPx(getActivity, 8)).show() getActivity.getWindow, 0, Utils.dpToPx(getActivity, 8)).show()
true true
} }
private final class ProfileViewHolder(val view: View) extends RecyclerView.ViewHolder(view) final class ProfileViewHolder(val view: View) extends RecyclerView.ViewHolder(view)
with View.OnClickListener with PopupMenu.OnMenuItemClickListener { with View.OnClickListener with PopupMenu.OnMenuItemClickListener {
var item: Profile = _ var item: Profile = _
...@@ -153,7 +158,7 @@ final class ProfilesFragment extends ToolbarFragment with Toolbar.OnMenuItemClic ...@@ -153,7 +158,7 @@ final class ProfilesFragment extends ToolbarFragment with Toolbar.OnMenuItemClic
} }
} }
private class ProfilesAdapter extends RecyclerView.Adapter[ProfileViewHolder] { final class ProfilesAdapter extends RecyclerView.Adapter[ProfileViewHolder] {
var profiles = new ArrayBuffer[Profile] var profiles = new ArrayBuffer[Profile]
profiles ++= app.profileManager.getAllProfiles.getOrElse(List.empty[Profile]) profiles ++= app.profileManager.getAllProfiles.getOrElse(List.empty[Profile])
...@@ -210,23 +215,18 @@ final class ProfilesFragment extends ToolbarFragment with Toolbar.OnMenuItemClic ...@@ -210,23 +215,18 @@ final class ProfilesFragment extends ToolbarFragment with Toolbar.OnMenuItemClic
} }
def removeId(id: Int) { def removeId(id: Int) {
val index = profiles.indexWhere(_.id == id) val index = profiles.indexWhere(_.id == id)
if (index >= 0) {
profiles.remove(index) profiles.remove(index)
notifyItemRemoved(index) notifyItemRemoved(index)
if (id == app.profileId) app.profileId(0) if (id == app.profileId) app.profileId(0) // switch to null profile
}
} }
} }
private var selectedItem: ProfileViewHolder = _ private var selectedItem: ProfileViewHolder = _
private val handler = new Handler private val handler = new Handler
private val profilesListener: BroadcastReceiver = (_, intent) => {
val id = intent.getIntExtra(Action.EXTRA_PROFILE_ID, -1)
intent.getAction match {
case Action.PROFILE_CHANGED => profilesAdapter.refreshId(id)
case Action.PROFILE_REMOVED => profilesAdapter.removeId(id)
}
}
private lazy val profilesAdapter = new ProfilesAdapter lazy val profilesAdapter = new ProfilesAdapter
private var undoManager: UndoSnackbarManager[Profile] = _ private var undoManager: UndoSnackbarManager[Profile] = _
private lazy val clipboard = getActivity.getSystemService(Context.CLIPBOARD_SERVICE).asInstanceOf[ClipboardManager] private lazy val clipboard = getActivity.getSystemService(Context.CLIPBOARD_SERVICE).asInstanceOf[ClipboardManager]
...@@ -243,12 +243,13 @@ final class ProfilesFragment extends ToolbarFragment with Toolbar.OnMenuItemClic ...@@ -243,12 +243,13 @@ final class ProfilesFragment extends ToolbarFragment with Toolbar.OnMenuItemClic
toolbar.inflateMenu(R.menu.profile_manager_menu) toolbar.inflateMenu(R.menu.profile_manager_menu)
toolbar.setOnMenuItemClickListener(this) toolbar.setOnMenuItemClickListener(this)
app.profileManager.setProfileAddedListener(profilesAdapter.add) if (app.profileManager.getFirstProfile.isEmpty) app.profileId(app.profileManager.createProfile().id)
val profilesList = view.findViewById(R.id.profilesList).asInstanceOf[RecyclerView] val profilesList = view.findViewById(R.id.profilesList).asInstanceOf[RecyclerView]
val layoutManager = new LinearLayoutManager(getActivity) val layoutManager = new LinearLayoutManager(getActivity)
profilesList.setLayoutManager(layoutManager) profilesList.setLayoutManager(layoutManager)
profilesList.setItemAnimator(new DefaultItemAnimator) profilesList.setItemAnimator(new DefaultItemAnimator)
profilesList.setAdapter(profilesAdapter) profilesList.setAdapter(profilesAdapter)
instance = this
layoutManager.scrollToPosition(profilesAdapter.profiles.zipWithIndex.collectFirst { layoutManager.scrollToPosition(profilesAdapter.profiles.zipWithIndex.collectFirst {
case (profile, i) if profile.id == app.profileId => i case (profile, i) if profile.id == app.profileId => i
}.getOrElse(-1)) }.getOrElse(-1))
...@@ -265,10 +266,6 @@ final class ProfilesFragment extends ToolbarFragment with Toolbar.OnMenuItemClic ...@@ -265,10 +266,6 @@ final class ProfilesFragment extends ToolbarFragment with Toolbar.OnMenuItemClic
true true
} }
}).attachToRecyclerView(profilesList) }).attachToRecyclerView(profilesList)
val filter = new IntentFilter(Action.PROFILE_CHANGED)
filter.addAction(Action.PROFILE_REMOVED)
LocalBroadcastManager.getInstance(getActivity).registerReceiver(profilesListener, filter)
} }
override def onTrafficUpdated(txRate: Long, rxRate: Long, txTotal: Long, rxTotal: Long): Unit = override def onTrafficUpdated(txRate: Long, rxRate: Long, txTotal: Long, rxTotal: Long): Unit =
...@@ -280,7 +277,7 @@ final class ProfilesFragment extends ToolbarFragment with Toolbar.OnMenuItemClic ...@@ -280,7 +277,7 @@ final class ProfilesFragment extends ToolbarFragment with Toolbar.OnMenuItemClic
} }
override def onDestroy() { override def onDestroy() {
LocalBroadcastManager.getInstance(getActivity).unregisterReceiver(profilesListener) instance = null
super.onDestroy() super.onDestroy()
} }
...@@ -300,9 +297,7 @@ final class ProfilesFragment extends ToolbarFragment with Toolbar.OnMenuItemClic ...@@ -300,9 +297,7 @@ final class ProfilesFragment extends ToolbarFragment with Toolbar.OnMenuItemClic
Toast.makeText(getActivity, R.string.action_import_err, Toast.LENGTH_SHORT).show() Toast.makeText(getActivity, R.string.action_import_err, Toast.LENGTH_SHORT).show()
true true
case R.id.action_manual_settings => case R.id.action_manual_settings =>
val profile = app.profileManager.createProfile() startConfig(app.profileManager.createProfile().id)
app.profileManager.updateProfile(profile)
startConfig(profile.id)
true true
case R.id.action_export => case R.id.action_export =>
app.profileManager.getAllProfiles match { app.profileManager.getAllProfiles match {
......
...@@ -88,8 +88,9 @@ class ShadowsocksApplication extends Application { ...@@ -88,8 +88,9 @@ class ShadowsocksApplication extends Application {
def currentProfile: Option[Profile] = profileManager.getProfile(profileId) def currentProfile: Option[Profile] = profileManager.getProfile(profileId)
def switchProfile(id: Int): Profile = { def switchProfile(id: Int): Profile = {
profileId(id) val result = profileManager.getProfile(id) getOrElse profileManager.createProfile()
profileManager.getProfile(id) getOrElse profileManager.createProfile() profileId(result.id)
result
} }
private def checkChineseLocale(locale: Locale): Locale = if (locale.getLanguage == "zh") locale.getCountry match { private def checkChineseLocale(locale: Locale): Locale = if (locale.getLanguage == "zh") locale.getCountry match {
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
package com.github.shadowsocks.database package com.github.shadowsocks.database
import android.util.Log import android.util.Log
import com.github.shadowsocks.ProfilesFragment
import com.github.shadowsocks.ShadowsocksApplication.app import com.github.shadowsocks.ShadowsocksApplication.app
object ProfileManager { object ProfileManager {
...@@ -30,9 +31,6 @@ object ProfileManager { ...@@ -30,9 +31,6 @@ object ProfileManager {
class ProfileManager(dbHelper: DBHelper) { class ProfileManager(dbHelper: DBHelper) {
import ProfileManager._ import ProfileManager._
var profileAddedListener: Profile => Any = _
def setProfileAddedListener(listener: Profile => Any): Unit = this.profileAddedListener = listener
def createProfile(p: Profile = null): Profile = { def createProfile(p: Profile = null): Profile = {
val profile = if (p == null) new Profile else p val profile = if (p == null) new Profile else p
profile.id = 0 profile.id = 0
...@@ -52,7 +50,7 @@ class ProfileManager(dbHelper: DBHelper) { ...@@ -52,7 +50,7 @@ class ProfileManager(dbHelper: DBHelper) {
.prepareStatementString).getFirstResult .prepareStatementString).getFirstResult
if (last != null && last.length == 1 && last(0) != null) profile.userOrder = last(0).toInt + 1 if (last != null && last.length == 1 && last(0) != null) profile.userOrder = last(0).toInt + 1
dbHelper.profileDao.createOrUpdate(profile) dbHelper.profileDao.createOrUpdate(profile)
if (profileAddedListener != null) profileAddedListener(profile) if (ProfilesFragment.instance != null) ProfilesFragment.instance.profilesAdapter.add(profile)
} catch { } catch {
case ex: Exception => case ex: Exception =>
Log.e(TAG, "addProfile", ex) Log.e(TAG, "addProfile", ex)
...@@ -90,6 +88,7 @@ class ProfileManager(dbHelper: DBHelper) { ...@@ -90,6 +88,7 @@ class ProfileManager(dbHelper: DBHelper) {
def delProfile(id: Int): Boolean = { def delProfile(id: Int): Boolean = {
try { try {
dbHelper.profileDao.deleteById(id) dbHelper.profileDao.deleteById(id)
if (ProfilesFragment.instance != null) ProfilesFragment.instance.profilesAdapter.removeId(id)
true true
} catch { } catch {
case ex: Exception => case ex: Exception =>
......
...@@ -176,8 +176,6 @@ object State { ...@@ -176,8 +176,6 @@ object State {
object Action { object Action {
final val SERVICE = "com.github.shadowsocks.SERVICE" final val SERVICE = "com.github.shadowsocks.SERVICE"
final val CLOSE = "com.github.shadowsocks.CLOSE" final val CLOSE = "com.github.shadowsocks.CLOSE"
final val PROFILE_CHANGED = "com.github.shadowsocks.PROFILE_CHANGED"
final val PROFILE_REMOVED = "com.github.shadowsocks.PROFILE_REMOVED"
final val EXTRA_PROFILE_ID = "com.github.shadowsocks.EXTRA_PROFILE_ID" final val EXTRA_PROFILE_ID = "com.github.shadowsocks.EXTRA_PROFILE_ID"
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment