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 Pokemon( | |
| val name: String = "", | |
| val type: PokemonType = PokemonType.NORMAL, | |
| val height: Double = 0.0, | |
| val weight: Double = 0.0 | |
| ) { | |
| operator fun component1() = name | |
| operator fun component2() = type | |
| operator fun component3() = height | |
| operator fun component4() = weight |
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
| for ((key, value) in map) { | |
| ... | |
| } |
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
| import 'package:flutter/material.dart'; | |
| void main() => runApp(new HelloWorldApp()); | |
| class HelloWorldApp extends StatelessWidget { | |
| @override | |
| Widget build(BuildContext context) { | |
| return new MaterialApp( | |
| home: new Material( | |
| child: new Center( |
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 ProductsRepository( | |
| private val localRepository: LocalRepository, | |
| private val remoteRepository: RemoteRepository, | |
| private val executor: Executor) { | |
| fun getProducts(): LiveData<List<Product>> { | |
| refreshProducts() // Prepare to update the local repository | |
| return localRepository.getProducts() // But in the meantime, display content from the local repository to the user | |
| } |
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 ProductsViewModel(private val repository: ProductsRepository) : ViewModel() { | |
| /* | |
| It is important to keep the data in a variable instead of writing | |
| fun getProducts() = repository.getProducts() | |
| Because this way if the screen is rotated for example, even though the | |
| activity will have been recreated, it will use the same data from the | |
| previous instance (Because it will be using the same viewModel instance) | |
| */ | |
| private val products = repository.getProducts() |
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 ProductsActivity : AppCompatActivity() { | |
| private val viewModel by viewModel<ProductsViewModel>() | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| setContentView(R.layout.activity_products) | |
| setUpProductsListener() | |
| } |
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 xProcessor = FirebaseVision.getInstance().getVisionXDetector() | |
| val image = getVisionImageFromImagePath(imagePath) | |
| val resultTask = xProcessor.detectInImage(image) | |
| .addOnSuccessListener { // Handle results } | |
| .addOnFailureListener { // Handle exception } |
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
| // Bitmap | |
| fun getVisionImageFromImagePath(imagePath: String) = | |
| FirebaseVisionImage.fromBitmap(BitmapFactory.decodeFile(imagePath)) | |
| // Uri | |
| fun getVisionImageFromImagePath(imagePath: String) = | |
| FirebaseVisionImage.fromFilePath(context, Uri.parse("file://" + imagePath)) |
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
| abstract class MLKitApi<P, T> { | |
| protected val firebaseVisionInstance = FirebaseVision.getInstance() | |
| protected abstract val processor: P | |
| protected abstract fun detectInImage(image: String, onSuccess: (String) -> Unit, onFailure: (String) -> Unit): Task<T> | |
| protected abstract fun onDetectionSuccess(result: T): String |
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
| object MLkitApiFactory { | |
| private val apis = EnumMap<MLKitApiType, MLKitApi<*, *>>(MLKitApiType::class.java) | |
| fun get(type: MLKitApiType): MLKitApi<*, *> = when (type) { | |
| BARCODE_DETECTOR -> apis.getOrPut(BARCODE_DETECTOR, { BarcodeDetector() }) | |
| FACE_DETECTOR -> apis.getOrPut(FACE_DETECTOR, { FaceDetector() }) | |
| IMAGE_LABELER -> apis.getOrPut(IMAGE_LABELER, { ImageLabeler() }) | |
| LANDMARK_DETECTOR -> apis.getOrPut(LANDMARK_DETECTOR, { LandmarkDetector() }) | |
| TEXT_DETECTOR -> apis.getOrPut(TEXT_DETECTOR, { TextDetector() }) |