-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Preload icons #177
base: main
Are you sure you want to change the base?
Preload icons #177
Conversation
This action runs the formatter for us if we happen to forget to run it before pushing to main or PR. * Update main.yml * Formatted code Co-authored-by: McFrappe <[email protected]>
@@ -40,6 +41,10 @@ const App = () => { | |||
setCustomText(customTextProps) | |||
setCustomTextInput(customTextProps) | |||
|
|||
function cacheFonts(fonts: { [x: string]: any }) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should fonts
really be of this type? Since you are using map
I would assume it is an array of strings (Array<string>
)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Expos own type definition is [x: string]: any
for Icon.font
, so if i would change cacheFonts
to Array<String>
fontAssets
complains about the type :/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, that seems weird, especially since you pass in an array of fonts to the function on line 69?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah i know, i believe expo forced this type for some reason.
|
||
// Preload Icons | ||
|
||
const fontAssets = cacheFonts([ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should also consider only using one icon pack. Loading three different icon packs seems inefficient (this can be done in a future in a separate PR, unless it is an easy fix, of course)
return ( | ||
<> | ||
{Icon} | ||
|
||
{data.length > 0 && ( | ||
{data != undefined && ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it not be better to have both of these conditions set to true before rendering?
{data != undefined && ( | |
{data && data.length > 0 && ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, i forgot i removed the data.length.
> | ||
{children} | ||
</PushTokenContext.Provider> | ||
<> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will prevent rendering of the app when the token is not defined (?), and this is not the the behaviour that we want. I think it is better to allow this to be null and instead check this in the components that actually use the token.
For example, in the notification badge you can add a check after extracting the token from the context and check whether it is null or not. If that is the case, simply return null and skip the rendering of the component. That should work, as long as the check happens before the useNotifications
hook.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would mean that the useNotifications hook is depentent on if-statement, why would that not be illegal?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you are right, it would be illegal. I thought it was okay to do this as long as the ordering of hooks remained the same. Apparently, this is not the case. I think it might work, but it is not actually allowed and might cause bugs :)
I suppose the solution would be to move the actual rendering of the count to a separate component?
Good catch 👍🏼
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can look some more at this one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you are right, it would be illegal. I thought it was okay to do this as long as the ordering of hooks remained the same. Apparently, this is not the case. I think it might work, but it is not actually allowed and might cause bugs :)
I suppose the solution would be to move the actual rendering of the count to a separate component?
Good catch 👍🏼
Check latest commit, it sets a loading state in the async function, and returns the components once the functions loading state is false. It ensures that the component render, even if the token might be null, and at the same time ensures that the component doesn't render before the fetch is completed. This removes the previous API-errors of trying to fetch a null token, and react doesn't complain on illegal hook usage.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It leads though to a 500ms longer start of the app due to a token being forced to finish fetching before render, but that might be fixed with a longer splash screen. An (mabye ugly) fix for this would be to call useToken() in App.tsx so to set the token state, and when it's finished we then render the app. Using this method would then make the latest commit unnessecary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having a slightly longer startup time is okay I guess. Since the app is actually running, we should be able to display some sort of loading indicator while the token is being fetched.
@@ -36,13 +37,11 @@ export const BadgedIcon = ({ name, showNum, color }: BadgeProps) => { | |||
return Icon |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check is not necessary now I believe, the Icon is returned regardless
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reverted the length check
components/PushTokenContext.tsx
Outdated
@@ -32,7 +32,7 @@ Notifications.setNotificationHandler({ | |||
}), | |||
}) | |||
|
|||
async function registerForPushNotificationsAsync() { | |||
async function registerForPushNotificationsAsync(setLoading) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it not be better to call setLoading
in the then(() => ...)
callback instead (line 84)? Then you do not need any extra arguments and you will update all the state at the same place.
components/PushTokenContext.tsx
Outdated
@@ -98,17 +99,21 @@ export const PushTokenProvider = ({ children }: Props) => { | |||
Notifications.removeNotificationSubscription(notificationListener.current) | |||
Notifications.removeNotificationSubscription(responseListener.current) | |||
} | |||
}, []) | |||
}, [token]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this effect-hook dependency really needed? The actual token is fetched inside the effect-hook and running the effect when the token changes seems weird (why would we fetch the token if we already have a token set?).
Running this effect on mount only ([]
) seems more appropriate imo.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, i dont know why this is here and why i added it. Will revert.
> | ||
{children} | ||
</PushTokenContext.Provider> | ||
<> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having a slightly longer startup time is okay I guess. Since the app is actually running, we should be able to display some sort of loading indicator while the token is being fetched.
components/PushTokenContext.tsx
Outdated
{children} | ||
</PushTokenContext.Provider> | ||
<> | ||
{!loading && ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a loading indicator when loading
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea. Will add.
This PR preloads the icons of the homepage during the splash screen.
Also fixes a bug where the badge in notifications renders before the api-async is finished.