Skip to content

Instantly share code, notes, and snippets.

View Sethathi's full-sized avatar
🏗️

Sethathi Morokole Sethathi

🏗️
View GitHub Profile
# Which dispatcher should be used for which use case?
## Dispatchers
a) Dispatchers.Main
b) Dispatchers.IO
c) Dispatchers.Default
<!-- What's wrong with this layout, how can it be improve? -->
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.LinearLayoutCompat
android:id="@+id/fullnameLayout"
<!-- How could we optimise this? -->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_top"
android:text="Full Name"
// 1. What would happen if a user pressed back button before `fetchProfileDetailsFromServer` completes
// 2. How can we improve this code?
class ProfileActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_profile)
loadProfileDetails()
}
// `BookingsActivity` is rotatable and currently loads bookings from API on each rotation. How can we prevent this from happening?
class BookingViewModel(
private val repository: BookingsRepository
) : ViewModel() {
// We want to make sure that this is only settable privately.
val bookings = MutableLiveData<List<Booking>>()
fun loadBookings() {
val bookings = repository.getBookings()
// Can you spot any issues with code?
class MyBroadcastReceiverActivity : AppCompatActivity() {
private var broadcastReceiver: BroadcastReceiver? = null
override fun onCreate(@Nullable savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_one)
}
private fun registerBroadCastReceiver() {
broadcastReceiver = object : BroadcastReceiver() {
// Why won't this compile?
var name: String?
if name != nil {
displayName(name: name)
}
func displayName(name: String) {
// display name
}
// What will be displayed by this code?
blueSquare.backgroundColor = UIColor.blue
redSquare.backgroundColor = UIColor.red
let constraints = [
redSquare.topAnchor.constraint(equalTo: blueSquare.topAnchor),
blueSquare.leftAnchor.constraint(equalTo: redSquare.leftAnchor, constant: -40),
blueSquare.bottomAnchor.constraint(equalTo: redSquare.bottomAnchor),
redSquare.rightAnchor.constraint(equalTo: blueSquare.rightAnchor, constant: -40)
]
// What's the output of this code?
extension Int {
func times(callback: (_ n: Int) -> ()) {
for n in 0...self {
callback(n)
}
}
}
2.times { (n) in
// How can this conditional be simplified?
let price = 5
if price >= 1 && price <= 5 {
print("Price within range")
}