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 main(args: Array<String>) { | |
val newDevice = SmartPhone.Builder() | |
.manufacture("Foxconn") | |
.model("Pixel 3") | |
.screenSize("1080 x 2160") | |
.storage("128Gb") | |
.build() | |
} |
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
import java.util.* | |
import kotlin.properties.Delegates | |
interface ValueChangedListener { | |
fun onValueChanged(newValue: String) | |
} | |
class PrintingTextChangedListener: ValueChangedListener { | |
override fun onValueChanged(newValue: String) { | |
println("Value of the text is: $newValue") |
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
var text: String by Delegates.observable( | |
initialValue = "", | |
onChange = { | |
property, oldValue, newValue -> | |
listener.onValueChanged(newValue) | |
} | |
) |
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 MyFragment : Fragment() { | |
private lateinit var viewModel: MyViewModel | |
override fun onActivityCreated(savedInstanceState: Bundle?) { | |
super.onActivityCreated(savedInstanceState) | |
viewModel = ViewModelProviders.of(this).get(MyViewModel::class.java) | |
} | |
} |
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 MyViewModel : ViewModel() { | |
// Stored cached bitmap. | |
private var cachedBitmap: Bitmap? = null | |
// Retrieves the image. | |
fun getImage(): Bitmap { | |
// If the image is not already cached, download it and cache it. | |
if (cachedBitmap == null) { | |
cachedBitmap = downloadImage() |
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 MainFragment : Fragment() { | |
private lateinit var viewModel: MainViewModel | |
override fun onActivityCreated(savedInstanceState: Bundle?) { | |
super.onActivityCreated(savedInstanceState) | |
viewModel = ViewModelProviders.of(this).get(MainViewModel::class.java) | |
imageView.setImageBitmap(viewModel.getImage()) | |
} | |
} |
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 MyViewModel : ViewModel() { | |
val imageLiveData = MutableLiveData<Bitmap>() | |
fun getImage() { | |
// ... | |
} | |
} |
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 MyFragment : Fragment() { | |
private lateinit var viewModel: MainViewModel | |
override fun onActivityCreated(savedInstanceState: Bundle?) { | |
super.onActivityCreated(savedInstanceState) | |
viewModel = ViewModelProviders.of(this).get(MainViewModel::class.java) | |
// We observe the bitmap emitted from `imageLiveData` and | |
// display it in the `ImageView`. | |
viewModel.imageLiveData.observe(this, Observer { | |
imageView.setImageBitmap(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
class MyViewModel : ViewModel() { | |
// Some image downloader class. | |
val imageDownloader = ImageDownloader() | |
val imageLiveData = MutableLiveData<Bitmap>() | |
fun getImage() { | |
// If we don't have a value already pushed to the live data | |
// and the download is not in progress, start a new one. | |
if (imageLiveData.value == null && !imageDownloader.isDownloading) { | |
// This is just some sample API that uses RxJava, but |
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 ViewModel: ViewModel() { | |
fun fetchDocs() { | |
get("developer.android.com") { result -> | |
show(result) | |
} | |
} | |
} |