From b1628492f5a452e437d8e16bad17acf5f8965b2d Mon Sep 17 00:00:00 2001 From: Austin Huang Date: Tue, 22 Jun 2021 19:23:41 -0400 Subject: [PATCH] convert some utils to kotlin --- .../awais/instagrabber/utils/DateUtils.kt | 27 ++++--- .../instagrabber/utils/PermissionUtils.kt | 73 ++++++++++--------- .../webservices/MediaRepository.kt | 2 +- 3 files changed, 54 insertions(+), 48 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/utils/DateUtils.kt b/app/src/main/java/awais/instagrabber/utils/DateUtils.kt index 473f4298..474d4eb8 100644 --- a/app/src/main/java/awais/instagrabber/utils/DateUtils.kt +++ b/app/src/main/java/awais/instagrabber/utils/DateUtils.kt @@ -1,18 +1,17 @@ -package awais.instagrabber.utils; +package awais.instagrabber.utils -import androidx.annotation.NonNull; +import java.time.LocalDateTime +import java.util.* -import java.time.LocalDateTime; -import java.util.Calendar; -import java.util.Locale; +object DateUtils { + val timezoneOffset: Int + get() { + val calendar = Calendar.getInstance(Locale.getDefault()) + return -(calendar[Calendar.ZONE_OFFSET] + calendar[Calendar.DST_OFFSET]) / (60 * 1000) + } -public final class DateUtils { - public static int getTimezoneOffset() { - final Calendar calendar = Calendar.getInstance(Locale.getDefault()); - return -(calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET)) / (60 * 1000); + @JvmStatic + fun isBeforeOrEqual(localDateTime: LocalDateTime, comparedTo: LocalDateTime): Boolean { + return localDateTime.isBefore(comparedTo) || localDateTime.isEqual(comparedTo) } - - public static boolean isBeforeOrEqual(@NonNull final LocalDateTime localDateTime, @NonNull final LocalDateTime comparedTo) { - return localDateTime.isBefore(comparedTo) || localDateTime.isEqual(comparedTo); - } -} +} \ No newline at end of file diff --git a/app/src/main/java/awais/instagrabber/utils/PermissionUtils.kt b/app/src/main/java/awais/instagrabber/utils/PermissionUtils.kt index 8b92fe1d..2071c24d 100644 --- a/app/src/main/java/awais/instagrabber/utils/PermissionUtils.kt +++ b/app/src/main/java/awais/instagrabber/utils/PermissionUtils.kt @@ -1,46 +1,53 @@ -package awais.instagrabber.utils; +package awais.instagrabber.utils -import android.content.Context; -import android.content.pm.PackageManager; +import android.Manifest.permission +import android.content.Context +import androidx.core.content.PermissionChecker +import awais.instagrabber.utils.PermissionUtils +import androidx.core.content.ContextCompat +import android.content.pm.PackageManager +import androidx.appcompat.app.AppCompatActivity +import androidx.core.app.ActivityCompat +import androidx.fragment.app.Fragment -import androidx.annotation.NonNull; -import androidx.appcompat.app.AppCompatActivity; -import androidx.core.app.ActivityCompat; -import androidx.core.content.ContextCompat; -import androidx.core.content.PermissionChecker; -import androidx.fragment.app.Fragment; - -import static android.Manifest.permission.CAMERA; -import static android.Manifest.permission.READ_EXTERNAL_STORAGE; -import static android.Manifest.permission.RECORD_AUDIO; -import static androidx.core.content.PermissionChecker.checkSelfPermission; - -public class PermissionUtils { - public static final String[] AUDIO_RECORD_PERMS = new String[]{RECORD_AUDIO}; - public static final String[] ATTACH_MEDIA_PERMS = new String[]{READ_EXTERNAL_STORAGE}; - public static final String[] CAMERA_PERMS = new String[]{CAMERA}; - - public static boolean hasAudioRecordPerms(@NonNull final Context context) { - return checkSelfPermission(context, RECORD_AUDIO) == PermissionChecker.PERMISSION_GRANTED; +object PermissionUtils { + val AUDIO_RECORD_PERMS = arrayOf(permission.RECORD_AUDIO) + val ATTACH_MEDIA_PERMS = arrayOf(permission.READ_EXTERNAL_STORAGE) + val CAMERA_PERMS = arrayOf(permission.CAMERA) + @JvmStatic + fun hasAudioRecordPerms(context: Context): Boolean { + return PermissionChecker.checkSelfPermission( + context, + permission.RECORD_AUDIO + ) == PermissionChecker.PERMISSION_GRANTED } - public static void requestAudioRecordPerms(final Fragment fragment, final int requestCode) { - fragment.requestPermissions(AUDIO_RECORD_PERMS, requestCode); + @JvmStatic + fun requestAudioRecordPerms(fragment: Fragment, requestCode: Int) { + fragment.requestPermissions(AUDIO_RECORD_PERMS, requestCode) } - public static boolean hasAttachMediaPerms(@NonNull final Context context) { - return checkSelfPermission(context, READ_EXTERNAL_STORAGE) == PermissionChecker.PERMISSION_GRANTED; + @JvmStatic + fun hasAttachMediaPerms(context: Context): Boolean { + return PermissionChecker.checkSelfPermission( + context, + permission.READ_EXTERNAL_STORAGE + ) == PermissionChecker.PERMISSION_GRANTED } - public static void requestAttachMediaPerms(final Fragment fragment, final int requestCode) { - fragment.requestPermissions(ATTACH_MEDIA_PERMS, requestCode); + @JvmStatic + fun requestAttachMediaPerms(fragment: Fragment, requestCode: Int) { + fragment.requestPermissions(ATTACH_MEDIA_PERMS, requestCode) } - public static boolean hasCameraPerms(final Context context) { - return ContextCompat.checkSelfPermission(context, CAMERA) == PackageManager.PERMISSION_GRANTED; + fun hasCameraPerms(context: Context?): Boolean { + return ContextCompat.checkSelfPermission( + context!!, + permission.CAMERA + ) == PackageManager.PERMISSION_GRANTED } - public static void requestCameraPerms(final AppCompatActivity activity, final int requestCode) { - ActivityCompat.requestPermissions(activity, CAMERA_PERMS, requestCode); + fun requestCameraPerms(activity: AppCompatActivity?, requestCode: Int) { + ActivityCompat.requestPermissions(activity!!, CAMERA_PERMS, requestCode) } -} +} \ No newline at end of file diff --git a/app/src/main/java/awais/instagrabber/webservices/MediaRepository.kt b/app/src/main/java/awais/instagrabber/webservices/MediaRepository.kt index 9976a072..172d12cd 100644 --- a/app/src/main/java/awais/instagrabber/webservices/MediaRepository.kt +++ b/app/src/main/java/awais/instagrabber/webservices/MediaRepository.kt @@ -132,7 +132,7 @@ class MediaRepository(private val service: MediaService) { videoOptions.clips = listOf(Clip(videoOptions.length, options.sourceType)) } } - val timezoneOffset = DateUtils.getTimezoneOffset().toString() + val timezoneOffset = DateUtils.timezoneOffset.toString() val form = mutableMapOf( "timezone_offset" to timezoneOffset, "_csrftoken" to csrfToken,