Skip to content

Instantly share code, notes, and snippets.

public class MultipartHelper {
private final String fileDir;
private final String outputDir;
public MultipartHelper(String fileDir, String outputDir) {
this.fileDir = fileDir;
String baseDir = BaseApplication.getAppContext().getObbDir() + File.separator + "uploads";
this.outputDir = baseDir + File.separator;
File upload = new File(baseDir);
@rezaiyan
rezaiyan / StaggeredGridLayoutManager.java
Created September 29, 2020 09:24
To have a full width item in the StaggeredGridLayoutManager
ViewGroup.LayoutParams layoutParams = itemView.getLayoutParams();
if (layoutParams instanceof StaggeredGridLayoutManager.LayoutParams) {
if (getAdapterPosition() == 0) {
((StaggeredGridLayoutManager.LayoutParams) layoutParams).setFullSpan(true);
} else {
((StaggeredGridLayoutManager.LayoutParams) layoutParams).setFullSpan(false);
}
}
@rezaiyan
rezaiyan / Degree.java
Created July 4, 2020 20:37
Array degree algo
public static String codeHere(String inputData) {
// Use this function to write your solution;
String[] arrayInfo = inputData.split("\n");
int size = Integer.parseInt(arrayInfo[0]);
int[] lenghsArray = new int[size];
String[] stringArray = arrayInfo[1].split(" ");
List<Integer> subArray = new ArrayList<>();
@rezaiyan
rezaiyan / Projection.java
Last active April 19, 2020 04:53
EPSG 4326 , EPSG 3857 converter
private static double EARTH_RADIUS = 6378137.0;
public static LatLng to4326(Wgs84 wgs84) {
double lat = Math.toDegrees(Math.atan(Math.exp(wgs84.getY() / EARTH_RADIUS)) * 2 - Math.PI/2);
double lng = Math.toDegrees(wgs84.getX() / EARTH_RADIUS);
return new LatLng(lat, lng);
}
public static Wgs84 toWgs84(LatLng latLng) {
double x = Math.toRadians(latLng.longitude) * EARTH_RADIUS;
@rezaiyan
rezaiyan / SharedViewModel.java
Last active November 11, 2019 08:01
This is a simple example of a shared viewmodel
class SharedViewModel extends ViewModel {
public MutableLiveData<String> liveData = new MutableLiveData<>();
}
class parentFragment {
sharedVm = new ViewModelProvider(requireActivity()).get(SharedViewModel.class);
@rezaiyan
rezaiyan / BObserver.java
Last active October 14, 2019 16:28
Binding Observer with LiveData
public class BObserver<T> extends DisposableObserver<T> {
private StateLiveData<T> liveData;
public BObserver(StateLiveData<T> liveData) {
this.liveData = liveData;
}
@Override
@rezaiyan
rezaiyan / OnTouchListener.java
Created June 11, 2019 08:07
This is a sample to swipe a layout to left and right
import android.view.MotionEvent;
import android.view.View;
/**
* @author ali (alirezaiyann@gmail.com)
* @since 6/11/19 12:24 PM.
*/
public class OnTouchListener implements View.OnTouchListener {
import android.annotation.SuppressLint
import android.view.MotionEvent
import android.view.View
import android.view.View.OnTouchListener
import java.util.*
/**
* Detects left and right swipes across a view.
*/
@rezaiyan
rezaiyan / PrettyLogger.kt
Created April 26, 2019 16:16
Pretty Json Logger
//Reference//
//https://github.com/orhanobut/logger
//https://github.com/JakeWharton/timber
// 1- Add this initializations to the oncreate method of Application class
Logger.addLogAdapter(AndroidLogAdapter())
Timber.plant(Timber.DebugTree())
// 2- Create a snippet to detect log is a json format (I used a string extention function)
fun String.isJson(): Boolean {
@rezaiyan
rezaiyan / build.gradle
Created April 24, 2019 11:42
Androidx conflict
/* packagingOptions {
exclude 'META-INF/androidx.localbroadcastmanager_localbroadcastmanager.version'
exclude 'META-INF/androidx.appcompat_appcompat.version'
exclude 'META-INF/androidx.customview_customview.version'
exclude 'META-INF/androidx.legacy_legacy-support-core-ui.version'
exclude 'META-INF/androidx.drawerlayout_drawerlayout.version'
exclude 'META-INF/androidx.interpolator_interpolator.version'
exclude 'META-INF/androidx.legacy_legacy-support-core-utils.version'
exclude 'META-INF/androidx.slidingpanelayout_slidingpanelayout.version'
exclude 'META-INF/androidx.swiperefreshlayout_swiperefreshlayout.version'