Skip to content

Instantly share code, notes, and snippets.

View husaynhakeem's full-sized avatar

Husayn Hakeem husaynhakeem

View GitHub Profile
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
for ((key, value) in map) {
...
}
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(
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
}
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()
class ProductsActivity : AppCompatActivity() {
private val viewModel by viewModel<ProductsViewModel>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_products)
setUpProductsListener()
}
val xProcessor = FirebaseVision.getInstance().getVisionXDetector()
val image = getVisionImageFromImagePath(imagePath)
val resultTask = xProcessor.detectInImage(image)
.addOnSuccessListener { // Handle results }
.addOnFailureListener { // Handle exception }
// Bitmap
fun getVisionImageFromImagePath(imagePath: String) =
FirebaseVisionImage.fromBitmap(BitmapFactory.decodeFile(imagePath))
// Uri
fun getVisionImageFromImagePath(imagePath: String) =
FirebaseVisionImage.fromFilePath(context, Uri.parse("file://" + imagePath))
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
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() })