This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
NetworkConnectLifecycle(this, lifecycle).run { | |
networkChange = { handleNetworkChange(it) } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun Context.isOnline(): Boolean { | |
val connectManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | |
val activeNetwork = connectManager.activeNetwork ?: return false | |
val activeNetworkCapabilities = | |
connectManager.getNetworkCapabilities(activeNetwork) ?: return false | |
return when { | |
activeNetworkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true | |
activeNetworkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true | |
activeNetworkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> true | |
activeNetworkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_BLUETOOTH) -> true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NetworkConnectLifecycle(private val context: Context, private val lifecycle: Lifecycle) : | |
LifecycleObserver, | |
ConnectivityManager.NetworkCallback() { | |
private val mConnectActivityManager: ConnectivityManager = | |
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | |
var networkChange: ((Boolean) -> Unit)? = null | |
init { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<resources> | |
<!-- Base application theme. --> | |
<style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar"> | |
<!-- Customize your theme here. --> | |
<item name="colorPrimary">@color/colorPrimary</item> | |
<item name="colorPrimaryDark">@color/colorPrimaryDark</item> | |
<item name="colorAccent">@color/colorAccent</item> | |
</style> | |
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<color name="colorPrimary">#463B5C</color> | |
<color name="colorPrimaryDark">#463B5C</color> | |
<color name="colorAccent">#463B5C</color> | |
//Define color for text primary and divider (change color in color value-night) | |
<color name="textColorPrimary">#111111</color> | |
<color name="colorDivider">#E4E4E4</color> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<color name="textColorPrimary">#ffffff</color> | |
<color name="colorDivider">#101010</color> | |
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by nambv on 8/20/2020 | |
*/ | |
object PreferenceUtils { | |
private const val PREFERENCE_NAME = "com.android.dark_theme_preferences" | |
const val PREFERENCE_KEY_THEME = "key_theme" | |
const val THEME_LIGHT = 0 | |
const val THEME_DARK = 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by nambv on 10/5/2020 | |
*/ | |
class ThemeApplication : Application() { | |
override fun onCreate() { | |
super.onCreate() | |
setupTheme() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PreferenceObserverLifecycle(private val context: Context, private val lifecycle: Lifecycle) : | |
LifecycleObserver, SharedPreferences.OnSharedPreferenceChangeListener { | |
private val mPreference = PreferenceUtils.getAppPrefs(context) | |
var themeConfigChange: (() -> Unit)? = null | |
init { | |
lifecycle.addObserver(this) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by nambv on 10/5/2020 | |
*/ | |
abstract class BaseThemeActivity : AppCompatActivity() { | |
@get:LayoutRes | |
abstract val layoutId: Int | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) |
OlderNewer