Skip to content

Instantly share code, notes, and snippets.

@MartuPuri
MartuPuri / ViewExtension.kt
Created October 12, 2017 20:13
Kotlin Extension - View
fun View.showKeyboard(context: Context) {
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.showSoftInput(this, InputMethodManager.SHOW_FORCED)
}
fun View.hideKeyboard(context: Context) {
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(this.windowToken, 0)
}
@MartuPuri
MartuPuri / StringExtension.kt
Created October 12, 2017 20:12
Kotlin Extension - String
fun String.Companion.emptyString(): String {
return ""
}
fun String.Companion.formatDate(dateFormat: String, date: String): Date {
val simpleDateFormat = SimpleDateFormat(dateFormat)
return simpleDateFormat.parse(date)
}
@MartuPuri
MartuPuri / FragmentExtension.kt
Created October 12, 2017 20:08
Kotlin Extension - Fragment
fun Fragment.slideNextFragment(fragment: Fragment,
@IdRes fragmentContentId: Int,
@AnimRes enter: Int? = null,
@AnimRes exit: Int? = null,
@AnimRes popEnter: Int? = null,
@AnimRes popExit: Int? = null,
vararg sharedElements: Pair<View, String>) {
if (isAdded) {
val fragmentTransaction = fragmentManager.beginTransaction()
enter?.let { exit?.let { it1 -> popEnter?.let { it2 -> popExit?.let { it3 -> fragmentTransaction.setCustomAnimations(it, it1, it2, it3) } } } }
@MartuPuri
MartuPuri / FragmentActivityExtension.kt
Created October 12, 2017 20:07
Kotlin Extension - FragmentActivity
fun FragmentActivity.initialFragment(fragment: Fragment,
@IdRes fragmentContentId: Int) {
val fragmentManager = supportFragmentManager
val fragmentTransaction = fragmentManager.beginTransaction()
fragmentTransaction.add(fragmentContentId, fragment).commit()
}
@MartuPuri
MartuPuri / ContextExtension.kt
Created October 12, 2017 20:06
Kotlin Extension - Context
@ColorInt
fun Context.getColor(@ColorRes colorRes: Int): Int {
val color: Int
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
color = resources.getColor(colorRes, theme)
} else {
color = resources.getColor(colorRes)
}
return color
}
@MartuPuri
MartuPuri / BaseProjectApplication.kt
Created October 12, 2017 20:01
Base Android Project - Application
class BaseProjectApplication : Application() {
override fun onCreate() {
super.onCreate()
Fresco.initialize(this);
Injector.initializeApplicationComponent(this)
}
}
@MartuPuri
MartuPuri / BaseFragment.kt
Created October 12, 2017 19:54
Base Android Project - BaseFragment
abstract class BaseFragment : Fragment() {
private var unbinder: Unbinder? = null
@get:LayoutRes protected abstract val layout: Int
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater?.inflate(layout, container, false)
}
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
@MartuPuri
MartuPuri / BaseFragmentActivity.kt
Created October 12, 2017 19:50
Base Android Project - BaseFragmentActivity
abstract class BaseFragmentActivity : AppCompatActivity() {
private var unbinder: Unbinder? = null
@get:LayoutRes protected abstract val contentView: Int
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(contentView)
unbinder = ButterKnife.bind(this)
@MartuPuri
MartuPuri / Injector.kt
Created October 12, 2017 19:45
Base Android Project - Injector
object Injector {
var serviceComponent: ServiceComponent? = null
fun initializeApplicationComponent(application: BaseProjectApplication) {
serviceComponent = DaggerServiceComponent.builder()
.appModule(AppModule(application))
.netModule(NetModule())
.build()
@MartuPuri
MartuPuri / Injector.kt
Created October 12, 2017 19:44
Base Android Project - Injector
object Injector {
var serviceComponent: ServiceComponent? = null
fun initializeApplicationComponent(application: BaseProjectApplication) {
serviceComponent = DaggerServiceComponent.builder()
.appModule(AppModule(application))
.preferencesModule(PreferencesModule())
.netModule(NetModule())
.build()