1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-22 06:37:30 +00:00

convert some utils to kotlin

This commit is contained in:
Austin Huang 2021-06-19 11:21:47 -04:00
parent 138a938423
commit 7e45716e61
No known key found for this signature in database
GPG Key ID: 84C23AA04587A91F
4 changed files with 120 additions and 160 deletions

View File

@ -1,45 +1,38 @@
package awais.instagrabber.utils;
package awais.instagrabber.utils
import java.util.Arrays;
import java.util.*
public class CubicInterpolation {
private final float[] array;
private final int tangentFactor;
private final int length;
public CubicInterpolation(final float[] array, final int cubicTension) {
this.array = Arrays.copyOf(array, array.length);
this.length = array.length;
tangentFactor = 1 - Math.max(0, Math.min(1, cubicTension));
class CubicInterpolation @JvmOverloads constructor(array: FloatArray, cubicTension: Int = 0) {
private val array: FloatArray
private val tangentFactor: Int
private val length: Int
private fun getTangent(k: Int): Float {
return tangentFactor * (getClippedInput(k + 1) - getClippedInput(k - 1)) / 2
}
public CubicInterpolation(final float[] array) {
this(array, 0);
fun interpolate(t: Float): Float {
val k = Math.floor(t.toDouble()).toInt()
val m = floatArrayOf(getTangent(k), getTangent(k + 1))
val p = floatArrayOf(getClippedInput(k), getClippedInput(k + 1))
val t1 = t - k
val t2 = t1 * t1
val t3 = t1 * t2
return (2 * t3 - 3 * t2 + 1) * p[0] + (t3 - 2 * t2 + t1) * m[0] + (-2 * t3 + 3 * t2) * p[1] + (t3 - t2) * m[1]
}
private float getTangent(int k) {
return tangentFactor * (getClippedInput(k + 1) - getClippedInput(k - 1)) / 2;
private fun getClippedInput(i: Int): Float {
return if (i >= 0 && i < length) {
array[i]
} else array[clipClamp(i, length)]
}
public float interpolate(final float t) {
int k = (int) Math.floor(t);
float[] m = new float[]{getTangent(k), getTangent(k + 1)};
float[] p = new float[]{getClippedInput(k), getClippedInput(k + 1)};
final float t1 = t - k;
final float t2 = t1 * t1;
final float t3 = t1 * t2;
return (2 * t3 - 3 * t2 + 1) * p[0] + (t3 - 2 * t2 + t1) * m[0] + (-2 * t3 + 3 * t2) * p[1] + (t3 - t2) * m[1];
private fun clipClamp(i: Int, n: Int): Int {
return Math.max(0, Math.min(i, n - 1))
}
private float getClippedInput(int i) {
if (i >= 0 && i < length) {
return array[i];
}
return array[clipClamp(i, length)];
init {
this.array = Arrays.copyOf(array, array.size)
length = array.size
tangentFactor = 1 - Math.max(0, Math.min(1, cubicTension))
}
private int clipClamp(int i, int n) {
return Math.max(0, Math.min(i, n - 1));
}
}
}

View File

@ -1,61 +1,46 @@
package awais.instagrabber.utils;
package awais.instagrabber.utils
import android.net.Uri;
import android.text.TextUtils;
import android.net.Uri
import android.text.TextUtils
import awais.instagrabber.models.IntentModel
import awais.instagrabber.models.enums.IntentModelType
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.List;
import awais.instagrabber.models.IntentModel;
import awais.instagrabber.models.enums.IntentModelType;
public final class IntentUtils {
@Nullable
public static IntentModel parseUrl(@NonNull final String url) {
final Uri parsedUrl = Uri.parse(url).normalizeScheme();
object IntentUtils {
@JvmStatic
fun parseUrl(url: String): IntentModel? {
val parsedUrl = Uri.parse(url).normalizeScheme()
// final String domain = parsedUrl.getHost().replaceFirst("^www\\.", "");
// final boolean isHttpsUri = "https".equals(parsedUrl.getScheme());
final List<String> paths = parsedUrl.getPathSegments();
val paths = parsedUrl.pathSegments
if (paths.isEmpty()) {
return null;
return null
}
String path = paths.get(0);
String text = null;
IntentModelType type = IntentModelType.UNKNOWN;
if (1 == paths.size()) {
text = path;
type = IntentModelType.USERNAME;
} else if ("_u".equals(path)) {
text = paths.get(1);
type = IntentModelType.USERNAME;
} else if ("p".equals(path) || "reel".equals(path) || "tv".equals(path)) {
text = paths.get(1);
type = IntentModelType.POST;
} else if (2 < paths.size() && "explore".equals(path)) {
path = paths.get(1);
if ("locations".equals(path)) {
text = paths.get(2);
type = IntentModelType.LOCATION;
var path = paths[0]
var text: String? = null
var type = IntentModelType.UNKNOWN
if (1 == paths.size) {
text = path
type = IntentModelType.USERNAME
} else if ("_u" == path) {
text = paths[1]
type = IntentModelType.USERNAME
} else if ("p" == path || "reel" == path || "tv" == path) {
text = paths[1]
type = IntentModelType.POST
} else if (2 < paths.size && "explore" == path) {
path = paths[1]
if ("locations" == path) {
text = paths[2]
type = IntentModelType.LOCATION
}
if ("tags".equals(path)) {
text = paths.get(2);
type = IntentModelType.HASHTAG;
if ("tags" == path) {
text = paths[2]
type = IntentModelType.HASHTAG
}
}
if (TextUtils.isEmpty(text)) {
return null;
}
return new IntentModel(type, text);
return if (TextUtils.isEmpty(text)) {
null
} else IntentModel(type, text!!)
}
}
}

View File

@ -1,49 +1,37 @@
package awais.instagrabber.utils;
package awais.instagrabber.utils
import java.util.ArrayList;
import java.util.List;
import awais.instagrabber.repositories.responses.Media
import java.util.*
import awais.instagrabber.repositories.responses.Caption;
import awais.instagrabber.repositories.responses.Media;
class KeywordsFilterUtils(private val keywords: ArrayList<String>) {
// fun filter(caption: String?): Boolean {
// if (caption == null) return false
// if (keywords.isEmpty()) return false
// val temp = caption.toLowerCase()
// for (s in keywords) {
// if (temp.contains(s)) return true
// }
// return false
// }
public final class KeywordsFilterUtils {
private final ArrayList<String> keywords;
public KeywordsFilterUtils(final ArrayList<String> keywords){
this.keywords = keywords;
}
public boolean filter(final String caption){
if(caption == null) return false;
if(keywords.isEmpty()) return false;
final String temp = caption.toLowerCase();
for(final String s:keywords){
if(temp.contains(s)) return true;
fun filter(media: Media?): Boolean {
if (media == null) return false
val (_, text) = media.caption ?: return false
if (keywords.isEmpty()) return false
val temp = text!!.lowercase(Locale.getDefault())
for (s in keywords) {
if (temp.contains(s)) return true
}
return false;
return false
}
public boolean filter(final Media media){
if(media == null) return false;
final Caption c = media.getCaption();
if(c == null) return false;
if(keywords.isEmpty()) return false;
final String temp = c.getText().toLowerCase();
for(final String s:keywords){
if(temp.contains(s)) return true;
fun filter(media: List<Media>?): List<Media>? {
if (keywords.isEmpty()) return media
if (media == null) return ArrayList()
val result: MutableList<Media> = ArrayList()
for (m in media) {
if (!filter(m)) result.add(m)
}
return false;
return result
}
public List<Media> filter(final List<Media> media){
if(keywords.isEmpty()) return media;
if(media == null) return new ArrayList<>();
final List<Media> result= new ArrayList<>();
for(final Media m:media){
if(!filter(m)) result.add(m);
}
return result;
}
}
}

View File

@ -1,50 +1,44 @@
package awais.instagrabber.utils;
@file:JvmName("ThemeUtils")
import android.content.Context;
import android.content.res.Configuration;
import android.os.Build;
package awais.instagrabber.utils
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatDelegate;
import awais.instagrabber.R;
import static awais.instagrabber.utils.Utils.settingsHelper;
public final class ThemeUtils {
private static final String TAG = "ThemeUtils";
public static void changeTheme(@NonNull final Context context) {
int themeCode = AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM; // this is fallback / default
if (settingsHelper != null) themeCode = settingsHelper.getThemeCode(false);
import android.content.Context
import android.content.res.Configuration
import android.os.Build
import androidx.appcompat.app.AppCompatDelegate
import awais.instagrabber.R
object ThemeUtils {
fun changeTheme(context: Context) {
var themeCode = AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM // this is fallback / default
if (Utils.settingsHelper != null) themeCode = Utils.settingsHelper.getThemeCode(false)
if (themeCode == AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM && Build.VERSION.SDK_INT < 29) {
themeCode = AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY;
themeCode = AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY
}
final boolean isNight = isNight(context, themeCode);
final String themeResName = isNight ? settingsHelper.getString(Constants.PREF_DARK_THEME)
: settingsHelper.getString(Constants.PREF_LIGHT_THEME);
final int themeResId = context.getResources().getIdentifier(themeResName, "style", context.getPackageName());
final int finalThemeResId;
if (themeResId <= 0) {
val isNight = isNight(context, themeCode)
val themeResName =
if (isNight) Utils.settingsHelper.getString(Constants.PREF_DARK_THEME) else Utils.settingsHelper.getString(
Constants.PREF_LIGHT_THEME
)
val themeResId = context.resources.getIdentifier(themeResName, "style", context.packageName)
val finalThemeResId: Int
finalThemeResId = if (themeResId <= 0) {
// Nothing set in settings
finalThemeResId = isNight ? R.style.AppTheme_Dark_Black
: R.style.AppTheme_Light_White;
} else finalThemeResId = themeResId;
if (isNight) R.style.AppTheme_Dark_Black else R.style.AppTheme_Light_White
} else themeResId
// Log.d(TAG, "changeTheme: finalThemeResId: " + finalThemeResId);
context.setTheme(finalThemeResId);
context.setTheme(finalThemeResId)
}
public static boolean isNight(final Context context, final int themeCode) {
fun isNight(context: Context, themeCode: Int): Boolean {
// check if setting is set to 'Dark'
boolean isNight = themeCode == AppCompatDelegate.MODE_NIGHT_YES;
var isNight = themeCode == AppCompatDelegate.MODE_NIGHT_YES
// if not dark check if themeCode is MODE_NIGHT_FOLLOW_SYSTEM or MODE_NIGHT_AUTO_BATTERY
if (!isNight && (themeCode == AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM || themeCode == AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY)) {
// check if resulting theme would be NIGHT
final int uiMode = context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
isNight = uiMode == Configuration.UI_MODE_NIGHT_YES;
val uiMode = context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
isNight = uiMode == Configuration.UI_MODE_NIGHT_YES
}
return isNight;
return isNight
}
}
}