Skip to content
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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open

Preload icons #177

wants to merge 10 commits into from

Conversation

RobertMartinis
Copy link
Contributor

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.

McFrappe and others added 6 commits June 17, 2021 16:12
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 }) {
Copy link
Member

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>)?

Copy link
Contributor Author

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 :/

Copy link
Member

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?

Copy link
Contributor Author

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([
Copy link
Member

@Frewacom Frewacom Jun 18, 2021

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 && (
Copy link
Member

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?

Suggested change
{data != undefined && (
{data && data.length > 0 && (

Copy link
Contributor Author

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>
<>
Copy link
Member

@Frewacom Frewacom Jun 18, 2021

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.

Copy link
Member

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?

Copy link
Member

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 👍🏼

Copy link
Contributor Author

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.

Copy link
Contributor Author

@RobertMartinis RobertMartinis Jun 18, 2021

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.

Copy link
Contributor Author

@RobertMartinis RobertMartinis Jun 18, 2021

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.

Copy link
Member

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
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted the length check

@@ -32,7 +32,7 @@ Notifications.setNotificationHandler({
}),
})

async function registerForPushNotificationsAsync() {
async function registerForPushNotificationsAsync(setLoading) {
Copy link
Member

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.

@@ -98,17 +99,21 @@ export const PushTokenProvider = ({ children }: Props) => {
Notifications.removeNotificationSubscription(notificationListener.current)
Notifications.removeNotificationSubscription(responseListener.current)
}
}, [])
}, [token])
Copy link
Member

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.

Copy link
Contributor Author

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>
<>
Copy link
Member

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.

{children}
</PushTokenContext.Provider>
<>
{!loading && (
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. Will add.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants