Skip to content

Instantly share code, notes, and snippets.

@wispborne
Created May 29, 2016 17:24
Show Gist options
  • Save wispborne/1ec946faeebd73924fe5855217fc8697 to your computer and use it in GitHub Desktop.
Save wispborne/1ec946faeebd73924fe5855217fc8697 to your computer and use it in GitHub Desktop.
package com.thunderclouddev.changelogs.ui
import android.content.Context
import android.os.Bundle
import android.support.annotation.LayoutRes
import android.support.annotation.StyleRes
import android.support.v7.app.AppCompatActivity
import com.thunderclouddev.changelogs.BaseApplication
import com.thunderclouddev.changelogs.R
import com.thunderclouddev.changelogs.configuration.LanguagePreference
import com.thunderclouddev.changelogs.configuration.Preferences
import com.thunderclouddev.changelogs.configuration.ThemePreference
import com.thunderclouddev.changelogs.event.Bus
import com.thunderclouddev.changelogs.event.PreferenceChangedEvent
import com.thunderclouddev.changelogs.utils.EventBusUtils
import com.thunderclouddev.changelogs.utils.LoggingUtils
import com.thunderclouddev.changelogs.utils.empty
import org.greenrobot.eventbus.Subscribe
import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper
import java.util.*
import javax.inject.Inject
/**
* Base activity.
*/
abstract class BaseActivity : AppCompatActivity() {
@Inject protected lateinit open var preferences: Preferences
@Inject protected lateinit open var bus: Bus
override fun onCreate(savedInstanceState: Bundle?) {
BaseApplication.getInjector().inject(this)
val customTheme = getCustomTheme()
setTheme(customTheme)
applyCustomLocale()
super.onCreate(savedInstanceState)
setContentView(getLayoutResourceId())
bindViews()
EventBusUtils.register(bus, this)
}
override fun onDestroy() {
EventBusUtils.unregister(bus, this)
super.onDestroy()
}
protected fun bindViews() {
//ButterKnife.inject(this)
}
override fun attachBaseContext(newBase: Context) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase))
}
@Subscribe
open fun onPreferenceChangedEvent(preferenceChangedEvent: PreferenceChangedEvent) {
// For theme or config changes, recreate the activity
if (preferenceChangedEvent.preference is ThemePreference) {
recreate()
} else if (preferenceChangedEvent.preference is LanguagePreference) {
LoggingUtils.putString(LoggingUtils.Keys.STRING_LOCALE,
preferences.languagePreference.locale)
recreate()
}
}
@LayoutRes
protected abstract fun getLayoutResourceId(): Int
/**
* Sets a custom theme for the activity.
*/
@StyleRes
open protected fun getCustomTheme(): Int {
return when (preferences.themePreference) {
ThemePreference.THEME_LIGHT -> R.style.AppTheme_Light
ThemePreference.THEME_DARK -> R.style.AppTheme_Dark
ThemePreference.THEME_BLACK -> R.style.AppTheme_Black
}
}
private fun applyCustomLocale() {
val chosenLocale = preferences.languagePreference.locale
if (!chosenLocale.isNullOrBlank() && !chosenLocale.equals(getString(R.string.prefKey_language_default))) {
val splitLocale = chosenLocale.split("-")
val language = splitLocale.getOrNull(0)
val country = splitLocale.getOrNull(1)
val variant = splitLocale.getOrNull(2)
val locale = Locale(language, country ?: String.empty, variant ?: String.empty)
Locale.setDefault(locale);
val resources = resources;
val configuration = resources.configuration;
configuration.locale = locale;
resources.updateConfiguration(configuration, resources.displayMetrics);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment