Skip to content

Commit

Permalink
Internal changes
Browse files Browse the repository at this point in the history
RELNOTES=n/a
PiperOrigin-RevId: 670672917
  • Loading branch information
wanyingd1996 authored and Dagger Team committed Oct 2, 2024
1 parent 18d2b26 commit e336bbd
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 35 deletions.
14 changes: 14 additions & 0 deletions java/dagger/hilt/android/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ android_library(
],
exports = [
":activity_retained_lifecycle",
":fragment_retained_lifecycle",
"//:dagger_with_compiler",
"//java/dagger/hilt:install_in",
"//java/dagger/hilt/android/components",
Expand Down Expand Up @@ -112,6 +113,17 @@ android_library(
],
)

android_library(
name = "fragment_retained_lifecycle",
srcs = [
"FragmentRetainedLifecycle.java",
],
deps = [
":package_info",
"//java/dagger/hilt/android/lifecycle:retained_lifecycle",
],
)

android_library(
name = "view_model_lifecycle",
srcs = [
Expand Down Expand Up @@ -190,6 +202,8 @@ gen_maven_artifact(
artifact_target_libs = [
":entry_point_accessors",
"//java/dagger/hilt/android:activity_retained_lifecycle",
"//java/dagger/hilt/android:fragment_retained_lifecycle",
"//java/dagger/hilt/android/internal/managers:internal_fragment_retained_component",
"//java/dagger/hilt/android:android_entry_point",
"//java/dagger/hilt/android:hilt_android_app",
"//java/dagger/hilt/android:unstable_api",
Expand Down
1 change: 1 addition & 0 deletions java/dagger/hilt/android/components/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ android_library(
"ActivityComponent.java",
"ActivityRetainedComponent.java",
"FragmentComponent.java",
"FragmentRetainedComponent.java",
"ServiceComponent.java",
"ViewComponent.java",
"ViewModelComponent.java",
Expand Down
1 change: 1 addition & 0 deletions java/dagger/hilt/android/internal/builders/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ android_library(
"//java/dagger/hilt:define_component",
"//java/dagger/hilt/android:view_model_lifecycle",
"//java/dagger/hilt/android/components",
"//java/dagger/hilt/android/internal/managers:internal_fragment_retained_component",
"//java/dagger/hilt/android/internal/managers:saved_state_handle_holder",
"@maven//:androidx_activity_activity",
"@maven//:androidx_fragment_fragment",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@
@DefineComponent.Builder
public interface FragmentComponentBuilder {
FragmentComponentBuilder fragment(@BindsInstance Fragment fragment);

FragmentComponent build();
}
1 change: 1 addition & 0 deletions java/dagger/hilt/android/internal/lifecycle/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ android_library(
"//java/dagger/hilt:entry_point",
"//java/dagger/hilt:install_in",
"//java/dagger/hilt/android:activity_retained_lifecycle",
"//java/dagger/hilt/android:fragment_retained_lifecycle",
"//java/dagger/hilt/android:view_model_lifecycle",
"//java/dagger/hilt/android/components",
"//java/dagger/hilt/android/internal",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
import java.util.Set;

/** Internal implementation. Do not use. */
public final class RetainedLifecycleImpl implements ActivityRetainedLifecycle, ViewModelLifecycle {
public final class RetainedLifecycleImpl
implements ActivityRetainedLifecycle,
ViewModelLifecycle {

private final Set<RetainedLifecycle.OnClearedListener> listeners = new HashSet<>();
private boolean onClearedDispatched = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ public <T extends ViewModel> T create(

@Override
public ActivityRetainedComponent generatedComponent() {
// TODO(wanyingd): don't need the component lock since the component is stored in ViewModel
// which does its own locking
if (component == null) {
synchronized (componentLock) {
if (component == null) {
Expand Down
15 changes: 14 additions & 1 deletion java/dagger/hilt/android/internal/managers/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ android_library(
"ApplicationComponentManager.java",
"BroadcastReceiverComponentManager.java",
"FragmentComponentManager.java",
"FragmentRetainedComponentManager.java",
"SavedStateHandleModule.java",
"ServiceComponentManager.java",
"ViewComponentManager.java",
],
exports = [":saved_state_handle_holder"],
exports = [
":internal_fragment_retained_component",
":saved_state_handle_holder",
],
deps = [
":component_supplier",
":saved_state_handle_holder",
Expand All @@ -43,6 +47,7 @@ android_library(
"//java/dagger/hilt:install_in",
"//java/dagger/hilt/android:activity_retained_lifecycle",
"//java/dagger/hilt/android:entry_point_accessors",
"//java/dagger/hilt/android:fragment_retained_lifecycle",
"//java/dagger/hilt/android:unstable_api",
"//java/dagger/hilt/android/components",
"//java/dagger/hilt/android/internal",
Expand All @@ -62,6 +67,14 @@ android_library(
],
)

android_library(
name = "internal_fragment_retained_component",
srcs = ["InternalFragmentRetainedComponent.java"],
deps = [
"//third_party/java/jsr330_inject",
],
)

android_library(
name = "saved_state_handle_holder",
srcs = ["SavedStateHandleHolder.java"],
Expand Down
1 change: 1 addition & 0 deletions java/dagger/hilt/android/scopes/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ android_library(
srcs = [
"ActivityRetainedScoped.java",
"ActivityScoped.java",
"FragmentRetainedScoped.java",
"FragmentScoped.java",
"ServiceScoped.java",
"ViewModelScoped.java",
Expand Down
53 changes: 20 additions & 33 deletions javatests/dagger/hilt/android/EntryPointAccessorsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@ import com.google.common.truth.Truth
import dagger.Module
import dagger.Provides
import dagger.hilt.EntryPoint
import dagger.hilt.EntryPoints
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ActivityComponent
import dagger.hilt.android.components.FragmentComponent
import dagger.hilt.android.components.FragmentRetainedComponent
import dagger.hilt.android.components.ViewComponent
import dagger.hilt.android.internal.managers.InternalFragmentRetainedComponent
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import dagger.hilt.android.testing.HiltTestApplication
import dagger.hilt.components.SingletonComponent
import javax.inject.Inject
import javax.inject.Qualifier
import org.junit.Rule
import org.junit.Test
Expand All @@ -55,24 +59,15 @@ class EntryPointAccessorsTest {
const val VIEW_STRING = "VIEW_STRING"
}

@get:Rule
var rule = HiltAndroidRule(this)
@get:Rule var rule = HiltAndroidRule(this)

@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class ApplicationLevel
@Qualifier @Retention(AnnotationRetention.BINARY) annotation class ApplicationLevel

@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class ActivityLevel
@Qualifier @Retention(AnnotationRetention.BINARY) annotation class ActivityLevel

@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class FragmentLevel
@Qualifier @Retention(AnnotationRetention.BINARY) annotation class FragmentLevel

@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class ViewLevel
@Qualifier @Retention(AnnotationRetention.BINARY) annotation class ViewLevel

@Module
@InstallIn(SingletonComponent::class)
Expand Down Expand Up @@ -117,50 +112,43 @@ class EntryPointAccessorsTest {
@EntryPoint
@InstallIn(SingletonComponent::class)
internal interface ApplicationEntryPoint {
@ApplicationLevel
fun getString(): String
@ApplicationLevel fun getString(): String
}

@EntryPoint
@InstallIn(ActivityComponent::class)
internal interface ActivityEntryPoint {
@ActivityLevel
fun getString(): String
@ActivityLevel fun getString(): String
}

@EntryPoint
@InstallIn(FragmentComponent::class)
internal interface FragmentEntryPoint {
@FragmentLevel
fun getString(): String
@FragmentLevel fun getString(): String
}

@EntryPoint
@InstallIn(ViewComponent::class)
internal interface ViewEntryPoint {
@ViewLevel
fun getString(): String
@ViewLevel fun getString(): String
}

@Test
fun testApplicationEntryPoint() {
val app = getApplicationContext<HiltTestApplication>()
val entryPoint = EntryPointAccessors.fromApplication<ApplicationEntryPoint>(app)
Truth.assertThat(entryPoint.getString())
.isEqualTo(APPLICATION_STRING)
Truth.assertThat(entryPoint.getString()).isEqualTo(APPLICATION_STRING)

val activity = Robolectric.buildActivity(TestActivity::class.java).setup().get()
val applicationEntryPoint = EntryPointAccessors.fromApplication<ApplicationEntryPoint>(activity)
Truth.assertThat(applicationEntryPoint.getString())
.isEqualTo(APPLICATION_STRING)
Truth.assertThat(applicationEntryPoint.getString()).isEqualTo(APPLICATION_STRING)
}

@Test
fun testActivityEntryPoint() {
val activity = Robolectric.buildActivity(TestActivity::class.java).setup().get()
val entryPoint = EntryPointAccessors.fromActivity<ActivityEntryPoint>(activity)
Truth.assertThat(entryPoint.getString())
.isEqualTo(ACTIVITY_STRING)
Truth.assertThat(entryPoint.getString()).isEqualTo(ACTIVITY_STRING)
}

@Test
Expand All @@ -169,24 +157,23 @@ class EntryPointAccessorsTest {
val fragment = TestFragment()
activity.supportFragmentManager.beginTransaction().add(fragment, "").commitNow()
val entryPoint = EntryPointAccessors.fromFragment<FragmentEntryPoint>(fragment)
Truth.assertThat(entryPoint.getString())
.isEqualTo(FRAGMENT_STRING)
Truth.assertThat(entryPoint.getString()).isEqualTo(FRAGMENT_STRING)
}

@Test
fun testViewEntryPoint() {
val activity = Robolectric.buildActivity(TestActivity::class.java).setup().get()
val view = TestView(activity)
val entryPoint = EntryPointAccessors.fromView<ViewEntryPoint>(view)
Truth.assertThat(entryPoint.getString())
.isEqualTo(VIEW_STRING)
Truth.assertThat(entryPoint.getString()).isEqualTo(VIEW_STRING)
}

@AndroidEntryPoint(FragmentActivity::class)
class TestActivity : Hilt_EntryPointAccessorsTest_TestActivity()

@AndroidEntryPoint(Fragment::class)
class TestFragment : Hilt_EntryPointAccessorsTest_TestFragment()
class TestFragment : Hilt_EntryPointAccessorsTest_TestFragment() {
}

@AndroidEntryPoint(View::class)
class TestView(context: Context) : Hilt_EntryPointAccessorsTest_TestView(context)
Expand Down

0 comments on commit e336bbd

Please sign in to comment.