Skip to content

Commit

Permalink
Merge pull request #135 from Reveille-Rides/bwees/favorite-migration
Browse files Browse the repository at this point in the history
Fixes #135
  • Loading branch information
bwees authored Feb 15, 2024
2 parents 80131c5 + 462f625 commit 6b46861
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/components/sheets/RouteDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ const RouteDetails: React.FC<SheetProps> = ({ sheetRef }) => {
</View>

<View style={{ flexDirection: "row", alignItems: 'center', marginBottom: 8, marginLeft: 16, gap: 4 }}>
<FavoritePill routeId={selectedRoute!.key} />
<FavoritePill routeShortName={selectedRoute!.shortName} />
<AlertPill routeId={selectedRoute!.key} />
</View>

Expand Down
34 changes: 32 additions & 2 deletions app/components/sheets/RoutesList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,39 @@ const RoutesList: React.FC<SheetProps> = ({ sheetRef }) => {
AsyncStorage.getItem('favorites').then((favorites: string | null) => {
if (!favorites) return;

const favoritesArray = JSON.parse(favorites);
var favoritesArray = JSON.parse(favorites);

// uuid regex
const uuidRegex = new RegExp("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$");

if (favoritesArray.some((fav: string) => uuidRegex.test(fav))) {
console.log("Found a uuid in favorited, running migration steps...")

// convert any uuids (based on regex) to the new route shortName
favoritesArray = favoritesArray.map((fav: string) => {
// check if the favorite is a uuid
if (uuidRegex.test(fav)) {
const match = routes.find(route => route.key === fav);

return match ? match.shortName : null; // return null if the route is not found
} else {
// otherwise return the favorite
return fav;
}
})

// remove any undefined values
favoritesArray = favoritesArray.filter((el: string | undefined) => el !== null);

// deduplicate the array
favoritesArray = [...new Set(favoritesArray)];

// save the converted favorites to AsyncStorage
AsyncStorage.setItem('favorites', JSON.stringify(favoritesArray));
}

setFavoriteRoutes(routes.filter(route => favoritesArray.includes(route.key)));
// set the favorite routes
setFavoriteRoutes(routes.filter(route => favoritesArray.includes(route.shortName)));
})
}

Expand Down
18 changes: 9 additions & 9 deletions app/components/ui/FavoritePill.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import IconPill from './IconPill'
import useAppStore from '../../stores/useAppStore';

interface Props {
routeId: string
routeShortName: string
}

const FavoritePill: React.FC<Props> = ({ routeId }) => {
const FavoritePill: React.FC<Props> = ({ routeShortName }) => {
const [isFavorite, setIsFavorite] = useState(false);
const theme = useAppStore((state) => state.theme);

Expand All @@ -20,7 +20,7 @@ const FavoritePill: React.FC<Props> = ({ routeId }) => {

const favoritesArray = JSON.parse(favorites);

setIsFavorite(favoritesArray.includes(routeId));
setIsFavorite(favoritesArray.includes(routeShortName));
})
} catch(error) {
console.error(error);
Expand All @@ -40,22 +40,22 @@ const FavoritePill: React.FC<Props> = ({ routeId }) => {
if (!savedFavorites && !newState) return;

try {
// If no favorites exist and we are adding favorite, create a new array with the routeId
// If no favorites exist and we are adding favorite, create a new array with the routeShortName
if (!savedFavorites && newState) {
setIsFavorite(true);
return AsyncStorage.setItem('favorites', JSON.stringify([routeId]));
return AsyncStorage.setItem('favorites', JSON.stringify([routeShortName]));
}

// If favorites exist and we are adding favorite, add routeId to array
// If favorites exist and we are adding favorite, add routeShortName to array
if (newState) {
const favoritesArray = JSON.parse(savedFavorites!);
favoritesArray.push(routeId);
favoritesArray.push(routeShortName);

AsyncStorage.setItem('favorites', JSON.stringify(favoritesArray));
} else {
// If favorites exist and we are removing favorite, remove routeId from array
// If favorites exist and we are removing favorite, remove routeShortName from array
const favoritesArray = JSON.parse(savedFavorites!);
const newFavoritesArray = favoritesArray.filter((id: string) => id !== routeId);
const newFavoritesArray = favoritesArray.filter((id: string) => id !== routeShortName);

AsyncStorage.setItem('favorites', JSON.stringify(newFavoritesArray));
}
Expand Down

0 comments on commit 6b46861

Please sign in to comment.