Skip to content

Instantly share code, notes, and snippets.

View alexmaryin's full-sized avatar
🐕
Fuck the war

Alex Maryin alexmaryin

🐕
Fuck the war
View GitHub Profile
@alexmaryin
alexmaryin / cpp_sieve.cpp
Last active April 27, 2022 11:57
C++ sieve
#include <list>
#include <cassert>
#include <iostream>
#include <chrono>
std::list<int>* sieveEratosphen(const int n)
{
assert(n > 2);
bool *numbers{new bool[n]};
for (int i = 0; i < n; i++) { numbers[i] = true; }
@alexmaryin
alexmaryin / python_sieve.py
Last active April 27, 2022 13:18
Python_sieve
import time
def measured_time(func):
def wrapped(*args):
start = time.perf_counter_ns()
result = func(*args)
elapsed = int((time.perf_counter_ns() - start) / 1000000)
print(f'Elapsed time is {elapsed} ms')
return result
@alexmaryin
alexmaryin / kotlin_sieve.kt
Last active April 27, 2022 13:17
Kotlin: Sieve of Eratosthenes
import kotlin.system.measureTimeMillis
fun sieveEratosphen(n: Int): List<Int> {
require(n > 2)
val numbers = Array(n) { true }
var next = 2
while (next * next <= n) {
if (numbers[next]) {
for (index in 0 until n - next) {
val step = next * next + index * next
@alexmaryin
alexmaryin / commonRes.kt
Created October 30, 2021 08:00
Common resources in Kotlin multiplatform project
// 1. In Common-level gradle file set source dir for assets to one folder both for desktop and android modules
sourceSets {
named("commonMain") {
resources.srcDirs("resources")
// ...
}
}
android { //...
sourceSets { //...
val shakeInterpolator = TimeInterpolator { delta ->
val freq = 6f
val decay = 2f
(sin(freq * delta * Math.PI) * exp((-delta * decay).toDouble())).toFloat()
}
fun RecyclerView.addItemShaker(position: Int) {
val shaker = object : RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
// BaseAdapter -> inner class DataViewHolder -> method bind
fun bind(item: Any, clickListener: AdapterClickListenerById) {
holder.bind(binding, item, clickListener)
binding.root.animation = AnimationUtils.loadAnimation(binding.root.context, R.anim.recycler_item_arrive)
}
// Animation file in resources
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
// On ViewModel we need to return position to scroll. Good practice is delegate it to responsible class:
fun getScrollPosition(items: List<Any>) = currentScreen.getPositionToScroll(items)
// On Fragment we need to observe event of scroll triggering:
viewModel.getScrollTrigger().collectOnFragment(this) { state ->
when (state) {
false -> {} // Other actions if scrolling is not available, i.e. show a SnackBar
true -> {
viewModel.getScrollPosition(referenceList)?.let { (position, snackText) ->
binding.recyclerView.addItemShaker(position)
@alexmaryin
alexmaryin / events_on_ashley.kt
Last active June 18, 2021 08:26
Implementing events system in Libgdx/Ashley
// Game Events
sealed class GameEvent
class GameOver(val score: Int) : GameEvent()
class CopCatchEnemy(val cop: Entity, val enemy: Entity) : GameEvent()
class PlayerRestoresCop(val cop: Entity) : GameEvent()
object EnemyMissed : GameEvent()
object EnemyCaught : GameEvent()
object CopMissed : GameEvent()
@alexmaryin
alexmaryin / medium1_add-callback.kt
Created June 3, 2021 07:06
medium1 adding callback to dispatcher
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
// ...
requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner, backPressHandler)
// ...
}
@alexmaryin
alexmaryin / medium1_backpressed.kt
Created June 3, 2021 06:53
medium1_backpressed
private var backPressedTime: Long = 0
private val backPressHandler = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
if (mainViewModel.currentScreen is MainScreen) {
if (backPressedTime + 2000 > System.currentTimeMillis()) {
activity?.finishAndRemoveTask()
} else {
Toast.makeText(requireContext(), "Press again to exit", Toast.LENGTH_SHORT).show()
backPressedTime = System.currentTimeMillis()
}