Skip to content

Instantly share code, notes, and snippets.

@Ribesg

Ribesg/Anko.kt Secret

Created January 8, 2016 10:13
Show Gist options
  • Save Ribesg/d8d457b1d30f91894ba9 to your computer and use it in GitHub Desktop.
Save Ribesg/d8d457b1d30f91894ba9 to your computer and use it in GitHub Desktop.
@file:Suppress("NOTHING_TO_INLINE")
package com.penguinsuplex.android.knap.ext
import android.content.Context
import android.graphics.Point
import android.support.design.widget.TabLayout
import android.support.v4.app.Fragment
import android.util.TypedValue
import android.view.*
import com.mopub.mobileads.MoPubView
import com.penguinsuplex.android.knap.ext.android.NoSwipeViewPager
import org.jetbrains.anko.*
import org.jetbrains.anko.custom.ankoView
/**
* @author Ribesg
*/
// AdView
/**
* Creates and attaches a [MoPubView].
*/
inline fun ViewManager.adView() = adView({})
/**
* Creates and attaches a [MoPubView], executing the provided initialization function.
*/
inline fun ViewManager.adView(init: MoPubView.() -> Unit): MoPubView {
return ankoView({ MoPubView(it) }, init)
}
// Context
val Context.appCtx: Context
get() = applicationContext
val Fragment.appCtx: Context
get() = context.applicationContext
val View.appCtx: Context
get() = context.applicationContext
// NoSwipeViewPager
/**
* Creates and attaches a [NoSwipeViewPager].
*/
inline fun ViewManager.noSwipeViewPager() = noSwipeViewPager({})
/**
* Creates and attaches a [NoSwipeViewPager], executing the provided initialization function.
*/
inline fun ViewManager.noSwipeViewPager(init: NoSwipeViewPager.() -> Unit): NoSwipeViewPager {
return ankoView({ NoSwipeViewPager(it) }, init)
}
// Sizes
/**
* Gets a [Point] object holder the device's screen height and width.
*/
inline fun Context.screenSize(): Point {
val windowManager = getSystemService(Context.WINDOW_SERVICE) as WindowManager
val size = Point()
windowManager.defaultDisplay.getSize(size)
return size
}
/**
* Gets the device's screen width.
*/
inline fun Context.screenWidth(): Int = screenSize().x
/**
* Gets the device's screen height.
*/
inline fun Context.screenHeight(): Int = screenSize().y
/**
* Gets the device's status bar height.
*/
inline fun Context.statusBarHeight(): Int {
var res = 0
val resId = resources.getIdentifier("status_bar_height", "dimen", "android")
if (resId > 0) {
res = resources.getDimensionPixelSize(resId)
}
return res
}
/**
* Gets the height of a rectangle with the screen width as width and an
* aspect ratio of 16:9.
*/
inline fun Context.ratioHeight() = (screenWidth() * 9f / 16f).toInt()
/**
* Gets the height of a rectangle with the provided width and an aspect ratio
* of 16:9.
*/
inline fun Context.ratioHeight(width: Int) = (width * 9f / 16f).toInt()
/**
* Gets the device's screen height minus the status bar height.
*/
inline fun Context.contentHeight(): Int = screenHeight() - statusBarHeight()
/**
* Gets the ActionBar height.
*/
inline fun Context.actionBarHeight(): Int {
val tv = TypedValue()
theme.resolveAttribute(android.support.v7.appcompat.R.attr.actionBarSize, tv, true)
return resources.getDimensionPixelSize(tv.resourceId)
}
// Tab
/**
* Creates and attaches a [TabLayout.Tab] to a [TabLayout].
*/
inline fun TabLayout.tab() = tab({})
/**
* Creates and attaches a [TabLayout.Tab] to a [TabLayout], executing the provided initialization function.
*/
inline fun TabLayout.tab(init: TabLayout.Tab.() -> Unit): TabLayout.Tab {
newTab().let { tab ->
addTab(tab)
tab.init()
return tab
}
}
/**
* Creates and attaches a [TabLayout.Tab] to a [TabLayout].
*/
inline fun TabLayout.tab(txt: String) = tab(txt, {})
/**
* Creates and attaches a [TabLayout.Tab] to a [TabLayout], executing the provided initialization function.
*/
inline fun TabLayout.tab(txt: String, init: TabLayout.Tab.() -> Unit): TabLayout.Tab {
newTab().let { tab ->
addTab(tab)
tab.setText(txt)
tab.init()
return tab
}
}
/**
* Creates and attaches a [TabLayout.Tab] to a [TabLayout].
*/
inline fun TabLayout.tab(id: Int) = tab(id, {})
/**
* Creates and attaches a [TabLayout.Tab] to a [TabLayout], executing the provided initialization function.
*/
inline fun TabLayout.tab(id: Int, init: TabLayout.Tab.() -> Unit): TabLayout.Tab {
newTab().let { tab ->
addTab(tab)
tab.setText(id)
tab.init()
return tab
}
}
// Wait
/**
* Executes the provided task after the provided time.
*/
inline fun Context.schedule(millis: Long, crossinline task: Context.() -> Unit) {
async() {
Thread.sleep(millis)
task()
}
}
/**
* Executes the provided task on the UI thread after the provided time.
*/
inline fun Context.scheduleUi(millis: Long, crossinline task: Context.() -> Unit) {
schedule(millis) {
onUiThread {
task()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment