Skip to content
This repository has been archived by the owner on Jul 13, 2020. It is now read-only.

migrate anko commons(include v4, v7) to androidx #735

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion anko/library/library.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ publishing {
version ANKO_VERSION

artifact sourcesJar
artifact bundleRelease
artifact bundleReleaseAar

pom.withXml {
asNode().children().last() + {
Expand Down
6 changes: 6 additions & 0 deletions anko/library/static/androidxAppcompat/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apply from: '../../library.gradle'

dependencies {
api "androidx.appcompat:appcompat:$androidx_appcompat_version"
api project(':static:commons-base')
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="org.jetbrains.anko.appcompatV7" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Copyright 2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jetbrains.anko.appcompat.v7

import android.content.Context
import android.content.DialogInterface
import android.graphics.drawable.Drawable
import android.view.KeyEvent
import android.view.View
import androidx.appcompat.app.AlertDialog
import org.jetbrains.anko.AlertBuilder
import org.jetbrains.anko.AlertBuilderFactory
import org.jetbrains.anko.internals.AnkoInternals
import org.jetbrains.anko.internals.AnkoInternals.NO_GETTER
import kotlin.DeprecationLevel.ERROR

val Appcompat: AlertBuilderFactory<AlertDialog> = ::AppcompatAlertBuilder

internal class AppcompatAlertBuilder(override val ctx: Context) : AlertBuilder<AlertDialog> {
private val builder = AlertDialog.Builder(ctx)

override var title: CharSequence
@Deprecated(NO_GETTER, level = ERROR) get() = AnkoInternals.noGetter()
set(value) { builder.setTitle(value) }

override var titleResource: Int
@Deprecated(NO_GETTER, level = ERROR) get() = AnkoInternals.noGetter()
set(value) { builder.setTitle(value) }

override var message: CharSequence
@Deprecated(NO_GETTER, level = ERROR) get() = AnkoInternals.noGetter()
set(value) { builder.setMessage(value) }

override var messageResource: Int
@Deprecated(NO_GETTER, level = ERROR) get() = AnkoInternals.noGetter()
set(value) { builder.setMessage(value) }

override var icon: Drawable
@Deprecated(NO_GETTER, level = ERROR) get() = AnkoInternals.noGetter()
set(value) { builder.setIcon(value) }

override var iconResource: Int
@Deprecated(NO_GETTER, level = ERROR) get() = AnkoInternals.noGetter()
set(value) { builder.setIcon(value) }

override var customTitle: View
@Deprecated(NO_GETTER, level = ERROR) get() = AnkoInternals.noGetter()
set(value) { builder.setCustomTitle(value) }

override var customView: View
@Deprecated(NO_GETTER, level = ERROR) get() = AnkoInternals.noGetter()
set(value) { builder.setView(value) }

override var isCancelable: Boolean
@Deprecated(NO_GETTER, level = ERROR) get() = AnkoInternals.noGetter()
set(value) { builder.setCancelable(value) }

override fun onCancelled(handler: (DialogInterface) -> Unit) {
builder.setOnCancelListener(handler)
}

override fun onKeyPressed(handler: (dialog: DialogInterface, keyCode: Int, e: KeyEvent) -> Boolean) {
builder.setOnKeyListener(handler)
}

override fun positiveButton(buttonText: String, onClicked: (dialog: DialogInterface) -> Unit) {
builder.setPositiveButton(buttonText) { dialog, _ -> onClicked(dialog) }
}

override fun positiveButton(buttonTextResource: Int, onClicked: (dialog: DialogInterface) -> Unit) {
builder.setPositiveButton(buttonTextResource) { dialog, _ -> onClicked(dialog) }
}

override fun negativeButton(buttonText: String, onClicked: (dialog: DialogInterface) -> Unit) {
builder.setNegativeButton(buttonText) { dialog, _ -> onClicked(dialog) }
}

override fun negativeButton(buttonTextResource: Int, onClicked: (dialog: DialogInterface) -> Unit) {
builder.setNegativeButton(buttonTextResource) { dialog, _ -> onClicked(dialog) }
}

override fun neutralPressed(buttonText: String, onClicked: (dialog: DialogInterface) -> Unit) {
builder.setNeutralButton(buttonText) { dialog, _ -> onClicked(dialog) }
}

override fun neutralPressed(buttonTextResource: Int, onClicked: (dialog: DialogInterface) -> Unit) {
builder.setNeutralButton(buttonTextResource) { dialog, _ -> onClicked(dialog) }
}

override fun items(items: List<CharSequence>, onItemSelected: (dialog: DialogInterface, index: Int) -> Unit) {
builder.setItems(Array(items.size) { i -> items[i].toString() }) { dialog, which ->
onItemSelected(dialog, which)
}
}

override fun <T> items(items: List<T>, onItemSelected: (dialog: DialogInterface, item: T, index: Int) -> Unit) {
builder.setItems(Array(items.size) { i -> items[i].toString() }) { dialog, which ->
onItemSelected(dialog, items[which], which)
}
}

override fun build(): AlertDialog = builder.create()

override fun show(): AlertDialog = builder.show()
}
6 changes: 6 additions & 0 deletions anko/library/static/androidxLegacySupportV4/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apply from: '../../library.gradle'

dependencies {
api "androidx.legacy:legacy-support-v4:$androidx_legacy_version"
api project(':static:commons-base')
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="org.jetbrains.anko.supportV4" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

@file:Suppress("unused")
package org.jetbrains.anko.support.v4

import android.view.View
import androidx.fragment.app.Fragment
import org.jetbrains.anko.*
import org.jetbrains.anko.internals.AnkoInternals
import org.jetbrains.anko.internals.AnkoInternals.createAnkoContext

inline fun <reified T : View> Fragment.find(id: Int): T = view?.findViewById(id) as T
inline fun <reified T : View> Fragment.findOptional(id: Int): T? = view?.findViewById(id) as? T

fun Fragment.UI(init: AnkoContext<Fragment>.() -> Unit) = createAnkoContext(requireActivity(), init)

inline fun <T: Any> Fragment.configuration(
screenSize: ScreenSize? = null,
density: ClosedRange<Int>? = null,
language: String? = null,
orientation: Orientation? = null,
long: Boolean? = null,
fromSdk: Int? = null,
sdk: Int? = null,
uiMode: UiMode? = null,
nightMode: Boolean? = null,
rightToLeft: Boolean? = null,
smallestWidth: Int? = null,
init: () -> T
): T? {
val act = activity
return if (act != null) {
if (AnkoInternals.testConfiguration(act, screenSize, density, language, orientation, long,
fromSdk, sdk, uiMode, nightMode, rightToLeft, smallestWidth)) init() else null
}
else null
}

fun <T: Fragment> T.withArguments(vararg params: Pair<String, Any?>): T {
arguments = bundleOf(*params)
return this
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

@file:Suppress("unused")
package org.jetbrains.anko.support.v4

import androidx.fragment.app.Fragment
import org.jetbrains.anko.AnkoAsyncContext

fun <T: Fragment> AnkoAsyncContext<T>.supportFragmentUiThread(f: (T) -> Unit): Boolean {
val fragment = weakRef.get() ?: return true
if (fragment.isDetached) return true
val activity = fragment.activity ?: return true
activity.runOnUiThread { f(fragment) }
return true
}

@Deprecated("Use onUiThread() instead", ReplaceWith("onUiThread(f)"))
inline fun Fragment.uiThread(crossinline f: () -> Unit) {
requireActivity().runOnUiThread { f() }
}

@Deprecated(message = "Use runOnUiThread() instead", replaceWith = ReplaceWith("runOnUiThread(f)"))
inline fun Fragment.onUiThread(crossinline f: () -> Unit) {
requireActivity().runOnUiThread { f() }
}

inline fun Fragment.runOnUiThread(crossinline f: () -> Unit) {
requireActivity().runOnUiThread { f() }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

@file:Suppress("unused")
package org.jetbrains.anko.support.v4

import android.content.Context
import android.content.SharedPreferences
import android.preference.PreferenceManager
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity

inline val Fragment.defaultSharedPreferences: SharedPreferences
get() = PreferenceManager.getDefaultSharedPreferences(activity)

@Deprecated(message = "Use either activity or requireActivity", replaceWith = ReplaceWith("activity"))
inline val Fragment.act: FragmentActivity
get() = requireActivity()

@Deprecated(message = "Use either context or requireContext", replaceWith = ReplaceWith("context"))
inline val Fragment.ctx: Context
get() = requireActivity()
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

@file:Suppress("NOTHING_TO_INLINE", "unused")
package org.jetbrains.anko.support.v4

import android.app.ProgressDialog
import android.content.DialogInterface
import androidx.fragment.app.Fragment
import org.jetbrains.anko.*

inline fun Fragment.toast(textResource: Int) = requireActivity().toast(textResource)

inline fun Fragment.toast(text: CharSequence) = requireActivity().toast(text)

inline fun Fragment.longToast(textResource: Int) = requireActivity().longToast(textResource)

inline fun Fragment.longToast(text: CharSequence) = requireActivity().longToast(text)

inline fun Fragment.selector(
title: CharSequence? = null,
items: List<CharSequence>,
noinline onClick: (DialogInterface, Int) -> Unit
): Unit = requireActivity().selector(title, items, onClick)

inline fun Fragment.alert(
message: String,
title: String? = null,
noinline init: (AlertBuilder<DialogInterface>.() -> Unit)? = null
) = requireActivity().alert(message, title, init)

inline fun Fragment.alert(
message: Int,
title: Int? = null,
noinline init: (AlertBuilder<DialogInterface>.() -> Unit)? = null
) = requireActivity().alert(message, title, init)

inline fun Fragment.alert(noinline init: AlertBuilder<DialogInterface>.() -> Unit) = requireActivity().alert(init)

@Deprecated(message = "Android progress dialogs are deprecated")
inline fun Fragment.progressDialog(
message: String? = null,
title: String? = null,
noinline init: (ProgressDialog.() -> Unit)? = null
) = requireActivity().progressDialog(message, title, init)

@Deprecated(message = "Android progress dialogs are deprecated")
inline fun Fragment.indeterminateProgressDialog(message: String? = null, title: String? = null, noinline init: (ProgressDialog.() -> Unit)? = null): ProgressDialog {
return requireActivity().indeterminateProgressDialog(message, title, init)
}

@Deprecated(message = "Android progress dialogs are deprecated")
inline fun Fragment.progressDialog(message: Int? = null, title: Int? = null, noinline init: (ProgressDialog.() -> Unit)? = null): ProgressDialog {
return requireActivity().progressDialog(message?.let { requireActivity().getString(it) }, title?.let { requireActivity().getString(it) }, init)
}

@Deprecated(message = "Android progress dialogs are deprecated")
inline fun Fragment.indeterminateProgressDialog(message: Int? = null, title: Int? = null, noinline init: (ProgressDialog.() -> Unit)? = null): ProgressDialog {
return requireActivity().indeterminateProgressDialog(message?.let { requireActivity().getString(it) }, title?.let { requireActivity().getString(it) }, init)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

@file:Suppress("NOTHING_TO_INLINE", "unused")
package org.jetbrains.anko.support.v4

import androidx.fragment.app.Fragment
import org.jetbrains.anko.*

inline fun Fragment.dip(value: Int): Int = requireActivity().dip(value)

inline fun Fragment.dip(value: Float): Int = requireActivity().dip(value)

inline fun Fragment.sp(value: Int): Int = requireActivity().sp(value)

inline fun Fragment.sp(value: Float): Int = requireActivity().sp(value)

inline fun Fragment.px2dip(px: Int): Float = requireActivity().px2dip(px)

inline fun Fragment.px2sp(px: Int): Float = requireActivity().px2sp(px)

inline fun Fragment.dimen(resource: Int): Int = requireActivity().dimen(resource)
Loading