This repository has been archived by the owner on Jun 15, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' of github.com:standardnotes/mobile
- Loading branch information
Showing
8 changed files
with
174 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import NetInfo, { NetInfoConfiguration } from '@react-native-community/netinfo'; | ||
import { removeFromArray } from '@standardnotes/snjs'; | ||
import React, { useEffect, useState } from 'react'; | ||
|
||
export const useIsOffline = () => { | ||
const networkService = React.useContext(NetworkServiceContext); | ||
const [isOffline, setIsOffline] = useState(false); | ||
|
||
useEffect(() => { | ||
const removeOfflineObserver = networkService!.addNetworkChangeObserver( | ||
networkState => | ||
setIsOffline( | ||
!networkState.isConnected || !networkState.isInternetReachable | ||
) | ||
); | ||
|
||
return () => { | ||
removeOfflineObserver && removeOfflineObserver(); | ||
}; | ||
}, [networkService]); | ||
|
||
return [isOffline]; | ||
}; | ||
|
||
export const NetworkServiceContext = React.createContext< | ||
NetworkService | undefined | ||
>(undefined); | ||
|
||
type NetworkState = { | ||
/** | ||
* If there is an active network connection. If unknown defaults to null. | ||
*/ | ||
isConnected: boolean | null; | ||
/** | ||
* If the internet is reachable with the currently active network connection. | ||
* If unknown defaults to null. | ||
*/ | ||
isInternetReachable: boolean | null; | ||
}; | ||
|
||
type NetworkChangeObserver = ( | ||
networkState: NetworkState | ||
) => Promise<void> | void; | ||
|
||
export class NetworkService { | ||
private observers: NetworkChangeObserver[] = []; | ||
private removeNetworkChangeListener?: () => void; | ||
|
||
constructor(configuration?: NetInfoConfiguration) { | ||
if (configuration) { | ||
NetInfo.configure({ | ||
...configuration, | ||
}); | ||
} | ||
} | ||
|
||
deinit() { | ||
if (this.removeNetworkChangeListener) { | ||
this.removeNetworkChangeListener(); | ||
this.removeNetworkChangeListener = undefined; | ||
} | ||
this.observers.length = 0; | ||
} | ||
|
||
async registerObservers() { | ||
this.removeNetworkChangeListener = NetInfo.addEventListener( | ||
({ isConnected, isInternetReachable }) => { | ||
this.notifyObserversOfNetworkChange({ | ||
isConnected, | ||
isInternetReachable, | ||
}); | ||
} | ||
); | ||
} | ||
|
||
/** | ||
* Registers an observer for network change | ||
* @returns function that unregisters this observer | ||
*/ | ||
public addNetworkChangeObserver(callback: NetworkChangeObserver) { | ||
// Sets initial values. | ||
NetInfo.fetch().then(({ isConnected, isInternetReachable }) => { | ||
callback({ | ||
isConnected, | ||
isInternetReachable, | ||
}); | ||
}); | ||
|
||
// Execute callback on subsequent network state changes. | ||
this.observers.push(callback); | ||
|
||
return () => { | ||
removeFromArray(this.observers, callback); | ||
}; | ||
} | ||
|
||
private notifyObserversOfNetworkChange(networkState: NetworkState) { | ||
for (const observer of this.observers) { | ||
observer(networkState); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters