mirror of https://github.com/tasks/tasks
Move extensions into objects
parent
e16cadc374
commit
0f82b39913
@ -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()
|
||||
}
|
Loading…
Reference in New Issue