Move extensions into objects

pull/1432/head
Alex Baker 4 years ago
parent e16cadc374
commit 0f82b39913

@ -65,7 +65,7 @@ import org.tasks.db.SuspendDbUtils.chunkedMap
import org.tasks.dialogs.DateTimePicker.Companion.newDateTimePicker
import org.tasks.dialogs.DialogBuilder
import org.tasks.dialogs.SortDialog
import org.tasks.extensions.safeStartActivityForResult
import org.tasks.extensions.Fragment.safeStartActivityForResult
import org.tasks.filters.PlaceFilter
import org.tasks.intents.TaskIntents
import org.tasks.locale.Locale

@ -12,7 +12,7 @@ import com.todoroo.astrid.api.FilterListItem.NO_ORDER
import com.todoroo.astrid.helper.UUIDHelper
import net.fortuna.ical4j.model.property.Geo
import org.tasks.Strings
import org.tasks.extensions.safeStartActivity
import org.tasks.extensions.Context.safeStartActivity
import org.tasks.location.MapPosition
import org.tasks.themes.CustomIcons.PLACE
import java.io.Serializable

@ -10,7 +10,7 @@ import com.todoroo.astrid.files.FilesControlSet
import dagger.hilt.android.AndroidEntryPoint
import org.tasks.R
import org.tasks.activities.CameraActivity
import org.tasks.extensions.safeStartActivityForResult
import org.tasks.extensions.Fragment.safeStartActivityForResult
import org.tasks.files.FileHelper.newFilePickerIntent
import org.tasks.preferences.Device
import java.util.*

@ -0,0 +1,21 @@
package org.tasks.extensions
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.widget.Toast
import org.tasks.R
object Context {
fun Context.safeStartActivity(intent: Intent) {
try {
startActivity(intent)
} catch (e: ActivityNotFoundException) {
toast(R.string.no_app_found)
}
}
fun Context.toast(resId: Int, duration: Int = Toast.LENGTH_LONG) {
Toast.makeText(this, resId, duration).show()
}
}

@ -1,30 +0,0 @@
package org.tasks.extensions
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.widget.Toast
import androidx.fragment.app.Fragment
import org.tasks.R
fun Context.safeStartActivity(intent: Intent) {
try {
startActivity(intent)
} catch (e: ActivityNotFoundException) {
toast(this)
}
}
fun Fragment.safeStartActivityForResult(intent: Intent, rc: Int) {
try {
startActivityForResult(intent, rc)
} catch (e: ActivityNotFoundException) {
toast(context)
}
}
private fun toast(context: Context?) {
context?.let {
Toast.makeText(it, R.string.no_app_found, Toast.LENGTH_LONG).show()
}
}

@ -0,0 +1,17 @@
package org.tasks.extensions
import android.content.ActivityNotFoundException
import android.content.Intent
import androidx.fragment.app.Fragment
import org.tasks.R
import org.tasks.extensions.Context.toast
object Fragment {
fun Fragment.safeStartActivityForResult(intent: Intent, rc: Int) {
try {
startActivityForResult(intent, rc)
} catch (e: ActivityNotFoundException) {
context?.toast(R.string.no_app_found)
}
}
}

@ -0,0 +1,18 @@
package org.tasks.extensions
import android.content.res.Resources
object Resources {
fun Resources.getMutableStringList(id: Int): MutableList<String> =
getStringArray(id).toMutableList()
fun Resources.getMutableIntList(id: Int): MutableList<Int> {
val typedArray = obtainTypedArray(id)
val result = IntArray(typedArray.length())
for (i in result.indices) {
result[i] = typedArray.getResourceId(i, 0)
}
typedArray.recycle()
return result.toMutableList()
}
}

@ -1,16 +0,0 @@
package org.tasks.extensions
import android.content.res.Resources
fun Resources.getMutableStringList(id: Int): MutableList<String> =
getStringArray(id).toMutableList()
fun Resources.getMutableIntList(id: Int): MutableList<Int> {
val typedArray = obtainTypedArray(id)
val result = IntArray(typedArray.length())
for (i in result.indices) {
result[i] = typedArray.getResourceId(i, 0)
}
typedArray.recycle()
return result.toMutableList()
}

@ -18,7 +18,7 @@ import com.google.common.io.ByteStreams
import com.google.common.io.Files
import com.todoroo.astrid.utility.Constants
import org.tasks.Strings.isNullOrEmpty
import org.tasks.extensions.safeStartActivity
import org.tasks.extensions.Context.safeStartActivity
import timber.log.Timber
import java.io.File
import java.io.FileNotFoundException

@ -19,8 +19,8 @@ import org.tasks.auth.SignInActivity
import org.tasks.caldav.CaldavAccountSettingsActivity
import org.tasks.dialogs.DialogBuilder
import org.tasks.etebase.EtebaseAccountSettingsActivity
import org.tasks.extensions.getMutableIntList
import org.tasks.extensions.getMutableStringList
import org.tasks.extensions.Resources.getMutableIntList
import org.tasks.extensions.Resources.getMutableStringList
import org.tasks.preferences.fragments.MainSettingsFragment.Companion.REQUEST_CALDAV_SETTINGS
import org.tasks.preferences.fragments.MainSettingsFragment.Companion.REQUEST_GOOGLE_TASKS
import org.tasks.preferences.fragments.MainSettingsFragment.Companion.REQUEST_TASKS_ORG

Loading…
Cancel
Save