Last active
November 9, 2018 05:26
-
-
Save creativedrewy/679bb311b19142af271531ebfcb838dd to your computer and use it in GitHub Desktop.
Kotlin, Practically Speaking - https://creativedrewy.github.io/KotlinPracticallySpeaking/
This file contains hidden or 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 <A> chain(vararg funcs: (A) -> Unit): (A) -> Unit { | |
| return fun(a: A) { | |
| funcs.forEach { it(a) } | |
| } | |
| } | |
| fun logThenDoSomething() { | |
| chain({ Log.v("TAG", "Log this first, yo!")}, ::functionInMyClass) | |
| } |
This file contains hidden or 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
| public class GroupEditActivity extends AppCompatActivity { | |
| public static final String GROUP_ID_EXTRA = "GROUP_ID_EXTRA"; | |
| public static Intent createIntentForActivity(Context ctx, int id) { | |
| Intent intent = new Intent(ctx, GroupEditActivity.class); | |
| intent.putExtra(GROUP_ID_EXTRA, id); | |
| return intent; | |
| } | |
| @Override | |
| public void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.group_details); | |
| } | |
| } |
This file contains hidden or 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 GroupEditActivity : AppCompatActivity() { | |
| public override fun onCreate(savedInstanceState: Bundle?) { ... } | |
| companion object { | |
| val GROUP_ID_EXTRA = "GROUP_ID_EXTRA" | |
| fun createIntentForActivity(ctx: Context, id: Int): Intent { | |
| val intent = Intent(ctx, GroupEditActivity::class.java) | |
| intent.putExtra(GROUP_ID_EXTRA, id) | |
| return intent | |
| } | |
| } | |
| } |
This file contains hidden or 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
| public class MyJavaActivity extends AppCompatActivity { | |
| @Override | |
| protected void onCreate(@Nullable Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| GroupEditActivity.Companion | |
| .createIntentForActivity(this, GroupEditActivity.Companion.getGROUP_ID_EXTRA(), 2); | |
| } | |
| } |
This file contains hidden or 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
| companion object { | |
| const val GROUP_ID_EXTRA = "GROUP_ID_EXTRA" | |
| @JvmStatic | |
| fun createIntentForActivity(ctx: Context, extraFlag: String, id: Int): Intent { | |
| val intent = Intent(ctx, GroupEditActivity::class.java) | |
| intent.putExtra(extraFlag, id) | |
| return intent | |
| } | |
| } |
This file contains hidden or 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
| @Override | |
| protected void onCreate(@Nullable Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| GroupEditActivity.createIntentForActivity(this, GroupEditActivity.GROUP_ID_EXTRA, 2); | |
| } |
This file contains hidden or 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
| public class ConvertMeActivity extends AppCompatActivity { | |
| private Button myButton; | |
| @Inject protected MyViewModelFactory viewModelFactory; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_convert_me); | |
| myButton = findViewById(R.id.my_button); | |
| myButton.setOnClickListener(new View.OnClickListener() { | |
| @Override | |
| public void onClick(View v) { | |
| Snackbar.make(v, "hi!", Snackbar.LENGTH_SHORT).show(); | |
| } | |
| }); | |
| MyViewModel myViewModel = ViewModelProviders.of(this, viewModelFactory).get(MyViewModel.class); | |
| Boolean someBool = myViewModel.getHasListBeenModified(); | |
| } | |
| private BottomNavigationView.OnNavigationItemSelectedListener listener = new BottomNavigationView.OnNavigationItemSelectedListener() { | |
| @Override | |
| public boolean onNavigationItemSelected(@NonNull MenuItem item) { | |
| switch (item.getItemId()) { | |
| case R.id.navigation_home: | |
| myButton.setText(R.string.title_home); | |
| return true; | |
| case R.id.navigation_dashboard: | |
| myButton.setText(R.string.title_dashboard); | |
| return true; | |
| } | |
| return false; | |
| } | |
| }; | |
| } |
This file contains hidden or 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 ConvertMeActivity : AppCompatActivity() { | |
| private lateinit var myButton: Button | |
| @Inject internal lateinit var viewModelFactory: MyViewModelFactory | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| setContentView(R.layout.activity_convert_me) | |
| myButton = findViewById(R.id.my_button) | |
| myButton.setOnClickListener { Snackbar.make(it, "hi!", Snackbar.LENGTH_SHORT).show() } | |
| val myViewModel = ViewModelProviders.of(this, viewModelFactory).get(MyViewModelFactory::class.java) | |
| val someBool = myViewModel.hasListBeenModified | |
| } | |
| private val listener = BottomNavigationView.OnNavigationItemSelectedListener { | |
| return@OnNavigationItemSelectedListener when (it.itemId) { | |
| R.id.navigation_home -> { | |
| myButton.setText(R.string.title_home) | |
| true | |
| } | |
| R.id.navigation_dashboard -> { | |
| myButton.setText(R.string.title_dashboard) | |
| true | |
| } | |
| else -> false | |
| } | |
| } | |
| } |
This file contains hidden or 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 ConvertMeActivity : AppCompatActivity() { | |
| private var myButton: Button? = null | |
| @Inject protected val viewModelFactory: MyViewModelFactory? = null | |
| override fun onCreate(savedInstanceState: Bundle) { | |
| super.onCreate(savedInstanceState) | |
| setContentView(R.layout.activity_convert_me) | |
| myButton = findViewById(R.id.my_button) | |
| myButton!!.setOnClickListener { v -> Snackbar.make(v, "hi!", Snackbar.LENGTH_SHORT).show() } | |
| val myViewModel = ViewModelProviders.of(this, viewModelFactory).get(MyViewModel::class.java) | |
| val someBool = myViewModel.hasListBeenModified | |
| } | |
| private val listener = BottomNavigationView.OnNavigationItemSelectedListener { item -> | |
| when (item.itemId) { | |
| R.id.navigation_home -> { | |
| myButton!!.setText(R.string.title_home) | |
| return@OnNavigationItemSelectedListener true | |
| } | |
| R.id.navigation_dashboard -> { | |
| myButton!!.setText(R.string.title_dashboard) | |
| return@OnNavigationItemSelectedListener true | |
| } | |
| } | |
| false | |
| } | |
| } |
This file contains hidden or 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.yammer.droid.convenience | |
| //This file is CalculatorFunctions.kt | |
| fun addFiveToNumber(input: Int): Int { | |
| return 5 + input | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment