diff --git a/twidere/src/main/java/org/mariotaku/twidere/util/Utils.java b/twidere/src/main/java/org/mariotaku/twidere/util/Utils.java index 06b613418..395900f95 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/util/Utils.java +++ b/twidere/src/main/java/org/mariotaku/twidere/util/Utils.java @@ -568,11 +568,6 @@ public final class Utils implements Constants { return null; } - public static int getDefaultTextSize(final Context context) { - if (context == null) return 15; - return context.getResources().getInteger(R.integer.default_text_size); - } - public static String getErrorMessage(final Context context, final CharSequence message) { if (context == null) return ParseUtils.parseString(message); if (TextUtils.isEmpty(message)) return context.getString(R.string.error_unknown_error); diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/activity/ComposeActivity.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/activity/ComposeActivity.kt index 6a911cd6e..cfb4c3aaf 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/activity/ComposeActivity.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/activity/ComposeActivity.kt @@ -244,7 +244,7 @@ class ComposeActivity : BaseActivity(), OnMenuItemClickListener, OnClickListener } setMenu() updateTextCount() - val textSize = preferences.getInt(KEY_TEXT_SIZE, Utils.getDefaultTextSize(this)) + val textSize = preferences[textSizeKey] editText.textSize = textSize * 1.25f } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/DummyItemAdapter.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/DummyItemAdapter.kt index 963daf075..cdfae3a5f 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/DummyItemAdapter.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/DummyItemAdapter.kt @@ -3,12 +3,13 @@ package org.mariotaku.twidere.adapter import android.content.Context import android.support.v4.text.BidiFormatter import android.support.v7.widget.RecyclerView +import org.mariotaku.kpreferences.get import org.mariotaku.twidere.R import org.mariotaku.twidere.adapter.iface.IGapSupportedAdapter import org.mariotaku.twidere.adapter.iface.IStatusesAdapter import org.mariotaku.twidere.adapter.iface.IUserListsAdapter import org.mariotaku.twidere.adapter.iface.IUsersAdapter -import org.mariotaku.twidere.constant.SharedPreferenceConstants +import org.mariotaku.twidere.constant.* import org.mariotaku.twidere.model.* import org.mariotaku.twidere.model.util.getActivityStatus import org.mariotaku.twidere.util.* @@ -172,16 +173,16 @@ class DummyItemAdapter @JvmOverloads constructor( } fun updateOptions() { - profileImageStyle = Utils.getProfileImageStyle(preferences.getString(SharedPreferenceConstants.KEY_PROFILE_IMAGE_STYLE, null)) - mediaPreviewStyle = Utils.getMediaPreviewStyle(preferences.getString(SharedPreferenceConstants.KEY_MEDIA_PREVIEW_STYLE, null)) - textSize = preferences.getInt(SharedPreferenceConstants.KEY_TEXT_SIZE, context.resources.getInteger(R.integer.default_text_size)).toFloat() - nameFirst = preferences.getBoolean(SharedPreferenceConstants.KEY_NAME_FIRST, true) - profileImageEnabled = preferences.getBoolean(SharedPreferenceConstants.KEY_DISPLAY_PROFILE_IMAGE, true) - mediaPreviewEnabled = preferences.getBoolean(SharedPreferenceConstants.KEY_MEDIA_PREVIEW, false) - sensitiveContentEnabled = preferences.getBoolean(SharedPreferenceConstants.KEY_DISPLAY_SENSITIVE_CONTENTS, false) - showCardActions = !preferences.getBoolean(SharedPreferenceConstants.KEY_HIDE_CARD_ACTIONS, false) - linkHighlightingStyle = Utils.getLinkHighlightingStyleInt(preferences.getString(SharedPreferenceConstants.KEY_LINK_HIGHLIGHT_OPTION, null)) - useStarsForLikes = preferences.getBoolean(SharedPreferenceConstants.KEY_I_WANT_MY_STARS_BACK) - isShowAbsoluteTime = preferences.getBoolean(SharedPreferenceConstants.KEY_SHOW_ABSOLUTE_TIME) + profileImageStyle = preferences[profileImageStyleKey] + mediaPreviewStyle = preferences[mediaPreviewStyleKey] + textSize = preferences[textSizeKey].toFloat() + nameFirst = preferences[nameFirstKey] + profileImageEnabled = preferences[displayProfileImageKey] + mediaPreviewEnabled = preferences[mediaPreviewKey] + sensitiveContentEnabled = preferences[displaySensitiveContentsKey] + showCardActions = !preferences[hideCardActionsKey] + linkHighlightingStyle = preferences[linkHighlightOptionKey] + useStarsForLikes = preferences[iWantMyStarsBackKey] + isShowAbsoluteTime = preferences[showAbsoluteTimeKey] } } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/constant/PreferenceKeys.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/constant/PreferenceKeys.kt index 672331d57..686a57761 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/constant/PreferenceKeys.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/constant/PreferenceKeys.kt @@ -10,8 +10,6 @@ import org.mariotaku.twidere.Constants.KEY_DISPLAY_PROFILE_IMAGE import org.mariotaku.twidere.Constants.KEY_NO_CLOSE_AFTER_TWEET_SENT import org.mariotaku.twidere.TwidereConstants.* import org.mariotaku.twidere.annotation.AccountType -import org.mariotaku.twidere.constant.SharedPreferenceConstants.KEY_CUSTOM_API_TYPE -import org.mariotaku.twidere.constant.SharedPreferenceConstants.VALUE_MEDIA_PREVIEW_STYLE_CODE_CROP import org.mariotaku.twidere.extension.getNonEmptyString import org.mariotaku.twidere.model.CustomAPIConfig import org.mariotaku.twidere.model.account.cred.Credentials @@ -33,7 +31,6 @@ val displaySensitiveContentsKey = KBooleanKey(KEY_DISPLAY_SENSITIVE_CONTENTS, fa val hideCardActionsKey = KBooleanKey(KEY_HIDE_CARD_ACTIONS, false) val iWantMyStarsBackKey = KBooleanKey(KEY_I_WANT_MY_STARS_BACK, false) val showAbsoluteTimeKey = KBooleanKey(KEY_SHOW_ABSOLUTE_TIME, false) -val linkHighlightOptionKey = KStringKey(KEY_LINK_HIGHLIGHT_OPTION, VALUE_LINK_HIGHLIGHT_OPTION_NONE) val statusShortenerKey = KNullableStringKey(KEY_STATUS_SHORTENER, null) val mediaUploaderKey = KNullableStringKey(KEY_MEDIA_UPLOADER, null) val newDocumentApiKey = KBooleanKey(KEY_NEW_DOCUMENT_API, Build.VERSION.SDK_INT == Build.VERSION_CODES.M) @@ -59,6 +56,7 @@ val themeColorKey = KIntKey(KEY_THEME_COLOR, 0) val filterUnavailableQuoteStatusesKey = KBooleanKey("filter_unavailable_quote_statuses", false) val filterPossibilitySensitiveStatusesKey = KBooleanKey("filter_possibility_sensitive_statuses", false) val chromeCustomTabKey = KBooleanKey("chrome_custom_tab", true) + object themeBackgroundAlphaKey : KSimpleKey(KEY_THEME_BACKGROUND_ALPHA, 0xFF) { override fun read(preferences: SharedPreferences): Int { return preferences.getInt(KEY_THEME_BACKGROUND_ALPHA, DEFAULT_THEME_BACKGROUND_ALPHA) @@ -98,6 +96,26 @@ object mediaPreviewStyleKey : KSimpleKey(KEY_MEDIA_PREVIEW_STYLE, VALUE_MED } +object linkHighlightOptionKey : KSimpleKey(KEY_LINK_HIGHLIGHT_OPTION, VALUE_LINK_HIGHLIGHT_OPTION_CODE_NONE) { + override fun read(preferences: SharedPreferences): Int = when (preferences.getString(key, null)) { + VALUE_LINK_HIGHLIGHT_OPTION_BOTH -> VALUE_LINK_HIGHLIGHT_OPTION_CODE_BOTH + VALUE_LINK_HIGHLIGHT_OPTION_UNDERLINE -> VALUE_LINK_HIGHLIGHT_OPTION_CODE_UNDERLINE + VALUE_LINK_HIGHLIGHT_OPTION_HIGHLIGHT -> VALUE_LINK_HIGHLIGHT_OPTION_CODE_HIGHLIGHT + else -> VALUE_LINK_HIGHLIGHT_OPTION_CODE_NONE + } + + override fun write(editor: SharedPreferences.Editor, value: Int): Boolean { + editor.putString(key, when (value) { + VALUE_LINK_HIGHLIGHT_OPTION_CODE_BOTH -> VALUE_LINK_HIGHLIGHT_OPTION_BOTH + VALUE_LINK_HIGHLIGHT_OPTION_CODE_UNDERLINE -> VALUE_LINK_HIGHLIGHT_OPTION_UNDERLINE + VALUE_LINK_HIGHLIGHT_OPTION_CODE_HIGHLIGHT -> VALUE_LINK_HIGHLIGHT_OPTION_HIGHLIGHT + else -> VALUE_LINK_HIGHLIGHT_OPTION_NONE + }) + return true + } + +} + object refreshIntervalKey : KSimpleKey(KEY_REFRESH_INTERVAL, 15) { override fun read(preferences: SharedPreferences): Long { return preferences.getString(key, null).toLong(def) diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/AbsContentListViewFragment.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/AbsContentListViewFragment.kt index b3d4ba7fa..48574e9d9 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/AbsContentListViewFragment.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/AbsContentListViewFragment.kt @@ -113,9 +113,6 @@ abstract class AbsContentListViewFragment : BaseFragment(), override fun onActivityCreated(savedInstanceState: Bundle?) { super.onActivityCreated(savedInstanceState) - - val view = view!! - val context = view.context val backgroundColor = ThemeUtils.getThemeBackgroundColor(context) val colorRes = TwidereColorUtils.getContrastYIQ(backgroundColor, R.color.bg_refresh_progress_color_light, R.color.bg_refresh_progress_color_dark) diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/BaseListFragment.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/BaseListFragment.kt deleted file mode 100644 index 23f06199f..000000000 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/BaseListFragment.kt +++ /dev/null @@ -1,164 +0,0 @@ -/* - * Twidere - Twitter client for Android - * - * Copyright (C) 2012-2014 Mariotaku Lee - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.mariotaku.twidere.fragment - -import android.content.ContentResolver -import android.content.Context -import android.content.SharedPreferences -import android.os.Bundle -import android.support.v4.app.ListFragment -import android.support.v4.app.ListFragmentAccessor -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup -import android.widget.AbsListView -import android.widget.AbsListView.OnScrollListener -import android.widget.ProgressBar -import org.mariotaku.chameleon.Chameleon -import org.mariotaku.chameleon.view.ChameleonProgressBar -import org.mariotaku.twidere.app.TwidereApplication -import org.mariotaku.twidere.constant.IntentConstants.EXTRA_TAB_POSITION -import org.mariotaku.twidere.fragment.iface.RefreshScrollTopInterface -import org.mariotaku.twidere.util.AsyncTwitterWrapper -import org.mariotaku.twidere.util.SharedPreferencesWrapper -import org.mariotaku.twidere.util.Utils -import org.mariotaku.twidere.util.dagger.GeneralComponentHelper -import javax.inject.Inject - -open class BaseListFragment : ListFragment(), OnScrollListener, RefreshScrollTopInterface { - - @Inject - lateinit var twitterWrapper: AsyncTwitterWrapper - @Inject - lateinit var preferences: SharedPreferencesWrapper - var activityFirstCreated: Boolean = false - private set - var instanceStateSaved: Boolean = false - private set - var reachedBottom: Boolean = false - private set - private var notReachedBottomBefore = true - - override fun onAttach(context: Context) { - super.onAttach(context) - GeneralComponentHelper.build(context).inject(this) - } - - val application: TwidereApplication - get() = TwidereApplication.getInstance(activity) - - val contentResolver: ContentResolver? - get() { - val activity = activity - if (activity != null) return activity.contentResolver - return null - } - - fun getSharedPreferences(name: String, mode: Int): SharedPreferences? { - val activity = activity - if (activity != null) return activity.getSharedPreferences(name, mode) - return null - } - - fun getSystemService(name: String): Any? { - val activity = activity - if (activity != null) return activity.getSystemService(name) - return null - } - - val tabPosition: Int - get() { - return arguments?.getInt(EXTRA_TAB_POSITION, -1) ?: -1 - } - - override fun onActivityCreated(savedInstanceState: Bundle?) { - super.onActivityCreated(savedInstanceState) - instanceStateSaved = savedInstanceState != null - val lv = listView - lv.setOnScrollListener(this) - } - - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - activityFirstCreated = true - } - - override fun onDestroy() { - super.onDestroy() - activityFirstCreated = true - } - - override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? { - val view = super.onCreateView(inflater, container, savedInstanceState)!! - ((view.findViewById(ListFragmentAccessor.INTERNAL_PROGRESS_CONTAINER_ID) as ViewGroup).getChildAt(0) as ProgressBar).apply { - val appearance = ChameleonProgressBar.Appearance() - appearance.progressColor = Chameleon.getOverrideTheme(activity, activity).colorPrimary - ChameleonProgressBar.Appearance.apply(this, appearance) - } - return view - } - - override fun onScroll(view: AbsListView, firstVisibleItem: Int, visibleItemCount: Int, - totalItemCount: Int) { - val reached = firstVisibleItem + visibleItemCount >= totalItemCount && totalItemCount >= visibleItemCount - - if (reachedBottom != reached) { - reachedBottom = reached - if (reachedBottom && notReachedBottomBefore) { - notReachedBottomBefore = false - return - } - if (reachedBottom && listAdapter.count > visibleItemCount) { - onReachedBottom() - } - } - - } - - override fun onScrollStateChanged(view: AbsListView, scrollState: Int) { - - } - - override fun onStart() { - super.onStart() - } - - override fun onStop() { - activityFirstCreated = false - super.onStop() - } - - override fun scrollToStart(): Boolean { - Utils.scrollListToTop(listView) - return true - } - - override fun setSelection(position: Int) { - Utils.scrollListToPosition(listView, position) - } - - override fun triggerRefresh(): Boolean { - return false - } - - protected fun onReachedBottom() { - - } -} diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/DraftsFragment.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/DraftsFragment.kt index fb4ce2255..38c23306b 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/DraftsFragment.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/DraftsFragment.kt @@ -43,6 +43,7 @@ import android.widget.AdapterView import android.widget.AdapterView.OnItemClickListener import android.widget.ListView import kotlinx.android.synthetic.main.fragment_drafts.* +import org.mariotaku.kpreferences.get import org.mariotaku.ktextension.toStringArray import org.mariotaku.sqliteqb.library.Columns.Column import org.mariotaku.sqliteqb.library.Expression @@ -52,6 +53,7 @@ import org.mariotaku.twidere.TwidereConstants.* import org.mariotaku.twidere.activity.iface.IExtendedActivity import org.mariotaku.twidere.adapter.DraftsAdapter import org.mariotaku.twidere.constant.IntentConstants +import org.mariotaku.twidere.constant.textSizeKey import org.mariotaku.twidere.extension.invertSelection import org.mariotaku.twidere.extension.selectAll import org.mariotaku.twidere.extension.selectNone @@ -64,7 +66,6 @@ import org.mariotaku.twidere.provider.TwidereDataStore.Drafts import org.mariotaku.twidere.service.LengthyOperationsService import org.mariotaku.twidere.util.AsyncTaskUtils import org.mariotaku.twidere.util.JsonSerializer -import org.mariotaku.twidere.util.Utils.getDefaultTextSize import java.io.File import java.util.* @@ -75,7 +76,7 @@ class DraftsFragment : BaseFragment(), LoaderCallbacks, OnItemClickList override fun onActivityCreated(savedInstanceState: Bundle?) { super.onActivityCreated(savedInstanceState) adapter = DraftsAdapter(activity).apply { - textSize = preferences.getInt(KEY_TEXT_SIZE, getDefaultTextSize(activity)).toFloat() + textSize = preferences[textSizeKey].toFloat() } listView.adapter = adapter diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/ExtensionsListFragment.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/ExtensionsListFragment.kt index 222a8e8e2..ecec09928 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/ExtensionsListFragment.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/ExtensionsListFragment.kt @@ -19,6 +19,7 @@ package org.mariotaku.twidere.fragment +import android.content.Context import android.content.Intent import android.content.pm.PackageManager import android.net.Uri @@ -31,8 +32,10 @@ import android.view.ContextMenu.ContextMenuInfo import android.view.MenuInflater import android.view.MenuItem import android.view.View +import android.widget.AdapterView import android.widget.AdapterView.AdapterContextMenuInfo -import android.widget.ListView +import kotlinx.android.synthetic.main.fragment_content_listview.* +import org.mariotaku.ktextension.isNullOrEmpty import org.mariotaku.ktextension.setItemAvailability import org.mariotaku.twidere.R import org.mariotaku.twidere.TwidereConstants.LOGTAG @@ -42,24 +45,25 @@ import org.mariotaku.twidere.loader.ExtensionsListLoader import org.mariotaku.twidere.loader.ExtensionsListLoader.ExtensionInfo import org.mariotaku.twidere.util.PermissionsManager -class ExtensionsListFragment : BaseListFragment(), LoaderCallbacks> { +class ExtensionsListFragment : AbsContentListViewFragment(), + LoaderCallbacks>, AdapterView.OnItemClickListener { private var packageManager: PackageManager? = null - private var permissionsManager: PermissionsManager? = null + private var permissionsManager: PermissionsManager? = null override fun onActivityCreated(savedInstanceState: Bundle?) { super.onActivityCreated(savedInstanceState) packageManager = activity.packageManager permissionsManager = PermissionsManager(activity) - listAdapter = ExtensionsAdapter(activity) - listView.setOnCreateContextMenuListener(this) + + listView.onItemClickListener = this + loaderManager.initLoader(0, null, this) - setEmptyText(getString(R.string.no_extension_installed)) - setListShown(false) + showProgress() } - override fun onStop() { - super.onStop() + override fun onCreateAdapter(context: Context): ExtensionsAdapter { + return ExtensionsAdapter(activity) } override fun onCreateLoader(id: Int, args: Bundle?): Loader> { @@ -67,28 +71,32 @@ class ExtensionsListFragment : BaseListFragment(), LoaderCallbacks>, data: List) { - (listAdapter as ExtensionsAdapter).setData(data) - setListShown(true) + adapter.setData(data) + if (data.isNullOrEmpty()) { + showEmpty(R.drawable.ic_info_info_generic, getString(R.string.no_extension_installed)) + } else { + showContent() + } } override fun onLoaderReset(loader: Loader>) { - (listAdapter as ExtensionsAdapter).setData(null) + adapter.setData(null) } - override fun onListItemClick(l: ListView?, v: View?, position: Int, id: Long) { - openSettings((listAdapter as ExtensionsAdapter).getItem(position)) + override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) { + openSettings(adapter.getItem(position)) } override fun onResume() { super.onResume() - (listAdapter as ExtensionsAdapter).notifyDataSetChanged() + adapter.notifyDataSetChanged() } override fun onCreateContextMenu(menu: ContextMenu, v: View, menuInfo: ContextMenuInfo) { val inflater = MenuInflater(v.context) inflater.inflate(R.menu.action_extension, menu) val adapterMenuInfo = menuInfo as AdapterContextMenuInfo - val extensionInfo = (listAdapter as ExtensionsAdapter).getItem(adapterMenuInfo.position) + val extensionInfo = adapter.getItem(adapterMenuInfo.position) if (extensionInfo.pname != null && extensionInfo.settings != null) { val intent = Intent(IntentConstants.INTENT_ACTION_EXTENSION_SETTINGS) intent.setClassName(extensionInfo.pname, extensionInfo.settings) @@ -101,7 +109,7 @@ class ExtensionsListFragment : BaseListFragment(), LoaderCallbacks { openSettings(extensionInfo) @@ -111,7 +119,7 @@ class ExtensionsListFragment : BaseListFragment(), LoaderCallbacks { permissionsManager!!.revoke(extensionInfo.pname) - (listAdapter as ExtensionsAdapter).notifyDataSetChanged() + adapter.notifyDataSetChanged() } else -> { return false diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/HostMappingsListFragment.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/HostMappingsListFragment.kt index c89e765e4..7198573b0 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/HostMappingsListFragment.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/fragment/HostMappingsListFragment.kt @@ -34,6 +34,7 @@ import android.view.* import android.widget.* import android.widget.AbsListView.MultiChoiceModeListener import android.widget.CompoundButton.OnCheckedChangeListener +import kotlinx.android.synthetic.main.fragment_content_listview.* import org.apache.commons.lang3.StringUtils import org.mariotaku.twidere.Constants import org.mariotaku.twidere.R @@ -42,24 +43,26 @@ import org.mariotaku.twidere.adapter.ArrayAdapter import org.mariotaku.twidere.util.ParseUtils import org.mariotaku.twidere.util.SharedPreferencesWrapper -class HostMappingsListFragment : BaseListFragment(), MultiChoiceModeListener, OnSharedPreferenceChangeListener { +class HostMappingsListFragment : AbsContentListViewFragment(), + AdapterView.OnItemClickListener, MultiChoiceModeListener, OnSharedPreferenceChangeListener { - private var mAdapter: HostMappingAdapter? = null - private var mHostMapping: SharedPreferencesWrapper? = null + private lateinit var hostMapping: SharedPreferencesWrapper override fun onActivityCreated(savedInstanceState: Bundle?) { super.onActivityCreated(savedInstanceState) setHasOptionsMenu(true) - mHostMapping = SharedPreferencesWrapper.getInstance(activity, + hostMapping = SharedPreferencesWrapper.getInstance(activity, Constants.HOST_MAPPING_PREFERENCES_NAME, Context.MODE_PRIVATE) - mHostMapping!!.registerOnSharedPreferenceChangeListener(this) - mAdapter = HostMappingAdapter(activity) - listAdapter = mAdapter + hostMapping.registerOnSharedPreferenceChangeListener(this) listView.choiceMode = ListView.CHOICE_MODE_MULTIPLE_MODAL listView.setMultiChoiceModeListener(this) reloadHostMappings() } + override fun onCreateAdapter(context: Context): HostMappingAdapter { + return HostMappingAdapter(activity) + } + override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean { mode.menuInflater.inflate(R.menu.action_multi_select_items, menu) return true @@ -73,15 +76,12 @@ class HostMappingsListFragment : BaseListFragment(), MultiChoiceModeListener, On override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean { when (item.itemId) { R.id.delete -> { - val editor = mHostMapping!!.edit() + val editor = hostMapping.edit() val array = listView!!.checkedItemPositions ?: return false - var i = 0 - val size = array.size() - while (i < size) { + for (i in 0 until array.size()) { if (array.valueAt(i)) { - editor.remove(mAdapter!!.getItem(i)) + editor.remove(adapter.getItem(i).first) } - i++ } editor.apply() reloadHostMappings() @@ -102,9 +102,8 @@ class HostMappingsListFragment : BaseListFragment(), MultiChoiceModeListener, On inflater!!.inflate(R.menu.menu_host_mapping, menu) } - override fun onListItemClick(l: ListView?, v: View?, position: Int, id: Long) { - val host = mAdapter!!.getItem(position) - val address = mAdapter!!.getAddress(host) + override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) { + val (host, address) = adapter.getItem(position) val args = Bundle() args.putString(EXTRA_HOST, host) args.putString(EXTRA_ADDRESS, address) @@ -135,8 +134,16 @@ class HostMappingsListFragment : BaseListFragment(), MultiChoiceModeListener, On } fun reloadHostMappings() { - if (mAdapter == null) return - mAdapter!!.reload() + adapter.clear() + adapter.addAll(hostMapping.all.mapNotNull { entry -> + val value = entry.value?.toString() ?: return@mapNotNull null + return@mapNotNull Pair(entry.key, value) + }) + if (adapter.isEmpty) { + showEmpty(R.drawable.ic_info_info_generic, getString(R.string.add_host_mapping)) + } else { + showContent() + } } private fun updateTitle(mode: ActionMode?) { @@ -235,38 +242,22 @@ class HostMappingsListFragment : BaseListFragment(), MultiChoiceModeListener, On } } - internal class HostMappingAdapter(context: Context) : ArrayAdapter(context, android.R.layout.simple_list_item_activated_2) { - - private val mHostMapping: SharedPreferences - - init { - mHostMapping = context.getSharedPreferences(Constants.HOST_MAPPING_PREFERENCES_NAME, Context.MODE_PRIVATE) - } + class HostMappingAdapter(context: Context) : ArrayAdapter>(context, + android.R.layout.simple_list_item_activated_2) { override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { val view = super.getView(position, convertView, parent) val text1 = view.findViewById(android.R.id.text1) as TextView val text2 = view.findViewById(android.R.id.text2) as TextView - val key = getItem(position) + val (key, value) = getItem(position) text1.text = key - val value = getAddress(key) - if (StringUtils.equals(key, value)) { + if (key == value) { text2.setText(R.string.excluded) } else { text2.text = value } return view } - - fun reload() { - clear() - val all = mHostMapping.all - addAll(all.keys) - } - - fun getAddress(key: String): String { - return mHostMapping.getString(key, null) - } } companion object { diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/util/dagger/GeneralComponent.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/util/dagger/GeneralComponent.kt index ab2307049..bfa0af8d6 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/util/dagger/GeneralComponent.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/util/dagger/GeneralComponent.kt @@ -72,8 +72,6 @@ interface GeneralComponent { fun inject(obj: TwidereDataProvider) - fun inject(obj: BaseListFragment) - fun inject(obj: BaseActivity) fun inject(obj: BaseRecyclerViewAdapter) diff --git a/twidere/src/main/res/layout/activity_settings.xml b/twidere/src/main/res/layout/activity_settings.xml index 1f4dae8d2..3ee271622 100644 --- a/twidere/src/main/res/layout/activity_settings.xml +++ b/twidere/src/main/res/layout/activity_settings.xml @@ -25,7 +25,7 @@ android:layout_height="match_parent"> @@ -43,8 +43,8 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="end" - android:layout_marginLeft="@dimen/element_size_normal" - android:layout_marginStart="@dimen/element_size_normal" + android:layout_marginLeft="@dimen/settings_panel_margin_start_details" + android:layout_marginStart="@dimen/settings_panel_margin_start_details" android:clickable="true" tools:background="?android:colorBackground"/> diff --git a/twidere/src/main/res/values-sw600dp/dimens.xml b/twidere/src/main/res/values-sw600dp/dimens.xml new file mode 100644 index 000000000..8e6ce6475 --- /dev/null +++ b/twidere/src/main/res/values-sw600dp/dimens.xml @@ -0,0 +1,4 @@ + + + @dimen/settings_panel_width_entries + \ No newline at end of file diff --git a/twidere/src/main/res/values-xlarge/integers.xml b/twidere/src/main/res/values-xlarge/integers.xml index 6439266da..aec9806f5 100644 --- a/twidere/src/main/res/values-xlarge/integers.xml +++ b/twidere/src/main/res/values-xlarge/integers.xml @@ -3,7 +3,6 @@ 15 19 - 17 12 2 2 diff --git a/twidere/src/main/res/values/dimens.xml b/twidere/src/main/res/values/dimens.xml index 8b74c3425..87e73e279 100644 --- a/twidere/src/main/res/values/dimens.xml +++ b/twidere/src/main/res/values/dimens.xml @@ -95,4 +95,7 @@ 40dp + 240dp + @dimen/element_size_normal + \ No newline at end of file