diff options
Diffstat (limited to 'javatests/artifacts/hilt-android/viewmodel/app/src/main/java/dagger/hilt/viewmodel')
5 files changed, 173 insertions, 0 deletions
diff --git a/javatests/artifacts/hilt-android/viewmodel/app/src/main/java/dagger/hilt/viewmodel/SimpleActivity.java b/javatests/artifacts/hilt-android/viewmodel/app/src/main/java/dagger/hilt/viewmodel/SimpleActivity.java new file mode 100644 index 000000000..ad6e73899 --- /dev/null +++ b/javatests/artifacts/hilt-android/viewmodel/app/src/main/java/dagger/hilt/viewmodel/SimpleActivity.java @@ -0,0 +1,50 @@ +/* + * 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.viewmodel; + +import android.os.Bundle; +import android.widget.TextView; +import androidx.annotation.OptIn; +import androidx.appcompat.app.AppCompatActivity; +import androidx.lifecycle.SavedStateHandle; +import androidx.lifecycle.ViewModelProvider; +import dagger.hilt.android.AndroidEntryPoint; +import dagger.hilt.android.UnstableApi; +import dagger.hilt.android.lifecycle.ActivityRetainedSavedState; +import javax.inject.Inject; + +/** The main activity of the application. */ +@OptIn(markerClass = UnstableApi.class) +@AndroidEntryPoint +public class SimpleActivity extends AppCompatActivity { + private static final String TAG = SimpleActivity.class.getSimpleName(); + + @Inject + @ActivityRetainedSavedState + SavedStateHandle savedStateHandle; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + SimpleViewModel viewModel = new ViewModelProvider(this).get(SimpleViewModel.class); + savedStateHandle.set("some_key", "some_content"); + setContentView(R.layout.activity_main); + + ((TextView) findViewById(R.id.greeting)) + .setText(getResources().getString(R.string.welcome, viewModel.userName)); + } +} diff --git a/javatests/artifacts/hilt-android/viewmodel/app/src/main/java/dagger/hilt/viewmodel/SimpleApplication.java b/javatests/artifacts/hilt-android/viewmodel/app/src/main/java/dagger/hilt/viewmodel/SimpleApplication.java new file mode 100644 index 000000000..eed62b175 --- /dev/null +++ b/javatests/artifacts/hilt-android/viewmodel/app/src/main/java/dagger/hilt/viewmodel/SimpleApplication.java @@ -0,0 +1,27 @@ +/* + * 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.viewmodel; + +import android.app.Application; +import dagger.hilt.android.HiltAndroidApp; + +/** + * A simple, skeletal application that demonstrates a dependency-injected application using the + * utilities in {@code Hilt} in Android. + */ +@HiltAndroidApp +public class SimpleApplication extends Application {} diff --git a/javatests/artifacts/hilt-android/viewmodel/app/src/main/java/dagger/hilt/viewmodel/SimpleViewModel.java b/javatests/artifacts/hilt-android/viewmodel/app/src/main/java/dagger/hilt/viewmodel/SimpleViewModel.java new file mode 100644 index 000000000..0509d233a --- /dev/null +++ b/javatests/artifacts/hilt-android/viewmodel/app/src/main/java/dagger/hilt/viewmodel/SimpleViewModel.java @@ -0,0 +1,32 @@ +/* + * 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.viewmodel; + +import androidx.lifecycle.ViewModel; +import dagger.hilt.android.lifecycle.HiltViewModel; +import javax.inject.Inject; + +/** The view model requires creation by hilt. */ +@HiltViewModel +public final class SimpleViewModel extends ViewModel { + String userName; + + @Inject + SimpleViewModel(@UserName String userName) { + this.userName = userName; + } +} diff --git a/javatests/artifacts/hilt-android/viewmodel/app/src/main/java/dagger/hilt/viewmodel/UserName.java b/javatests/artifacts/hilt-android/viewmodel/app/src/main/java/dagger/hilt/viewmodel/UserName.java new file mode 100644 index 000000000..6bed1cbc6 --- /dev/null +++ b/javatests/artifacts/hilt-android/viewmodel/app/src/main/java/dagger/hilt/viewmodel/UserName.java @@ -0,0 +1,29 @@ +/* + * 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.viewmodel; + +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import javax.inject.Qualifier; + +/** Qualifies bindings relating to the user name. */ +@Qualifier +@Retention(RUNTIME) +@Documented +@interface UserName {} diff --git a/javatests/artifacts/hilt-android/viewmodel/app/src/main/java/dagger/hilt/viewmodel/UserNameModule.java b/javatests/artifacts/hilt-android/viewmodel/app/src/main/java/dagger/hilt/viewmodel/UserNameModule.java new file mode 100644 index 000000000..e1d5bd547 --- /dev/null +++ b/javatests/artifacts/hilt-android/viewmodel/app/src/main/java/dagger/hilt/viewmodel/UserNameModule.java @@ -0,0 +1,35 @@ +/* + * 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.viewmodel; + +import dagger.Module; +import dagger.Provides; +import dagger.hilt.InstallIn; +import dagger.hilt.android.components.ViewModelComponent; +import java.util.Random; + +@Module +@InstallIn(ViewModelComponent.class) +final class UserNameModule { + @UserName + @Provides + static String provideUserName() { + return "ProdUser-" + new Random().nextInt(); + } + + private UserNameModule() {} +} |