Skip to content

Instantly share code, notes, and snippets.

@PreyeaRegmi
PreyeaRegmi / setup
Created July 23, 2024 01:21
Android basic setup
[versions]
agp = "8.5.0"
kotlin = "1.9.0"
coreKtx = "1.10.1"
junit = "4.13.2"
junitVersion = "1.1.5"
espressoCore = "3.5.1"
lifecycleRuntimeKtx = "2.6.1"
activityCompose = "1.8.0"
composeBom = "2024.04.01"
@PreyeaRegmi
PreyeaRegmi / gist:0f65a1b3a73b82e3af16440be81091fe
Created June 14, 2024 19:57
verticalscroll-gauss-compose
fun getFactor(index: Int, draggedIndex: Int): Float {
val distance = Math.abs(index - draggedIndex).toFloat()
val sigma = 1f // Adjust the sigma for the width of the bell curve
val max = .9f
// Gaussian function
val bellCurve = exp((-distance.pow(2)) / (2 * sigma.pow(2)))
// Scale the bell curve to be in the range 0f to 0.8f
val scaledValue = bellCurve * max
@PreyeaRegmi
PreyeaRegmi / gist:c19c34b081e9a261b28d5b9cf237b9d9
Created June 14, 2024 19:56
verticalscroll-layout-compose
layout(constraints.maxWidth, totalHeight) {
var yPosition = 0
placeables.forEachIndexed { index, placeable ->
val dragVal = dragAmount.value
//Calculate the new position based on the drag value and the gaussian factor,
// where the dragged index has highest factor and decreasing factor along its adjacent elements
val newPostion = (yPosition + (dragVal * getFactor(index, draggingItemIndex)))
@PreyeaRegmi
PreyeaRegmi / gist:6cf9073178d0365b5c37cfed1cd0e088
Created June 14, 2024 19:55
verticalscroll-event-handler-compose
.pointerInput(Unit) {
detectVerticalDragGestures(
onDragStart = { offset ->
draggingItemIndex = (offset.y / itemHeight).toInt()
},
onDragEnd = {
coroutineScope.launch {
dragAmount.animateTo(0f, tween(200, easing = EaseInOut))
draggingItemIndex = -1
}
import java.util.*
import kotlin.math.pow
/**
* @author Preyea R. Regmi
* Entry point to the division operation
* 1) Takes input from CLI as string and parse into decimal format.
* 2) Converts the input and divisor into binary string
* 3) Performs division on binary representation
* 4) Prints Quotient and Remainder in both binary and decimal format
test(
"Fetch loan should not fail with any exception when mocked with success response",
() {
FetchLoanDetail fetchLoanUseCase =
FetchLoanDetail(MockedSucessRateFetchRepository(LoanDetailDTO(3)));
fetchLoanUseCase.execute(LoanParamRequest(100, 2), (data) {},
expectAsync1((error) {
expect(error, isNot(isInstanceOf<UseCaseNotImplementedException>()),
class LoanDetailResponse {
final String _emi;
LoanDetailResponse(this._emi);
static from(EMICalculator emiCalculator) {
return LoanDetailResponse(emiCalculator.calculateEMI().toString());
}
}
test(
"EMI must be 150391.64490861617 for amount = 10,000, rate = 15%, time =24 months",
() {
FetchLoanDetail fetchLoanUseCase =
FetchLoanDetail(MockedSucessRateFetchRepository(LoanDetailDTO("15")));
fetchLoanUseCase.execute(
LoanParamRequest(10000, 24), expectAsync1((data) {
expect("150391.64490861617",equals( data._emi),reason: "Emi calculation invalid");
//Domain Entity responsible for calculating EMI for given params.
class EMICalculator {
final double amount, rate, time;
EMICalculator(this.amount, this.rate, this.time){
if(amount<0)
throw ArgumentError("Amount cannot be less than zero");
if(rate<0)
throw ArgumentError("Rate cannot be less than zero");
if(time<0)
test(
"Use case must also fail when fetch loan fails from network",
() {
FetchLoanDetail fetchLoanUseCase =
FetchLoanDetail(MockedFailedRateFetchRepository(RateFetchFailType.NETWORK));
fetchLoanUseCase.execute(LoanParamRequest(100, 2), (data) {
}, expectAsync1((error) {
expect(error, isNot(isInstanceOf<RateFetchFailException>()),