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 { class CubicInterpolation @JvmOverloads constructor(array: FloatArray, cubicTension: Int = 0) {
private val array: FloatArray
private final float[] array; private val tangentFactor: Int
private final int tangentFactor; private val length: Int
private final int length; private fun getTangent(k: Int): Float {
return tangentFactor * (getClippedInput(k + 1) - getClippedInput(k - 1)) / 2
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));
} }
public CubicInterpolation(final float[] array) { fun interpolate(t: Float): Float {
this(array, 0); 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) { private fun getClippedInput(i: Int): Float {
return tangentFactor * (getClippedInput(k + 1) - getClippedInput(k - 1)) / 2; return if (i >= 0 && i < length) {
array[i]
} else array[clipClamp(i, length)]
} }
public float interpolate(final float t) { private fun clipClamp(i: Int, n: Int): Int {
int k = (int) Math.floor(t); return Math.max(0, Math.min(i, n - 1))
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 float getClippedInput(int i) { init {
if (i >= 0 && i < length) { this.array = Arrays.copyOf(array, array.size)
return array[i]; length = array.size
} tangentFactor = 1 - Math.max(0, Math.min(1, cubicTension))
return array[clipClamp(i, length)];
}
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.net.Uri
import android.text.TextUtils; import android.text.TextUtils
import awais.instagrabber.models.IntentModel
import awais.instagrabber.models.enums.IntentModelType
import androidx.annotation.NonNull; object IntentUtils {
import androidx.annotation.Nullable; @JvmStatic
fun parseUrl(url: String): IntentModel? {
import java.util.List; val parsedUrl = Uri.parse(url).normalizeScheme()
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();
// final String domain = parsedUrl.getHost().replaceFirst("^www\\.", ""); // final String domain = parsedUrl.getHost().replaceFirst("^www\\.", "");
// final boolean isHttpsUri = "https".equals(parsedUrl.getScheme()); // final boolean isHttpsUri = "https".equals(parsedUrl.getScheme());
val paths = parsedUrl.pathSegments
final List<String> paths = parsedUrl.getPathSegments();
if (paths.isEmpty()) { if (paths.isEmpty()) {
return null; return null
} }
var path = paths[0]
String path = paths.get(0); var text: String? = null
String text = null; var type = IntentModelType.UNKNOWN
IntentModelType type = IntentModelType.UNKNOWN; if (1 == paths.size) {
if (1 == paths.size()) { text = path
text = path; type = IntentModelType.USERNAME
type = IntentModelType.USERNAME; } else if ("_u" == path) {
} else if ("_u".equals(path)) { text = paths[1]
text = paths.get(1); type = IntentModelType.USERNAME
type = IntentModelType.USERNAME; } else if ("p" == path || "reel" == path || "tv" == path) {
} else if ("p".equals(path) || "reel".equals(path) || "tv".equals(path)) { text = paths[1]
text = paths.get(1); type = IntentModelType.POST
type = IntentModelType.POST; } else if (2 < paths.size && "explore" == path) {
} else if (2 < paths.size() && "explore".equals(path)) { path = paths[1]
path = paths.get(1); if ("locations" == path) {
text = paths[2]
if ("locations".equals(path)) { type = IntentModelType.LOCATION
text = paths.get(2);
type = IntentModelType.LOCATION;
} }
if ("tags" == path) {
if ("tags".equals(path)) { text = paths[2]
text = paths.get(2); type = IntentModelType.HASHTAG
type = IntentModelType.HASHTAG;
} }
} }
return if (TextUtils.isEmpty(text)) {
if (TextUtils.isEmpty(text)) { null
return null; } else IntentModel(type, text!!)
}
return new IntentModel(type, text);
} }
} }

View File

@ -1,49 +1,37 @@
package awais.instagrabber.utils; package awais.instagrabber.utils
import java.util.ArrayList; import awais.instagrabber.repositories.responses.Media
import java.util.List; import java.util.*
import awais.instagrabber.repositories.responses.Caption; class KeywordsFilterUtils(private val keywords: ArrayList<String>) {
import awais.instagrabber.repositories.responses.Media; // 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 { fun filter(media: Media?): Boolean {
if (media == null) return false
private final ArrayList<String> keywords; val (_, text) = media.caption ?: return false
if (keywords.isEmpty()) return false
public KeywordsFilterUtils(final ArrayList<String> keywords){ val temp = text!!.lowercase(Locale.getDefault())
this.keywords = keywords; for (s in keywords) {
if (temp.contains(s)) return true
}
return false
} }
public boolean filter(final String caption){ fun filter(media: List<Media>?): List<Media>? {
if(caption == null) return false; if (keywords.isEmpty()) return media
if(keywords.isEmpty()) return false; if (media == null) return ArrayList()
final String temp = caption.toLowerCase(); val result: MutableList<Media> = ArrayList()
for(final String s:keywords){ for (m in media) {
if(temp.contains(s)) return true; if (!filter(m)) result.add(m)
} }
return false; return result
}
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;
}
return false;
}
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; package awais.instagrabber.utils
import android.content.res.Configuration;
import android.os.Build;
import androidx.annotation.NonNull; import android.content.Context
import androidx.appcompat.app.AppCompatDelegate; import android.content.res.Configuration
import android.os.Build
import awais.instagrabber.R; 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);
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) { 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); val isNight = isNight(context, themeCode)
final String themeResName = isNight ? settingsHelper.getString(Constants.PREF_DARK_THEME) val themeResName =
: settingsHelper.getString(Constants.PREF_LIGHT_THEME); if (isNight) Utils.settingsHelper.getString(Constants.PREF_DARK_THEME) else Utils.settingsHelper.getString(
final int themeResId = context.getResources().getIdentifier(themeResName, "style", context.getPackageName()); Constants.PREF_LIGHT_THEME
final int finalThemeResId; )
if (themeResId <= 0) { val themeResId = context.resources.getIdentifier(themeResName, "style", context.packageName)
val finalThemeResId: Int
finalThemeResId = if (themeResId <= 0) {
// Nothing set in settings // Nothing set in settings
finalThemeResId = isNight ? R.style.AppTheme_Dark_Black if (isNight) R.style.AppTheme_Dark_Black else R.style.AppTheme_Light_White
: R.style.AppTheme_Light_White; } else themeResId
} else finalThemeResId = themeResId;
// Log.d(TAG, "changeTheme: finalThemeResId: " + finalThemeResId); // 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' // 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 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)) { if (!isNight && (themeCode == AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM || themeCode == AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY)) {
// check if resulting theme would be NIGHT // check if resulting theme would be NIGHT
final int uiMode = context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK; val uiMode = context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
isNight = uiMode == Configuration.UI_MODE_NIGHT_YES; isNight = uiMode == Configuration.UI_MODE_NIGHT_YES
} }
return isNight; return isNight
} }
} }