Skip to content

Instantly share code, notes, and snippets.

val pair: Pair<File, Long> = measureTimeMillisPair(::readAndDecodeFile)
val file = pair.first
Log.d(TAG, "Read and decode took ${pair.last}ms")
val file: File = readAndDecodeFile()
inline fun<T> measureTimeMillisPair(function: () -> T): Pair<T, Long> {
val startTime = System.currentTimeMillis()
val result: T = function.invoke()
val endTime = System.currentTimeMillis()
return Pair(result, endTime - startTime)
}
/**
* An extension of the kotlin stdlib function [measureTimeMillis] that can measure
* the execution time fo functions that return values.
* We want this function to return just one value (the function under measurement's
* return value) but we also need to "return" the timing result somehow.
* In order to avoid returning a [Pair], and since the timing result is usually used
* in a side-effect (logging), we can provide a lambda to be executed with the timing
* result as its input.
*
* @param loggingFunction The function to execute with the timing result (typically
// 1. Use with a block of code
val executionTime = measureTimeMillis {
// block of code to be measured
}
// 2. Use with a function reference
fun doSth() {
// body of function to be measured
}
/**
* Executes the given [block] and returns elapsed time in milliseconds.
*/
public inline fun measureTimeMillis(block: () -> Unit): Long {
val start = System.currentTimeMillis()
block()
return System.currentTimeMillis() - start
}
@Singleton
class NotificationsHelper @Inject constructor(private val context: Context,
private val notificationRepository: NotificationRepository,
private val groupsRepository: GroupsRepository) {
private val notificationManager: NotificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
init {
atLeastApi(Build.VERSION_CODES.O) { createNotificationChannels() }
fusedLocationProviderClient
.getLastLocation()
.addOnSuccessListener(location -> {
// Do something with the location
}
)
public class LocationPresenter implements LocationContract.Presenter {
private static final String TAG = "LOCATION";
private final WeakReference<LocationContract.View> viewWeakReference;
private final LocationInteractor interactor;
private PermissionRequestHandler permissionRequestHandler;
private final CompositeDisposable disposables = new CompositeDisposable();
@Inject
public class LocationActivity extends AppCompatActivity
implements LocationContract.View {
@BindView(R.id.latitudeTextView)
TextView latitudeTextView;
@BindView(R.id.longitudeTextView)
TextView longitudeTextView;
@BindView(R.id.softDenyTextView)
TextView softDeniedWarningTextView;
@BindView(R.id.hardDenyTextView)