Skip to content

Instantly share code, notes, and snippets.

View rezaiyan's full-sized avatar
👋

Ali Rezaiyan rezaiyan

👋
View GitHub Profile
@rezaiyan
rezaiyan / MaxSubArray.java
Last active April 1, 2019 07:06
Maximum sum of a sub-array
//Given an integer array nums,
//find the contiguous subarray (containing at least one number)
//which has the largest sum and return its sum.
class Solution {
public int maxSubArray(int[] nums) {
if (nums.length == 1)
return nums[0];
int sum = 0;
fun factorial(number: Int): Int {
var factor = 1
return if (number <= 1)
factor
else {
(1 until number).forEach { factor*=(it+1) }
factor
}
}
@rezaiyan
rezaiyan / pleaseconnect.sh
Created February 16, 2019 09:54
It's a simple script to automate your connectivity to a ssh with OpenConnect
#!/bin/bash
echo -e "yes\n<USERNAME>\n<PASSWORD>" | sudo openconnect <HOST>
@rezaiyan
rezaiyan / Default for XWin copy.xml
Created January 15, 2019 11:43
Best keymap :))
<keymap version="1" name="Default for XWin copy" parent="Default for XWin">
<action id="$Copy">
<keyboard-shortcut first-keystroke="ctrl c" />
<keyboard-shortcut first-keystroke="ctrl insert" />
<keyboard-shortcut first-keystroke="ctrl #1000632" />
</action>
<action id="$Cut">
<keyboard-shortcut first-keystroke="ctrl x" />
<keyboard-shortcut first-keystroke="shift delete" />
<keyboard-shortcut first-keystroke="ctrl #1000637" />
@rezaiyan
rezaiyan / Binding.kt
Last active January 14, 2019 11:47
This is an inner Recyclerview sample with dataBindign
@BindingAdapter("setActorsData")
fun setAvatars(recyclerView: RecyclerView, list: ArrayList<Actor>) {
val context = recyclerView.context
val radius = context.resources.getDimension(R.dimen.list_radius)
val peek = context.resources.getDimension(R.dimen.list_peek)
val layoutManager = TurnLayoutManager(context,
TurnLayoutManager.Gravity.END,
TurnLayoutManager.Orientation.HORIZONTAL,
radius.toInt(),
peek.toInt(),
package com.weembee.sdk.base
import retrofit2.HttpException
sealed class Result<out Any> {
data class Ok<T: Any>(val value: T): Result<T>()
data class NetworkError(val code: Int): Result<Nothing>()
data class Error(val throwable: Throwable): Result<Nothing>()
}
@rezaiyan
rezaiyan / Retrofit Call coroutine extention
Last active October 28, 2018 06:58
It's an extension function to convert retrofit Call to a coroutine
suspend fun <T> Call<T>.await(): T = suspendCoroutine { continuation ->
enqueue(object : Callback<T> {
override fun onFailure(call: Call<T>, t: Throwable) {
continuation.resumeWithException(t)
}
override fun onResponse(call: Call<T>, response: Response<T>) {
if (response.isSuccessful)
continuation.resume(response.body()!!)
@rezaiyan
rezaiyan / UploadFileWithRetrofit.kt
Last active January 14, 2019 11:48
Upload file with retrofit
// Activity
val file = File(path)
val requestFile = RequestBody.create(MediaType.parse(context.contentResolver.getType(data.data)), file)
val body = MultipartBody.Part.createFormData("avatar", file.name, requestFile)
presenter.uploadUserAvatar(body)
// Activity
// Endpoint.kt
@Multipart
@POST(Const.URL.USER_AVATAR_UPDATE)
public static String formatPrice(double priceAsDouble) {
NumberFormat formatter = NumberFormat.getInstance();
if (Math.round(priceAsDouble * 100) % 100 == 0) {
formatter.setMaximumFractionDigits(0);
}
return formatter.format(priceAsDouble);
}
package com.parvazyab.android.common.adapter;
import android.util.Log;
import com.google.gson.JsonSyntaxException;
import java.io.IOException;
import java.lang.annotation.Annotation;
import okhttp3.ResponseBody;