From f1a9fd5ba51d6861567aee4689c7082eb7173d3a Mon Sep 17 00:00:00 2001 From: Mygod Date: Wed, 28 Dec 2016 02:14:53 +0800 Subject: [PATCH] Refine ProfilesFragment --- .../com/github/shadowsocks/MainActivity.scala | 2 - .../shadowsocks/ProfileConfigFragment.scala | 8 +--- .../github/shadowsocks/ProfilesFragment.scala | 45 +++++++++---------- .../shadowsocks/ShadowsocksApplication.scala | 5 ++- .../shadowsocks/database/ProfileManager.scala | 7 ++- .../github/shadowsocks/utils/Constants.scala | 2 - 6 files changed, 28 insertions(+), 41 deletions(-) diff --git a/src/main/scala/com/github/shadowsocks/MainActivity.scala b/src/main/scala/com/github/shadowsocks/MainActivity.scala index 702d3f33..cfae52b9 100644 --- a/src/main/scala/com/github/shadowsocks/MainActivity.scala +++ b/src/main/scala/com/github/shadowsocks/MainActivity.scala @@ -269,8 +269,6 @@ class MainActivity extends Activity with ServiceBoundContext with Drawer.OnDrawe val intent = getIntent if (intent != null) handleShareIntent(intent) - - if (app.profileManager.getFirstProfile.isEmpty) app.profileId(app.profileManager.createProfile().id) } override def onNewIntent(intent: Intent) { diff --git a/src/main/scala/com/github/shadowsocks/ProfileConfigFragment.scala b/src/main/scala/com/github/shadowsocks/ProfileConfigFragment.scala index dabdc244..d8952daa 100644 --- a/src/main/scala/com/github/shadowsocks/ProfileConfigFragment.scala +++ b/src/main/scala/com/github/shadowsocks/ProfileConfigFragment.scala @@ -20,11 +20,10 @@ package com.github.shadowsocks -import android.content.{DialogInterface, Intent, SharedPreferences} import android.content.SharedPreferences.OnSharedPreferenceChangeListener +import android.content.{DialogInterface, Intent, SharedPreferences} import android.os.{Build, Bundle, UserManager} import android.support.v14.preference.SwitchPreference -import android.support.v4.content.LocalBroadcastManager import android.support.v7.app.AlertDialog import android.support.v7.preference.Preference import android.support.v7.widget.Toolbar.OnMenuItemClickListener @@ -87,8 +86,6 @@ class ProfileConfigFragment extends PreferenceFragment with OnMenuItemClickListe .setTitle("Confirm?") // TODO .setPositiveButton("Yes", ((_, _) => { app.profileManager.delProfile(profile.id) - LocalBroadcastManager.getInstance(getActivity) - .sendBroadcast(new Intent(Action.PROFILE_REMOVED).putExtra(Action.EXTRA_PROFILE_ID, profile.id)) getActivity.finish() }): DialogInterface.OnClickListener) .setNegativeButton("No", null) @@ -104,8 +101,7 @@ class ProfileConfigFragment extends PreferenceFragment with OnMenuItemClickListe def saveAndExit() { profile.deserialize(app.settings) app.profileManager.updateProfile(profile) - LocalBroadcastManager.getInstance(getActivity) - .sendBroadcast(new Intent(Action.PROFILE_CHANGED).putExtra(Action.EXTRA_PROFILE_ID, profile.id)) + if (ProfilesFragment.instance != null) ProfilesFragment.instance.profilesAdapter.refreshId(profile.id) getActivity.finish() } } diff --git a/src/main/scala/com/github/shadowsocks/ProfilesFragment.scala b/src/main/scala/com/github/shadowsocks/ProfilesFragment.scala index 6abb9a70..b7348136 100644 --- a/src/main/scala/com/github/shadowsocks/ProfilesFragment.scala +++ b/src/main/scala/com/github/shadowsocks/ProfilesFragment.scala @@ -24,13 +24,12 @@ import java.util.GregorianCalendar import android.content._ 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._ import android.support.v7.widget.helper.ItemTouchHelper import android.support.v7.widget.helper.ItemTouchHelper.SimpleCallback -import android.view._ import android.view.View.OnLongClickListener +import android.view._ import android.widget.{LinearLayout, PopupMenu, TextView, Toast} import com.github.shadowsocks.ShadowsocksApplication.app import com.github.shadowsocks.database.Profile @@ -41,14 +40,20 @@ import com.google.android.gms.ads.{AdRequest, AdSize, NativeExpressAdView} import scala.collection.mutable.ArrayBuffer import scala.util.Random +object ProfilesFragment { + var instance: ProfilesFragment = _ // used for callback from ProfileManager +} + final class ProfilesFragment extends ToolbarFragment with Toolbar.OnMenuItemClickListener { + import ProfilesFragment._ + private val cardButtonLongClickListener: OnLongClickListener = view => { Utils.positionToast(Toast.makeText(getActivity, view.getContentDescription, Toast.LENGTH_SHORT), view, getActivity.getWindow, 0, Utils.dpToPx(getActivity, 8)).show() 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 { var item: Profile = _ @@ -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] profiles ++= app.profileManager.getAllProfiles.getOrElse(List.empty[Profile]) @@ -210,23 +215,18 @@ final class ProfilesFragment extends ToolbarFragment with Toolbar.OnMenuItemClic } def removeId(id: Int) { val index = profiles.indexWhere(_.id == id) - profiles.remove(index) - notifyItemRemoved(index) - if (id == app.profileId) app.profileId(0) + if (index >= 0) { + profiles.remove(index) + notifyItemRemoved(index) + if (id == app.profileId) app.profileId(0) // switch to null profile + } } } private var selectedItem: ProfileViewHolder = _ 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 lazy val clipboard = getActivity.getSystemService(Context.CLIPBOARD_SERVICE).asInstanceOf[ClipboardManager] @@ -243,12 +243,13 @@ final class ProfilesFragment extends ToolbarFragment with Toolbar.OnMenuItemClic toolbar.inflateMenu(R.menu.profile_manager_menu) 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 layoutManager = new LinearLayoutManager(getActivity) profilesList.setLayoutManager(layoutManager) profilesList.setItemAnimator(new DefaultItemAnimator) profilesList.setAdapter(profilesAdapter) + instance = this layoutManager.scrollToPosition(profilesAdapter.profiles.zipWithIndex.collectFirst { case (profile, i) if profile.id == app.profileId => i }.getOrElse(-1)) @@ -265,10 +266,6 @@ final class ProfilesFragment extends ToolbarFragment with Toolbar.OnMenuItemClic true } }).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 = @@ -280,7 +277,7 @@ final class ProfilesFragment extends ToolbarFragment with Toolbar.OnMenuItemClic } override def onDestroy() { - LocalBroadcastManager.getInstance(getActivity).unregisterReceiver(profilesListener) + instance = null super.onDestroy() } @@ -300,9 +297,7 @@ final class ProfilesFragment extends ToolbarFragment with Toolbar.OnMenuItemClic Toast.makeText(getActivity, R.string.action_import_err, Toast.LENGTH_SHORT).show() true case R.id.action_manual_settings => - val profile = app.profileManager.createProfile() - app.profileManager.updateProfile(profile) - startConfig(profile.id) + startConfig(app.profileManager.createProfile().id) true case R.id.action_export => app.profileManager.getAllProfiles match { diff --git a/src/main/scala/com/github/shadowsocks/ShadowsocksApplication.scala b/src/main/scala/com/github/shadowsocks/ShadowsocksApplication.scala index 326422b1..2bd45095 100644 --- a/src/main/scala/com/github/shadowsocks/ShadowsocksApplication.scala +++ b/src/main/scala/com/github/shadowsocks/ShadowsocksApplication.scala @@ -88,8 +88,9 @@ class ShadowsocksApplication extends Application { def currentProfile: Option[Profile] = profileManager.getProfile(profileId) def switchProfile(id: Int): Profile = { - profileId(id) - profileManager.getProfile(id) getOrElse profileManager.createProfile() + val result = profileManager.getProfile(id) getOrElse profileManager.createProfile() + profileId(result.id) + result } private def checkChineseLocale(locale: Locale): Locale = if (locale.getLanguage == "zh") locale.getCountry match { diff --git a/src/main/scala/com/github/shadowsocks/database/ProfileManager.scala b/src/main/scala/com/github/shadowsocks/database/ProfileManager.scala index 9db9441b..263343f7 100644 --- a/src/main/scala/com/github/shadowsocks/database/ProfileManager.scala +++ b/src/main/scala/com/github/shadowsocks/database/ProfileManager.scala @@ -21,6 +21,7 @@ package com.github.shadowsocks.database import android.util.Log +import com.github.shadowsocks.ProfilesFragment import com.github.shadowsocks.ShadowsocksApplication.app object ProfileManager { @@ -30,9 +31,6 @@ object ProfileManager { class ProfileManager(dbHelper: DBHelper) { import ProfileManager._ - var profileAddedListener: Profile => Any = _ - def setProfileAddedListener(listener: Profile => Any): Unit = this.profileAddedListener = listener - def createProfile(p: Profile = null): Profile = { val profile = if (p == null) new Profile else p profile.id = 0 @@ -52,7 +50,7 @@ class ProfileManager(dbHelper: DBHelper) { .prepareStatementString).getFirstResult if (last != null && last.length == 1 && last(0) != null) profile.userOrder = last(0).toInt + 1 dbHelper.profileDao.createOrUpdate(profile) - if (profileAddedListener != null) profileAddedListener(profile) + if (ProfilesFragment.instance != null) ProfilesFragment.instance.profilesAdapter.add(profile) } catch { case ex: Exception => Log.e(TAG, "addProfile", ex) @@ -90,6 +88,7 @@ class ProfileManager(dbHelper: DBHelper) { def delProfile(id: Int): Boolean = { try { dbHelper.profileDao.deleteById(id) + if (ProfilesFragment.instance != null) ProfilesFragment.instance.profilesAdapter.removeId(id) true } catch { case ex: Exception => diff --git a/src/main/scala/com/github/shadowsocks/utils/Constants.scala b/src/main/scala/com/github/shadowsocks/utils/Constants.scala index f7bad735..703d2629 100644 --- a/src/main/scala/com/github/shadowsocks/utils/Constants.scala +++ b/src/main/scala/com/github/shadowsocks/utils/Constants.scala @@ -176,8 +176,6 @@ object State { object Action { final val SERVICE = "com.github.shadowsocks.SERVICE" 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" }