Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/florent37/newandroidarchitecture-component-github

Sample project based on the new Android Component Architecture
https://github.com/florent37/newandroidarchitecture-component-github

2017 android architecture dagger google inject io livedata mvvm observable retrofit rx

Last synced: 13 days ago
JSON representation

Sample project based on the new Android Component Architecture

Awesome Lists containing this project

README

        

# 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](https://raw.githubusercontent.com/florent37/NewAndroidArchitecture-Github/master/media/screen_small.png)

# LiveData as Observables !

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

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

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

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

}
});

return liveData;
}
```

# LifeCycle Owner

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

```java
public class MainFragment extends Fragment {
```

# DataBinding and ViewHolders

MainFragment.java
```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

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

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

# Dagger and Repository

```java
public class UserViewModel extends ViewModel {

private final GithubRepository githubRepository;

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

public LiveData 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/](https://www.fiches-plateau-moto.fr/)