diff --git a/twidere.component.common/src/main/java/org/mariotaku/twidere/model/UserKey.java b/twidere.component.common/src/main/java/org/mariotaku/twidere/model/UserKey.java index 25003317b..e69fb8825 100644 --- a/twidere.component.common/src/main/java/org/mariotaku/twidere/model/UserKey.java +++ b/twidere.component.common/src/main/java/org/mariotaku/twidere/model/UserKey.java @@ -160,8 +160,7 @@ public class UserKey implements Comparable, Parcelable { } @Nullable - public static UserKey[] arrayOf(@Nullable String str) { - if (str == null) return null; + public static UserKey[] arrayOf(@NonNull String str) { List split = split(str, ","); UserKey[] keys = new UserKey[split.size()]; for (int i = 0, splitLength = split.size(); i < splitLength; i++) { 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 cfb4c3aaf..bceb706e0 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/activity/ComposeActivity.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/activity/ComposeActivity.kt @@ -110,8 +110,9 @@ class ComposeActivity : BaseActivity(), OnMenuItemClickListener, OnClickListener lateinit var validator: TwidereValidator @Inject lateinit var defaultFeatures: DefaultFeatures + @Inject + lateinit var locationManager: LocationManager - private lateinit var locationManager: LocationManager private lateinit var itemTouchHelper: ItemTouchHelper private val supportMenuInflater by lazy { SupportMenuInflater(this) } private var currentTask: AsyncTask? = null @@ -229,9 +230,10 @@ class ComposeActivity : BaseActivity(), OnMenuItemClickListener, OnClickListener override fun onStart() { super.onStart() - imageUploaderUsed = !ServicePickerPreference.isNoneValue(preferences.getString(KEY_MEDIA_UPLOADER, null)) - statusShortenerUsed = !ServicePickerPreference.isNoneValue(preferences.getString(KEY_STATUS_SHORTENER, null)) - if (preferences[attachLocationKey]) { + + imageUploaderUsed = !ServicePickerPreference.isNoneValue(kPreferences[mediaUploaderKey]) + statusShortenerUsed = !ServicePickerPreference.isNoneValue(kPreferences[statusShortenerKey]) + if (kPreferences[attachLocationKey]) { if (checkAnySelfPermissionsGranted(AndroidPermission.ACCESS_COARSE_LOCATION, AndroidPermission.ACCESS_FINE_LOCATION)) { try { startLocationUpdateIfEnabled() @@ -480,7 +482,6 @@ class ComposeActivity : BaseActivity(), OnMenuItemClickListener, OnClickListener override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) GeneralComponentHelper.build(this).inject(this) - locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager nameFirst = preferences[nameFirstKey] setContentView(R.layout.activity_compose) @@ -504,8 +505,8 @@ class ComposeActivity : BaseActivity(), OnMenuItemClickListener, OnClickListener accountSelectorButton.setOnClickListener(this) replyLabel.setOnClickListener(this) locationSwitch.max = LOCATION_OPTIONS.size - val attachLocation = preferences[attachLocationKey] - val attachPreciseLocation = preferences[attachPreciseLocationKey] + val attachLocation = kPreferences[attachLocationKey] + val attachPreciseLocation = kPreferences[attachPreciseLocationKey] if (attachLocation) { if (attachPreciseLocation) { locationSwitch.checkedPosition = LOCATION_OPTIONS.indexOf(LOCATION_VALUE_COORDINATE) @@ -530,10 +531,10 @@ class ComposeActivity : BaseActivity(), OnMenuItemClickListener, OnClickListener attachPreciseLocationChecked = false } } - preferences.edit() - .putBoolean(KEY_ATTACH_LOCATION, attachLocationChecked) - .putBoolean(KEY_ATTACH_PRECISE_LOCATION, attachPreciseLocationChecked) - .apply() + kPreferences.edit { + this[attachLocationKey] = attachLocationChecked + this[attachPreciseLocationKey] = attachPreciseLocationChecked + } if (attachLocationChecked) { requestOrUpdateLocation() } else if (locationListener != null) { @@ -599,7 +600,7 @@ class ComposeActivity : BaseActivity(), OnMenuItemClickListener, OnClickListener setLabel(intent) val selectedAccountIds = accountsAdapter.selectedAccountKeys if (ArrayUtils.isEmpty(selectedAccountIds)) { - val idsInPrefs: Array = UserKey.arrayOf(preferences.getString(KEY_COMPOSE_ACCOUNTS, null)) ?: emptyArray() + val idsInPrefs: Array = kPreferences[composeAccountsKey] ?: emptyArray() val intersection: Array = defaultAccountIds.intersect(listOf(*idsInPrefs)).toTypedArray() if (intersection.isEmpty()) { 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 622c60f63..e5c35baa0 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/constant/PreferenceKeys.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/constant/PreferenceKeys.kt @@ -10,9 +10,11 @@ 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_COMPOSE_ACCOUNTS import org.mariotaku.twidere.constant.SharedPreferenceConstants.KEY_DISPLAY_SENSITIVE_CONTENTS import org.mariotaku.twidere.extension.getNonEmptyString import org.mariotaku.twidere.model.CustomAPIConfig +import org.mariotaku.twidere.model.UserKey import org.mariotaku.twidere.model.account.cred.Credentials import org.mariotaku.twidere.model.sync.SyncProviderInfo import org.mariotaku.twidere.preference.ThemeBackgroundPreference @@ -187,4 +189,18 @@ object dataSyncProviderInfoKey : KPreferenceKey { return true } +} + +object composeAccountsKey : KSimpleKey?>(KEY_COMPOSE_ACCOUNTS, null) { + + override fun read(preferences: SharedPreferences): Array? { + val string = preferences.getString(key, null) ?: return null + return UserKey.arrayOf(string) + } + + override fun write(editor: SharedPreferences.Editor, value: Array?): Boolean { + editor.putString(key, value?.joinToString(",")) + return true + } + } \ No newline at end of file diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/util/dagger/ApplicationModule.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/util/dagger/ApplicationModule.kt index ecb17e36e..52a999440 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/util/dagger/ApplicationModule.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/util/dagger/ApplicationModule.kt @@ -21,6 +21,7 @@ package org.mariotaku.twidere.util.dagger import android.app.Application import android.content.Context +import android.location.LocationManager import android.os.Build import android.os.Looper import android.support.v4.text.BidiFormatter @@ -293,6 +294,11 @@ class ApplicationModule(private val application: Application) { return ETagCache(application) } + @Provides + fun locationManager(): LocationManager { + return application.getSystemService(Context.LOCATION_SERVICE) as LocationManager + } + private fun createDiskCache(dirName: String, preferences: SharedPreferencesWrapper): DiskCache { val cacheDir = Utils.getExternalCacheDir(application, dirName) val fallbackCacheDir = Utils.getInternalCacheDir(application, dirName)