Last active
June 15, 2017 07:47
-
-
Save namhyun-gu/0861a763cdb94780ee558801a000287f to your computer and use it in GitHub Desktop.
Refactoring Playground/MainFragment.kt
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
package com.namhyun.playground | |
import android.content.Intent | |
import android.os.Bundle | |
import android.preference.Preference | |
import android.preference.PreferenceFragment | |
import android.preference.PreferenceScreen | |
import com.namhyun.playground.camera.BasicActivity | |
import com.namhyun.playground.firebase.DatabaseActivity | |
import com.namhyun.playground.location.LocationActivity | |
import com.namhyun.playground.location.LocationUpdatesActivity | |
import com.namhyun.playground.ui.CircularImageActivity | |
import com.namhyun.playground.ui.LottieActivity | |
import kotlin.reflect.KClass | |
class MainFragment : PreferenceFragment() { | |
private lateinit val bindPreferenceMap: HashMap<String, Any> | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
addPreferencesFromResource(R.xml.pref_main_menu) | |
// if not working getString in property | |
bindPreferenceMap = hashMapOf( | |
getString(R.string.pref_ui_circularimageview_key) to CircularImageActivity::class | |
getString(R.string.pref_ui_lottie_key) to LottieActivity::class | |
getString(R.string.pref_location_location_key) to LocationActivity::class | |
getString(R.string.pref_location_locationupdates_key) to LocationUpdatesActivity::class | |
getString(R.string.pref_camera_basic_key) to BasicActivity::class | |
getString(R.string.pref_firebase_database_key) to DatabaseActivity::class | |
) | |
} | |
override fun onPreferenceTreeClick(preferenceScreen: PreferenceScreen?, preference: Preference?): Boolean { | |
if (bindPreferenceMap.containsKey(preference?.key)) { | |
when (preference?.key) { | |
is KClass<*> -> value.java | |
is Class<*> -> value | |
else -> null | |
}.let { startActivity(Intent(activity, it)) } | |
} | |
return super.onPreferenceTreeClick(preferenceScreen, preference) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment