1818using System . Collections . Generic ;
1919using System . Threading . Tasks ;
2020using Firebase . Extensions ;
21+ using Firebase . RemoteConfig ;
2122
2223namespace Hamster
2324{
@@ -178,7 +179,7 @@ private void InitializeFirebaseAndStartGame()
178179 if ( dependencyStatus == Firebase . DependencyStatus . Available ) {
179180 // Create and hold a reference to your FirebaseApp,
180181 app = Firebase . FirebaseApp . DefaultInstance ;
181- InitializeCommonDataAndStartGame ( ) ;
182+ SetRemoteConfigDefaults ( ) ;
182183 } else {
183184 UnityEngine . Debug . LogError (
184185 $ "Could not resolve all Firebase dependencies: { dependencyStatus } \n " +
@@ -191,21 +192,61 @@ private void InitializeFirebaseAndStartGame()
191192 // before starting the game.
192193 private void SetRemoteConfigDefaults ( )
193194 {
194- throw new System . NotImplementedException ( ) ;
195+ var defaults = new System . Collections . Generic . Dictionary < string , object > ( ) ;
196+ defaults . Add (
197+ Hamster . MapObjects . AccelerationTile . AccelerationTileForceKey ,
198+ Hamster . MapObjects . AccelerationTile . AccelerationTileForceDefault ) ;
199+ defaults . Add (
200+ Hamster . States . MainMenu . SubtitleOverrideKey ,
201+ Hamster . States . MainMenu . SubtitleOverrideDefault ) ;
202+ var remoteConfig = FirebaseRemoteConfig . DefaultInstance ;
203+ remoteConfig . SetDefaultsAsync ( defaults ) . ContinueWithOnMainThread (
204+ previousTask =>
205+ {
206+ FetchRemoteConfig ( InitializeCommonDataAndStartGame ) ;
207+ }
208+ ) ;
195209 }
196210
197211 // (Re)fetches Remote Config values and pass down the onFetchAndActivateSuccessful callback.
198212 // Called during the initialization flow but can also be called indepedently.
199213 public void FetchRemoteConfig ( System . Action onFetchAndActivateSuccessful )
200214 {
201- throw new System . NotImplementedException ( ) ;
215+ if ( app == null )
216+ {
217+ Debug . LogError ( $ "Do not use Firebase until it is properly initialized by calling { nameof ( InitializeFirebaseAndStartGame ) } .") ;
218+ return ;
219+ }
220+
221+ Debug . Log ( "Fetching data..." ) ;
222+ var remoteConfig = FirebaseRemoteConfig . DefaultInstance ;
223+ remoteConfig . FetchAsync ( System . TimeSpan . Zero ) . ContinueWithOnMainThread (
224+ previousTask=>
225+ {
226+ if ( ! previousTask . IsCompleted )
227+ {
228+ Debug . LogError ( $ "{ nameof ( remoteConfig . FetchAsync ) } incomplete: Status '{ previousTask . Status } '") ;
229+ return ;
230+ }
231+ ActivateRetrievedRemoteConfigValues ( onFetchAndActivateSuccessful ) ;
232+ } ) ;
202233 }
203234
204235 // The final method in the initialization flow that will activate fetched values
205236 // and on Success will call onFetchAndActivateSuccessful.
206237 private void ActivateRetrievedRemoteConfigValues ( System . Action onFetchAndActivateSuccessful )
207238 {
208- throw new System . NotImplementedException ( ) ;
239+ var remoteConfig = FirebaseRemoteConfig . DefaultInstance ;
240+ var info = remoteConfig . Info ;
241+ if ( info . LastFetchStatus == LastFetchStatus . Success )
242+ {
243+ remoteConfig . ActivateAsync ( ) . ContinueWithOnMainThread (
244+ previousTask =>
245+ {
246+ Debug . Log ( $ "Remote data loaded and ready (last fetch time { info . FetchTime } ).") ;
247+ onFetchAndActivateSuccessful ( ) ;
248+ } ) ;
249+ }
209250 }
210251 }
211252}
0 commit comments