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 fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
... | |
val binding = MainLayoutBinding.inflate(layoutInflater) | |
setup(binding) | |
} | |
// write binding once and forget ✅ | |
fun setup(binding:MainLayoutBinding) = with(binding) { | |
textName.text = "ch8n" |
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
val binding = MainLayoutBinding.inflate(layoutInflater) | |
with(binding) { | |
textName.text = "ch8n" | |
textTwitter.text = "twitter@ch8n2" | |
}) |
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
val userName: String | |
get() = preferenceManager.getInstance()?.getName() ?: getString(R.string.stranger) |
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
val userName: String | |
get() { | |
preferenceManager.getInstance()?.run { | |
getName() | |
} ?: run { | |
getString(R.string.stranger) | |
} | |
} | |
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
val intent = Intent(context, MyActivity::class.java).apply { | |
// scope to configure object | |
putExtra("data", 123) | |
addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) | |
}) | |
startActivity(this@FooActivity,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
Intent(context, MyActivity::class.java).apply { | |
// scope to configure object | |
putExtra("data", 123) | |
addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) | |
}).also { intent -> | |
// scope to consume the object | |
startActivity(this@FooActivity,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
class Foo { | |
var imageFile : File ? = ... | |
fun deleteImage(){ | |
// 🧐 you can get away for now | |
imageFile?.let { image -> | |
if(image.exists()) image.delete() | |
} | |
} | |
} |
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 Foo { | |
var imageFile : File ? = ... | |
fun deleteImage(){ | |
val imageFile = this.imageFile | |
if(imageFile != null) { | |
if(imageFile.exists()) imageFile.delete() | |
} | |
} | |
} |
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 deleteImage(){ | |
val imageFile : File ? = ... | |
if(imageFile != null) { | |
// 👇 smart casted imageFile into non nullable | |
if(imageFile.exists()) imageFile.delete() | |
} | |
} |
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 deleteImage(){ | |
var imageFile : File ? = ... | |
imageFile?.let { // ❌ don't write code like this | |
if(it.exists()) it.delete() | |
} | |
} |