Migrate last java classes from API module to kotlin except data sources

This commit is contained in:
Shinokuni 2021-07-20 19:58:45 +02:00
parent a5501b2524
commit 137e591573
12 changed files with 128 additions and 167 deletions

View File

@ -1,53 +0,0 @@
package com.readrops.api.utils;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import org.jsoup.Jsoup;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public final class ApiUtils {
public static final String HTML_CONTENT_TYPE = "text/html";
public static final String CONTENT_TYPE_HEADER = "content-type";
public static final String ETAG_HEADER = "ETag";
public static final String IF_NONE_MATCH_HEADER = "If-None-Match";
public static final String LAST_MODIFIED_HEADER = "Last-Modified";
public static final String IF_MODIFIED_HEADER = "If-Modified-Since";
public static final int HTTP_UNPROCESSABLE = 422;
public static final int HTTP_NOT_FOUND = 404;
public static final int HTTP_CONFLICT = 409;
private static final String RSS_CONTENT_TYPE_REGEX = "([^;]+)";
public static boolean isMimeImage(@NonNull String type) {
return type.equals("image") || type.equals("image/jpeg") || type.equals("image/jpg")
|| type.equals("image/png");
}
@Nullable
public static String parseContentType(String header) {
Matcher matcher = Pattern.compile(RSS_CONTENT_TYPE_REGEX)
.matcher(header);
if (matcher.find()) {
return matcher.group(0);
} else {
return null;
}
}
/**
* Remove html tags and trim the text
*
* @param text string to clean
* @return cleaned text
*/
public static String cleanText(String text) {
return Jsoup.parse(text).text().trim();
}
}

View File

@ -0,0 +1,43 @@
package com.readrops.api.utils
import org.jsoup.Jsoup
import java.util.regex.Pattern
object ApiUtils {
const val HTML_CONTENT_TYPE = "text/html"
const val CONTENT_TYPE_HEADER = "content-type"
const val ETAG_HEADER = "ETag"
const val IF_NONE_MATCH_HEADER = "If-None-Match"
const val LAST_MODIFIED_HEADER = "Last-Modified"
const val IF_MODIFIED_HEADER = "If-Modified-Since"
const val HTTP_UNPROCESSABLE = 422
const val HTTP_NOT_FOUND = 404
const val HTTP_CONFLICT = 409
private const val RSS_CONTENT_TYPE_REGEX = "([^;]+)"
fun isMimeImage(type: String): Boolean =
type == "image" || type == "image/jpeg" || type == "image/jpg" || type == "image/png"
fun parseContentType(header: String?): String? {
val matcher = Pattern.compile(RSS_CONTENT_TYPE_REGEX)
.matcher(header)
return if (matcher.find()) {
matcher.group(0)
} else {
null
}
}
/**
* Remove html tags and trim the text
*
* @param text string to clean
* @return cleaned text
*/
fun cleanText(text: String?): String {
return Jsoup.parse(text).text().trim()
}
}

View File

@ -1,73 +0,0 @@
package com.readrops.api.utils;
import androidx.annotation.Nullable;
import org.joda.time.LocalDateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.DateTimeFormatterBuilder;
import java.util.Locale;
public final class DateUtils {
private static final String TAG = DateUtils.class.getSimpleName();
/**
* Base of common RSS 2 date formats.
* Examples :
* Fri, 04 Jan 2019 22:21:46 GMT
* Fri, 04 Jan 2019 22:21:46 +0000
*/
private static final String RSS_2_BASE_PATTERN = "EEE, dd MMM yyyy HH:mm:ss";
private static final String GMT_PATTERN = "ZZZ";
private static final String OFFSET_PATTERN = "Z";
private static final String ISO_PATTERN = ".SSSZZ";
private static final String EDT_PATTERN = "zzz";
/**
* Date pattern for format : 2019-01-04T22:21:46+00:00
*/
private static final String ATOM_JSON_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";
@Nullable
public static LocalDateTime parse(String value) {
if (value == null) {
return null;
}
try {
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendOptional(DateTimeFormat.forPattern(RSS_2_BASE_PATTERN + " ").getParser()) // with timezone
.appendOptional(DateTimeFormat.forPattern(RSS_2_BASE_PATTERN).getParser()) // no timezone, important order here
.appendOptional(DateTimeFormat.forPattern(ATOM_JSON_DATE_FORMAT).getParser())
.appendOptional(DateTimeFormat.forPattern(GMT_PATTERN).getParser())
.appendOptional(DateTimeFormat.forPattern(OFFSET_PATTERN).getParser())
.appendOptional(DateTimeFormat.forPattern(ISO_PATTERN).getParser())
.appendOptional(DateTimeFormat.forPattern(EDT_PATTERN).getParser())
.toFormatter()
.withLocale(Locale.ENGLISH)
.withOffsetParsed();
return formatter.parseLocalDateTime(value);
} catch (Exception e) {
return null;
}
}
public static String formattedDateByLocal(LocalDateTime dateTime) {
return DateTimeFormat.mediumDate()
.withLocale(Locale.getDefault())
.print(dateTime);
}
public static String formattedDateTimeByLocal(LocalDateTime dateTime) {
return DateTimeFormat.forPattern("dd MMM yyyy · HH:mm")
.withLocale(Locale.getDefault())
.print(dateTime);
}
}

View File

@ -0,0 +1,67 @@
package com.readrops.api.utils
import org.joda.time.LocalDateTime
import org.joda.time.format.DateTimeFormat
import org.joda.time.format.DateTimeFormatterBuilder
import java.util.*
object DateUtils {
private val TAG = DateUtils::class.java.simpleName
/**
* Base of common RSS 2 date formats.
* Examples :
* Fri, 04 Jan 2019 22:21:46 GMT
* Fri, 04 Jan 2019 22:21:46 +0000
*/
private const val RSS_2_BASE_PATTERN = "EEE, dd MMM yyyy HH:mm:ss"
private const val GMT_PATTERN = "ZZZ"
private const val OFFSET_PATTERN = "Z"
private const val ISO_PATTERN = ".SSSZZ"
private const val EDT_PATTERN = "zzz"
/**
* Date pattern for format : 2019-01-04T22:21:46+00:00
*/
private const val ATOM_JSON_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss"
@JvmStatic
fun parse(value: String?): LocalDateTime? = if (value == null) {
null
} else try {
val formatter = DateTimeFormatterBuilder()
.appendOptional(DateTimeFormat.forPattern("$RSS_2_BASE_PATTERN ").parser) // with timezone
.appendOptional(DateTimeFormat.forPattern(RSS_2_BASE_PATTERN).parser) // no timezone, important order here
.appendOptional(DateTimeFormat.forPattern(ATOM_JSON_DATE_FORMAT).parser)
.appendOptional(DateTimeFormat.forPattern(GMT_PATTERN).parser)
.appendOptional(DateTimeFormat.forPattern(OFFSET_PATTERN).parser)
.appendOptional(DateTimeFormat.forPattern(ISO_PATTERN).parser)
.appendOptional(DateTimeFormat.forPattern(EDT_PATTERN).parser)
.toFormatter()
.withLocale(Locale.ENGLISH)
.withOffsetParsed()
formatter.parseLocalDateTime(value)
} catch (e: Exception) {
null
}
@JvmStatic
fun formattedDateByLocal(dateTime: LocalDateTime): String {
return DateTimeFormat.mediumDate()
.withLocale(Locale.getDefault())
.print(dateTime)
}
@JvmStatic
fun formattedDateTimeByLocal(dateTime: LocalDateTime): String {
return DateTimeFormat.forPattern("dd MMM yyyy · HH:mm")
.withLocale(Locale.getDefault())
.print(dateTime)
}
}

View File

@ -1,12 +0,0 @@
package com.readrops.api.utils.exceptions;
public class ConflictException extends Exception {
public ConflictException() {
}
public ConflictException(String message) {
super(message);
}
}

View File

@ -0,0 +1,3 @@
package com.readrops.api.utils.exceptions
class ConflictException : Exception()

View File

@ -1,12 +0,0 @@
package com.readrops.api.utils.exceptions;
public class ParseException extends Exception {
public ParseException() {
super();
}
public ParseException(String message) {
super(message);
}
}

View File

@ -0,0 +1,3 @@
package com.readrops.api.utils.exceptions
class ParseException(message: String?) : Exception(message)

View File

@ -1,12 +0,0 @@
package com.readrops.api.utils.exceptions;
public class UnknownFormatException extends Exception {
public UnknownFormatException() {
}
public UnknownFormatException(String message) {
super(message);
}
}

View File

@ -0,0 +1,7 @@
package com.readrops.api.utils.exceptions
class UnknownFormatException : Exception {
constructor()
constructor(message: String?) : super(message)
}

View File

@ -17,9 +17,9 @@ import com.readrops.api.utils.exceptions.ConflictException;
import com.readrops.api.utils.exceptions.UnknownFormatException;
import com.readrops.app.R;
import com.readrops.app.databinding.FragmentFoldersBinding;
import com.readrops.app.feedsfolders.ManageFeedsFoldersViewModel;
import com.readrops.app.utils.SharedPreferencesManager;
import com.readrops.app.utils.Utils;
import com.readrops.app.feedsfolders.ManageFeedsFoldersViewModel;
import com.readrops.db.entities.Folder;
import com.readrops.db.entities.account.Account;

View File

@ -25,13 +25,13 @@ import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.transition.DrawableCrossFadeFactory;
import com.bumptech.glide.util.ViewPreloadSizeProvider;
import com.readrops.app.R;
import com.readrops.db.entities.Item;
import com.readrops.db.pojo.ItemWithFeed;
import com.readrops.app.databinding.ListItemBinding;
import com.readrops.api.utils.DateUtils;
import com.readrops.app.R;
import com.readrops.app.databinding.ListItemBinding;
import com.readrops.app.utils.GlideRequests;
import com.readrops.app.utils.Utils;
import com.readrops.db.entities.Item;
import com.readrops.db.pojo.ItemWithFeed;
import java.util.ArrayList;
import java.util.Collections;