Giter Club home page Giter Club logo

newandroidarchitecture-component-github's Introduction

NewAndroidArchitecture-Github

Android app on Google Play

Sample project based on the new Android Component Architecture

Lifecycle, LiveData, MVVM, ViewModels, DataBinding, Dagger, Retrofit

https://developer.android.com/topic/libraries/architecture/guide.html

Alt sample

LiveData as Observables !

LiveDatas works like RxJava's Observables, they will notify the observer when the data is Available

@Override
public LiveData<User> getUser(String userName){
    final MutableLiveData<User> liveData = new MutableLiveData<>();

    githubAPI.user(userName).enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                if(response.isSuccessful()) {
                    liveData.setValue(response.body());
                }
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {

            }
        });

    return liveData;
}

LifeCycle Owner

Use Support Fragment and AppCompatActivity to be attached to the application's state

public class MainFragment extends Fragment {

DataBinding and ViewHolders

MainFragment.java

reposAdapter = new ReposAdapter();
viewDataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
viewDataBinding.recyclerView.setAdapter(reposAdapter);

AppComponent.from(getContext()).inject(this);
//inject the viewmodel responding to User
//inject the viewmodel responding to List<Repo>

//fetch the user from the datasource
userViewModel.getUser("florent37")
                .observe(this, new Observer<User>() {
                    @Override
                    public void onChanged(@Nullable User user) {
                        viewDataBinding.setUser(user);
                    }
                });

//fetch the repos from the datasource
reposListViewModel.getRepos("florent37")
                .observe(this, new Observer<List<Repo>>() {
                    @Override
                    public void onChanged(@Nullable List<Repo> repos) {
                        //when available, send it to the recyclerview
                        reposAdapter.setRepos(repos);
                    }
                });

Dagger and Repository

public class UserViewModel extends ViewModel {

    private final GithubRepository githubRepository;

    @Inject
    public UserViewModel(GithubRepository githubRepository) {
        this.githubRepository = githubRepository;
    }

    public LiveData<User> getUser(String userName) {
        //userLiveData will be notified when the user is fetched
        return githubRepository.getUser(userName);
    }
}
Android app on Google Play

Fiches Plateau Moto : https://www.fiches-plateau-moto.fr/

newandroidarchitecture-component-github's People

Contributors

florent37 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.