forked from square/dagger
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide
SavedStateHandle
binding from ActivityRetainedComponent
.
RELNOTES=n/a PiperOrigin-RevId: 577983612
- Loading branch information
1 parent
6fe4a23
commit 1cac33b
Showing
21 changed files
with
545 additions
and
6 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
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
79 changes: 79 additions & 0 deletions
79
java/dagger/hilt/android/internal/managers/SavedStateHandleHolder.java
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,79 @@ | ||
/* | ||
* Copyright (C) 2023 The Dagger Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dagger.hilt.android.internal.managers; | ||
|
||
import static dagger.hilt.internal.Preconditions.checkNotNull; | ||
import static dagger.hilt.internal.Preconditions.checkState; | ||
|
||
import android.os.Bundle; | ||
import androidx.annotation.Nullable; | ||
import androidx.lifecycle.SavedStateHandle; | ||
import androidx.lifecycle.SavedStateHandleSupport; | ||
import androidx.lifecycle.viewmodel.CreationExtras; | ||
import androidx.lifecycle.viewmodel.MutableCreationExtras; | ||
import dagger.hilt.android.internal.ThreadUtil; | ||
|
||
/** Implementation for SavedStateHandleHolder. */ | ||
public final class SavedStateHandleHolder { | ||
private CreationExtras extras; | ||
private SavedStateHandle handle; | ||
private final boolean nonComponentActivity; | ||
|
||
SavedStateHandleHolder(@Nullable CreationExtras extras) { | ||
nonComponentActivity = (extras == null); | ||
this.extras = extras; | ||
} | ||
|
||
SavedStateHandle getSavedStateHandle() { | ||
ThreadUtil.ensureMainThread(); | ||
checkState( | ||
!nonComponentActivity, | ||
"Activity that does not extend ComponentActivity cannot use SavedStateHandle"); | ||
if (handle != null) { | ||
return handle; | ||
} | ||
checkNotNull( | ||
extras, | ||
"The first access to SavedStateHandle should happen between super.onCreate() and" | ||
+ " super.onDestroy()"); | ||
// Clean up default args, since those are unused and we don't want to duplicate those for each | ||
// SavedStateHandle | ||
MutableCreationExtras mutableExtras = new MutableCreationExtras(extras); | ||
mutableExtras.set(SavedStateHandleSupport.DEFAULT_ARGS_KEY, Bundle.EMPTY); | ||
extras = mutableExtras; | ||
handle = SavedStateHandleSupport.createSavedStateHandle(extras); | ||
|
||
extras = null; | ||
return handle; | ||
} | ||
|
||
public void clear() { | ||
extras = null; | ||
} | ||
|
||
public void setExtras(CreationExtras extras) { | ||
if (handle != null) { | ||
// If handle is already created, we don't need to store CreationExtras. | ||
return; | ||
} | ||
this.extras = extras; | ||
} | ||
|
||
public boolean isInvalid() { | ||
return handle == null && extras == null; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
java/dagger/hilt/android/internal/managers/SavedStateHandleModule.java
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,37 @@ | ||
/* | ||
* Copyright (C) 2023 The Dagger Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dagger.hilt.android.internal.managers; | ||
|
||
import androidx.lifecycle.SavedStateHandle; | ||
import dagger.Module; | ||
import dagger.Provides; | ||
import dagger.hilt.InstallIn; | ||
import dagger.hilt.android.components.ActivityRetainedComponent; | ||
import dagger.hilt.android.lifecycle.ActivityRetainedSavedState; | ||
import dagger.hilt.android.scopes.ActivityRetainedScoped; | ||
|
||
/** Module providing SavedStateHandle from ActivityRetainedComponent. */ | ||
@Module | ||
@InstallIn(ActivityRetainedComponent.class) | ||
abstract class SavedStateHandleModule { | ||
@ActivityRetainedSavedState | ||
@ActivityRetainedScoped | ||
@Provides | ||
static SavedStateHandle provideSavedStateHandle(SavedStateHandleHolder savedStateHandleHolder) { | ||
return savedStateHandleHolder.getSavedStateHandle(); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
java/dagger/hilt/android/lifecycle/ActivityRetainedSavedState.java
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,26 @@ | ||
/* | ||
* Copyright (C) 2023 The Dagger Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dagger.hilt.android.lifecycle; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Target; | ||
import javax.inject.Qualifier; | ||
|
||
/** Qualifies a binding that belongs to ActivityRetainedComponent. */ | ||
@Qualifier | ||
@Target({ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD}) | ||
public @interface ActivityRetainedSavedState {} |
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
Oops, something went wrong.