Skip to content

A sample of Android development best practices with Android Jetpack programming by Java.

Notifications You must be signed in to change notification settings

cuiyiming007/sunflower-java

Repository files navigation

Android Sunflower Java

A gardening app illustrating Android development best practices with Android Jetpack in JAVA.

The google official sample is in KOTLIN.

Releases:

Version Description
v1.3.0 improve performance and a elegant way to inject params like intent of activity
v1.2.0 another simple and better way to inject ViewModel
v1.1.0 using dagger2 as di, add LongClickListener to delete plant
v1.0.0 android architecture in Java

There is a detailed introduction in my Blog - Android Sunflower + Dagger2. It's in Chinese.

A brief description between v1.1.0 and v1.2.0

In v1.1.0, I reference the Google Official Sample GithubBrowserSample. Re-write the di codes from Kotlin to Java.
To inject ViewModels, we’ve to create a singleton factory that was supplied with a map of ViewModel-based classes and their respective Providers. It required us to create a custom ViewModelKey annotation and use Dagger to generate the map in a module using IntoMap bindings.
There is the code snippet,
ViewModelFactory:

@Singleton
public class ViewModelProviderFactory extends ViewModelProvider.NewInstanceFactory {

    Map<Class<? extends ViewModel>, Provider<ViewModel>> creators;

    @Inject
    public ViewModelProviderFactory(Map<Class<? extends ViewModel>, Provider<ViewModel>> creators) {
        this.creators = creators;
    }

    @NonNull
    @Override
    public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
        Provider<ViewModel> creator = creators.get(modelClass);
        if (creator == null) {
            for (Map.Entry<Class<? extends ViewModel>, Provider<ViewModel>> entry : creators.entrySet()) {
                if (modelClass.isAssignableFrom(entry.getKey())) {
                    creator = entry.getValue();
                    break;
                }
            }
        }

        if (creator == null) {
            throw new IllegalArgumentException("unknown model class " + modelClass);
        }
        return (T) creator.get();
    }
}

ViewModelKey:

@MapKey
@interface ViewModelKey {
    Class<? extends ViewModel> value();
}

ViewModelModule:

@Module
abstract class ViewModelModule {
    @Binds
    @IntoMap
    @ViewModelKey(GardenPlantingListViewModel.class)
    abstract ViewModel bindGardenPlantingListViewModel(GardenPlantingListViewModel gardenPlantingListViewModel);

    @Binds
    @IntoMap
    @ViewModelKey(PlantDetailViewModel.class)
    abstract ViewModel bindPlantDetailViewModel(PlantDetailViewModel plantDetailViewModel);

    @Binds
    @IntoMap
    @ViewModelKey(PlantListViewModel.class)
    abstract ViewModel bindPlantListViewModel(PlantListViewModel plantListViewModel);

    @Binds
    abstract ViewModelProvider.Factory bindViewModelFactory(ViewModelProviderFactory factory);
}

In v1.2.0, we only need to write a generic ViewModel factory class of which instances are created for each Activity or Fragment instance.
There is the code snippet,
ViewModelFactory:

@Singleton
public class ViewModelSimpleFactory<VM extends ViewModel> extends ViewModelProvider.NewInstanceFactory {

    private Lazy<VM> viewModel;
    @Inject
    public ViewModelSimpleFactory(Lazy<VM> viewModel) {
        this.viewModel = viewModel;
    }

    @NonNull
    @Override
    public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
        return (T) viewModel.get();
    }
}

Activity or Fragment:

public class GardenFragment extends Fragment implements Injectable {
    @Inject
    public ViewModelSimpleFactory<GardenPlantingListViewModel> factory;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //...
        GardenPlantingListViewModel viewModel = ViewModelProviders.of(this, factory).get(GardenPlantingListViewModel.class);
        //...
    }
}

That's all!

v1.3.0 A elegant way to inject Params like Intent of Activity

In v1.3.0, I improve the performance of Dagger reference this article- Keeping the Daggers Sharp.

And I find a way to Inject variable params elegantly - using AssistedInject library.

First, add dependencies:

compileOnly 'com.squareup.inject:assisted-inject-annotations-dagger2:0.4.0'
annotationProcessor 'com.squareup.inject:assisted-inject-processor-dagger2:0.4.0'

Second, annotated with @AssistedInject instead of @Inject, annotate the variable params as @Assisted, create a factory interface annotated with @AssistedInject.Factory.

PlantDetailViewModel:

public class PlantDetailViewModel extends ViewModel {

    @AssistedInject
    public PlantDetailViewModel(PlantRepository plantRepository, GardenPlantingRepository gardenPlantingRepository,@Assisted String plantId) {
    }

    @AssistedInject.Factory
    public interface Factory {
        PlantDetailViewModel create(String plantId);
    }
}

Third, create a module or add it in a exist module to include the Factory in the graph.

GardenActivityModule:

@AssistedModule
@Module(includes = {AssistedInject_GardenActivityModule.class})
abstract class GardenActivityModule {
    //...
}

Fourth, in your Activity or Fragement:

public class PlantDetailFragment extends Fragment implements Injectable {

    @Inject
    public PlantDetailViewModel.Factory factory2;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        String plantId = PlantDetailFragmentArgs.fromBundle(getArguments()).getPlantId();

        PlantDetailViewModel plantDetailViewModel = factory2.create(plantId);
    }
}

That's all.

But this is not good enough. The ViewModle should provide by the ViewModelProvider.Factory, I don't know how to implement it. If you hava an idea, please tell me.


CircleCI

A gardening app illustrating Android development best practices with Android Jetpack.

Android Sunflower is currently released as an alpha and is under heavy development. To view the latest changes, please visit the Releases page. Note that some changes (such as database schema modifications) are not backwards compatible during this alpha period and may cause the app to crash. In this case, please uninstall and re-install the app.

Introduction

Android Jetpack is a set of components, tools and guidance to make great Android apps. They bring together the existing Support Library and Architecture Components and arranges them into four categories:

Android Jetpack

Android Sunflower demonstrates utilizing these components to create a simple gardening app. Read the Introducing Android Sunflower article for a walkthrough of the app.

Getting Started

This project uses the Gradle build system. To build this project, use the gradlew build command or use "Import Project" in Android Studio.

There are two Gradle tasks for testing the project:

  • connectedAndroidTest - for running Espresso on a connected device
  • test - for running unit tests

For more resources on learning Android development, visit the Developer Guides at developer.android.com.

Screenshots

List of plants Plant details My Garden

Libraries Used

  • Foundation - Components for core system capabilities, Kotlin extensions and support for multidex and automated testing.
    • AppCompat - Degrade gracefully on older versions of Android.
    • Android KTX - Write more concise, idiomatic Kotlin code.
    • Test - An Android testing framework for unit and runtime UI tests.
  • Architecture - A collection of libraries that help you design robust, testable, and maintainable apps. Start with classes for managing your UI component lifecycle and handling data persistence.
    • Data Binding - Declaratively bind observable data to UI elements.
    • Lifecycles - Create a UI that automatically responds to lifecycle events.
    • LiveData - Build data objects that notify views when the underlying database changes.
    • Navigation - Handle everything needed for in-app navigation.
    • Room - Access your app's SQLite database with in-app objects and compile-time checks.
    • ViewModel - Store UI-related data that isn't destroyed on app rotations. Easily schedule asynchronous tasks for optimal execution.
    • WorkManager - Manage your Android background jobs.
  • UI - Details on why and how to use UI Components in your apps - together or separate
  • Third party
    • Glide for image loading
    • Kotlin Coroutines for managing background threads with simplified code and reducing needs for callbacks

Upcoming features

Updates will include incorporating additional Jetpack components and updating existing components as the component libraries evolve.

Interested in seeing a particular feature of the Android Framework or Jetpack implemented in this app? Please open a new issue.

Android Studio IDE setup

For development, the latest version of Android Studio is required. The latest version can be downloaded from here.

Sunflower uses ktlint to enforce Kotlin coding styles. Here's how to configure it for use with Android Studio (instructions adapted from the ktlint README):

  • Close Android Studio if it's open

  • Download ktlint using these installation instructions

  • Inside the project root directory run:

    ./ktlint --apply-to-idea-project --android

  • Start Android Studio

Additional resources

Check out these Wiki pages to learn more about Android Sunflower:

Non-Goals

The focus of this project is on Android Jetpack and the Android framework. Thus, there are no immediate plans to implement features outside of this scope.

A note on dependency injection - while many projects (such as Plaid) use Dagger 2 for DI, there are no plans to incorporate DI into Sunflower. This allows developers unfamiliar with dependency injection to better understand Sunflower's code without having to learn DI.

Support

If you've found an error in this sample, please file an issue: https://github.com/googlesamples/android-sunflower/issues

Patches are encouraged, and may be submitted by forking this project and submitting a pull request through GitHub.

Third Party Content

Select text used for describing the plants (in plants.json) are used from Wikipedia via CC BY-SA 3.0 US (license in ASSETS_LICENSE).

License

Copyright 2019 CuiYiming

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you 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.

About

A sample of Android development best practices with Android Jetpack programming by Java.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages