Refactored AccessibilityHelper for a better read experience
This commit is contained in:
parent
eabc2aa0cd
commit
00c211aa75
|
|
@ -1,20 +1,90 @@
|
|||
package com.example.acloc.utility;
|
||||
|
||||
import android.content.Context;
|
||||
import androidx.annotation.DrawableRes;
|
||||
import androidx.annotation.StringRes;
|
||||
import androidx.annotation.ColorRes;
|
||||
|
||||
import com.ieslamar.acloc.R;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class AccessibilityHelper {
|
||||
|
||||
public enum AccessibilityType {
|
||||
WHEELCHAIR(R.string.accessibility_wheelchair, R.drawable.ic_accessible,
|
||||
new String[]{"silla", "wheelchair", "rueda", "silla_de_ruedas"}),
|
||||
VISUAL(R.string.accessibility_visual, R.drawable.ic_visibility,
|
||||
new String[]{"visual", "vista", "ciego", "vision", "sight"}),
|
||||
HEARING(R.string.accessibility_hearing, R.drawable.ic_hearing,
|
||||
new String[]{"auditivo", "hearing", "sordo", "audio", "sound"}),
|
||||
COGNITIVE(R.string.accessibility_cognitive, R.drawable.ic_psychology,
|
||||
new String[]{"cognitivo", "mental", "cognitive", "psicologico"}),
|
||||
MOBILITY(R.string.accessibility_mobility, R.drawable.ic_directions_walk,
|
||||
new String[]{"movilidad", "mobility", "caminar", "walking", "movement"}),
|
||||
PARKING(R.string.accessibility_parking, R.drawable.ic_local_parking,
|
||||
new String[]{"estacionamiento", "parking", "aparcamiento", "plaza"}),
|
||||
ENTRANCE(R.string.accessibility_entrance, R.drawable.ic_door_front,
|
||||
new String[]{"entrada", "entrance", "puerta", "door", "acceso"}),
|
||||
BATHROOM(R.string.accessibility_bathroom, R.drawable.ic_wc,
|
||||
new String[]{"baño", "bathroom", "aseo", "wc", "toilet"}),
|
||||
ELEVATOR(R.string.accessibility_elevator, R.drawable.ic_elevator,
|
||||
new String[]{"ascensor", "elevator", "lift"}),
|
||||
RAMP(R.string.accessibility_ramp, R.drawable.ic_trending_up,
|
||||
new String[]{"rampa", "ramp", "slope", "incline"});
|
||||
|
||||
@StringRes public final int displayNameRes;
|
||||
@DrawableRes public final int iconRes;
|
||||
public final String[] keywords;
|
||||
|
||||
AccessibilityType(@StringRes int displayNameRes, @DrawableRes int iconRes, String[] keywords) {
|
||||
this.displayNameRes = displayNameRes;
|
||||
this.iconRes = iconRes;
|
||||
this.keywords = keywords;
|
||||
}
|
||||
}
|
||||
|
||||
public enum RatingLevel {
|
||||
GOOD(Constants.GOOD_RATING, R.color.accessibility_good_bg,
|
||||
R.color.accessibility_good_text, R.color.accessibility_good_border),
|
||||
AVERAGE(Constants.AVERAGE_RATING, R.color.accessibility_average_bg,
|
||||
R.color.accessibility_average_text, R.color.accessibility_average_border),
|
||||
BAD(Constants.BAD_RATING, R.color.accessibility_poor_bg,
|
||||
R.color.accessibility_poor_text, R.color.accessibility_poor_border);
|
||||
|
||||
public final int value;
|
||||
@ColorRes public final int backgroundColorRes;
|
||||
@ColorRes public final int textColorRes;
|
||||
@ColorRes public final int borderColorRes;
|
||||
|
||||
RatingLevel(int value, @ColorRes int backgroundColorRes,
|
||||
@ColorRes int textColorRes, @ColorRes int borderColorRes) {
|
||||
this.value = value;
|
||||
this.backgroundColorRes = backgroundColorRes;
|
||||
this.textColorRes = textColorRes;
|
||||
this.borderColorRes = borderColorRes;
|
||||
}
|
||||
|
||||
public static RatingLevel fromValue(int rating) {
|
||||
for (RatingLevel level : values()) {
|
||||
if (level.value == rating) return level;
|
||||
}
|
||||
return AVERAGE;
|
||||
}
|
||||
}
|
||||
|
||||
public static class AccessibilityInfo {
|
||||
public final String displayName;
|
||||
public final int iconResource;
|
||||
public final int backgroundColorRes;
|
||||
public final int textColorRes;
|
||||
public final int borderColorRes;
|
||||
@DrawableRes public final int iconResource;
|
||||
@ColorRes public final int backgroundColorRes;
|
||||
@ColorRes public final int textColorRes;
|
||||
@ColorRes public final int borderColorRes;
|
||||
|
||||
public AccessibilityInfo(String displayName, int iconResource,
|
||||
int backgroundColorRes, int textColorRes, int borderColorRes) {
|
||||
public AccessibilityInfo(String displayName, @DrawableRes int iconResource,
|
||||
@ColorRes int backgroundColorRes, @ColorRes int textColorRes,
|
||||
@ColorRes int borderColorRes) {
|
||||
this.displayName = displayName;
|
||||
this.iconResource = iconResource;
|
||||
this.backgroundColorRes = backgroundColorRes;
|
||||
|
|
@ -23,142 +93,108 @@ public class AccessibilityHelper {
|
|||
}
|
||||
}
|
||||
|
||||
public static AccessibilityInfo getAccessibilityInfo(String reportTypeName, int rating) {
|
||||
// Get icon based on report type name
|
||||
int iconResource = getIconForReportType(reportTypeName);
|
||||
private static final Map<String, AccessibilityType> typeCache = new HashMap<>();
|
||||
private static final Pattern TIMESTAMP_PATTERN = Pattern.compile("\\d{17}_");
|
||||
private static final Pattern SUFFIX_PATTERN = Pattern.compile("_[a-z0-9]{8}$");
|
||||
|
||||
// Get colors based on rating
|
||||
int[] colors = getColorsForRating(rating);
|
||||
public static AccessibilityInfo getAccessibilityInfo(Context context, String reportTypeName, int rating) {
|
||||
AccessibilityType type = getAccessibilityType(reportTypeName);
|
||||
RatingLevel ratingLevel = RatingLevel.fromValue(rating);
|
||||
|
||||
return new AccessibilityInfo(
|
||||
reportTypeName,
|
||||
iconResource,
|
||||
colors[0], // background
|
||||
colors[1], // text
|
||||
colors[2] // border
|
||||
);
|
||||
String displayName = type != null ?
|
||||
context.getString(type.displayNameRes) :
|
||||
getCleanDisplayName(context, reportTypeName);
|
||||
|
||||
int iconResource = type != null ? type.iconRes : R.drawable.ic_accessible;
|
||||
|
||||
return new AccessibilityInfo(displayName, iconResource, ratingLevel.backgroundColorRes,
|
||||
ratingLevel.textColorRes, ratingLevel.borderColorRes);
|
||||
}
|
||||
|
||||
// Made public for ReportTypeAdapter
|
||||
public static int getIconForReportType(String reportTypeName) {
|
||||
if (reportTypeName == null) return R.drawable.ic_accessible;
|
||||
|
||||
String name = reportTypeName.toLowerCase();
|
||||
|
||||
// Map based on common accessibility type names
|
||||
if (name.contains("silla") || name.contains("wheelchair") || name.contains("rueda")) {
|
||||
return R.drawable.ic_accessible;
|
||||
} else if (name.contains("visual") || name.contains("vista") || name.contains("ciego")) {
|
||||
return R.drawable.ic_visibility;
|
||||
} else if (name.contains("auditivo") || name.contains("hearing") || name.contains("sordo")) {
|
||||
return com.ieslamar.acloc.R.drawable.ic_hearing;
|
||||
} else if (name.contains("cognitivo") || name.contains("mental") || name.contains("cognitive")) {
|
||||
return R.drawable.ic_psychology;
|
||||
} else if (name.contains("movilidad") || name.contains("mobility") || name.contains("caminar")) {
|
||||
return R.drawable.ic_directions_walk;
|
||||
} else if (name.contains("estacionamiento") || name.contains("parking") || name.contains("aparcamiento")) {
|
||||
return R.drawable.ic_local_parking;
|
||||
} else if (name.contains("entrada") || name.contains("entrance") || name.contains("puerta")) {
|
||||
return R.drawable.ic_door_front;
|
||||
} else if (name.contains("baño") || name.contains("bathroom") || name.contains("aseo")) {
|
||||
return R.drawable.ic_wc;
|
||||
} else if (name.contains("ascensor") || name.contains("elevator")) {
|
||||
return R.drawable.ic_elevator;
|
||||
} else if (name.contains("rampa") || name.contains("ramp")) {
|
||||
return R.drawable.ic_trending_up;
|
||||
} else {
|
||||
return R.drawable.ic_accessible; // Default accessibility icon
|
||||
}
|
||||
}
|
||||
|
||||
// New method for getting display name
|
||||
public static String getDisplayName(Context context, String reportTypeName) {
|
||||
if (reportTypeName == null || reportTypeName.trim().isEmpty()) {
|
||||
return context.getString(R.string.accessibility_unknown);
|
||||
}
|
||||
|
||||
// Clean up the name first - remove timestamps and weird suffixes
|
||||
String cleanName = cleanReportTypeName(reportTypeName);
|
||||
String name = cleanName.toLowerCase();
|
||||
|
||||
// Return localized display names
|
||||
if (name.contains("silla") || name.contains("wheelchair") || name.contains("rueda")) {
|
||||
return context.getString(R.string.accessibility_wheelchair);
|
||||
} else if (name.contains("visual") || name.contains("vista") || name.contains("ciego")) {
|
||||
return context.getString(R.string.accessibility_visual);
|
||||
} else if (name.contains("auditivo") || name.contains("hearing") || name.contains("sordo")) {
|
||||
return context.getString(R.string.accessibility_hearing);
|
||||
} else if (name.contains("cognitivo") || name.contains("mental") || name.contains("cognitive")) {
|
||||
return context.getString(R.string.accessibility_cognitive);
|
||||
} else if (name.contains("movilidad") || name.contains("mobility") || name.contains("caminar")) {
|
||||
return context.getString(R.string.accessibility_mobility);
|
||||
} else if (name.contains("estacionamiento") || name.contains("parking") || name.contains("aparcamiento")) {
|
||||
return context.getString(R.string.accessibility_parking);
|
||||
} else if (name.contains("entrada") || name.contains("entrance") || name.contains("puerta")) {
|
||||
return context.getString(R.string.accessibility_entrance);
|
||||
} else if (name.contains("baño") || name.contains("bathroom") || name.contains("aseo")) {
|
||||
return context.getString(R.string.accessibility_bathroom);
|
||||
} else if (name.contains("ascensor") || name.contains("elevator")) {
|
||||
return context.getString(R.string.accessibility_elevator);
|
||||
} else if (name.contains("rampa") || name.contains("ramp")) {
|
||||
return context.getString(R.string.accessibility_ramp);
|
||||
} else {
|
||||
// If no mapping found, return the cleaned name
|
||||
return capitalizeFirstLetter(cleanName);
|
||||
AccessibilityType type = getAccessibilityType(reportTypeName);
|
||||
if (type != null) {
|
||||
return context.getString(type.displayNameRes);
|
||||
}
|
||||
return getCleanDisplayName(context, reportTypeName);
|
||||
}
|
||||
|
||||
@DrawableRes
|
||||
public static int getIconForReportType(String reportTypeName) {
|
||||
AccessibilityType type = getAccessibilityType(reportTypeName);
|
||||
return type != null ? type.iconRes : R.drawable.ic_accessible;
|
||||
}
|
||||
|
||||
private static AccessibilityType getAccessibilityType(String reportTypeName) {
|
||||
if (reportTypeName == null || reportTypeName.trim().isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String cacheKey = reportTypeName.toLowerCase();
|
||||
if (typeCache.containsKey(cacheKey)) {
|
||||
return typeCache.get(cacheKey);
|
||||
}
|
||||
|
||||
String cleanName = cleanReportTypeName(reportTypeName).toLowerCase();
|
||||
|
||||
for (AccessibilityType type : AccessibilityType.values()) {
|
||||
for (String keyword : type.keywords) {
|
||||
if (cleanName.contains(keyword.toLowerCase())) {
|
||||
typeCache.put(cacheKey, type);
|
||||
return type;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
typeCache.put(cacheKey, null);
|
||||
return null;
|
||||
}
|
||||
|
||||
// Add this new method to clean report type names
|
||||
private static String cleanReportTypeName(String reportTypeName) {
|
||||
if (reportTypeName == null) return "";
|
||||
|
||||
// Remove timestamp patterns like "20250418155930234_"
|
||||
String cleaned = reportTypeName.replaceAll("\\d{17}_", "");
|
||||
|
||||
// Remove random suffixes like "_a1kzpr9q"
|
||||
cleaned = cleaned.replaceAll("_[a-z0-9]{8}$", "");
|
||||
|
||||
// Remove any remaining underscores and replace with spaces
|
||||
cleaned = cleaned.replace("_", " ");
|
||||
|
||||
// Trim whitespace
|
||||
cleaned = cleaned.trim();
|
||||
String cleaned = reportTypeName;
|
||||
cleaned = TIMESTAMP_PATTERN.matcher(cleaned).replaceAll("");
|
||||
cleaned = SUFFIX_PATTERN.matcher(cleaned).replaceAll("");
|
||||
cleaned = cleaned.replace("_", " ").trim();
|
||||
|
||||
return cleaned;
|
||||
}
|
||||
|
||||
private static String capitalizeFirstLetter(String text) {
|
||||
if (text == null || text.isEmpty()) return text;
|
||||
return text.substring(0, 1).toUpperCase() + text.substring(1).toLowerCase();
|
||||
private static String getCleanDisplayName(Context context, String reportTypeName) {
|
||||
if (reportTypeName == null || reportTypeName.trim().isEmpty()) {
|
||||
return context.getString(R.string.accessibility_unknown);
|
||||
}
|
||||
|
||||
String cleanName = cleanReportTypeName(reportTypeName);
|
||||
return capitalizeFirstLetter(cleanName);
|
||||
}
|
||||
|
||||
private static int[] getColorsForRating(int rating) {
|
||||
// Returns [background, text, border] colors
|
||||
switch (rating) {
|
||||
case Constants.GOOD_RATING: // 3
|
||||
return new int[]{
|
||||
R.color.accessibility_good_bg,
|
||||
R.color.accessibility_good_text,
|
||||
R.color.accessibility_good_border
|
||||
};
|
||||
case Constants.AVERAGE_RATING: // 2
|
||||
return new int[]{
|
||||
R.color.accessibility_average_bg,
|
||||
R.color.accessibility_average_text,
|
||||
R.color.accessibility_average_border
|
||||
};
|
||||
case Constants.BAD_RATING: // 1
|
||||
return new int[]{
|
||||
R.color.accessibility_poor_bg,
|
||||
R.color.accessibility_poor_text,
|
||||
R.color.accessibility_poor_border
|
||||
};
|
||||
default:
|
||||
return new int[]{
|
||||
R.color.accessibility_default_bg,
|
||||
R.color.accessibility_default_text,
|
||||
R.color.accessibility_default_border
|
||||
};
|
||||
private static String capitalizeFirstLetter(String text) {
|
||||
if (text == null || text.isEmpty()) return text;
|
||||
|
||||
String[] words = text.split("\\s+");
|
||||
StringBuilder result = new StringBuilder();
|
||||
|
||||
for (int i = 0; i < words.length; i++) {
|
||||
if (i > 0) result.append(" ");
|
||||
String word = words[i];
|
||||
if (!word.isEmpty()) {
|
||||
result.append(Character.toUpperCase(word.charAt(0)));
|
||||
if (word.length() > 1) {
|
||||
result.append(word.substring(1).toLowerCase());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public static void clearCache() {
|
||||
typeCache.clear();
|
||||
}
|
||||
|
||||
public static RatingLevel getRatingLevel(int rating) {
|
||||
return RatingLevel.fromValue(rating);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue