Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
dataBinding {
enabled = true
}
}

dependencies {
Expand All @@ -33,4 +36,5 @@ dependencies {
compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'io.reactivex:rxandroid:1.2.0'
compile 'io.reactivex:rxjava:1.1.5'
compile 'com.manaschaudhari:android-mvvm:0.1.2'
}
9 changes: 5 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.chrisarriola.githubrxjava">
package="com.chrisarriola.githubrxjava">

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.INTERNET" />

<application
android:name=".GithubApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.chrisarriola.githubrxjava;

import android.databinding.BindingConversion;
import android.view.View;

import rx.subjects.PublishSubject;

@SuppressWarnings("unused")
public class BindingAdapters {

@BindingConversion
public static View.OnClickListener subjectToListener(final PublishSubject<Void> subject) {
if (subject != null) {
return new View.OnClickListener() {
@Override
public void onClick(View view) {
subject.onNext(null);
}
};
} else {
return null;
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.chrisarriola.githubrxjava;

import android.app.Application;
import android.databinding.ViewDataBinding;

import com.manaschaudhari.android_mvvm.ViewModel;
import com.manaschaudhari.android_mvvm.adapters.ViewModelBinder;
import com.manaschaudhari.android_mvvm.utils.BindingUtils;

public class GithubApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
BindingUtils.setDefaultBinder(new ViewModelBinder() {
@Override
public void bind(ViewDataBinding viewDataBinding, ViewModel viewModel) {
viewDataBinding.setVariable(com.chrisarriola.githubrxjava.BR.vm, viewModel);
}
});
}
}
18 changes: 18 additions & 0 deletions app/src/main/java/com/chrisarriola/githubrxjava/GithubRepoVM.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.chrisarriola.githubrxjava;

import com.manaschaudhari.android_mvvm.ViewModel;

public class GithubRepoVM implements ViewModel {
public final String name;
public final String description;
public final String language;
public final String stars;

public GithubRepoVM(GitHubRepo repo) {
name = repo.name;
description = repo.description;
language = "Language: " + repo.language;
stars = "Stars: " + repo.stargazersCount;
}

}
72 changes: 11 additions & 61 deletions app/src/main/java/com/chrisarriola/githubrxjava/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,70 +1,20 @@
package com.chrisarriola.githubrxjava;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.util.List;
import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import android.support.annotation.NonNull;

public class MainActivity extends AppCompatActivity {
import com.manaschaudhari.android_mvvm.MvvmActivity;
import com.manaschaudhari.android_mvvm.ViewModel;

private static final String TAG = MainActivity.class.getSimpleName();
private GitHubRepoAdapter adapter = new GitHubRepoAdapter();
private Subscription subscription;
public class MainActivity extends MvvmActivity {

@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final ListView listView = (ListView) findViewById(R.id.list_view_repos);
listView.setAdapter(adapter);

final EditText editTextUsername = (EditText) findViewById(R.id.edit_text_username);
final Button buttonSearch = (Button) findViewById(R.id.button_search);
buttonSearch.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
final String username = editTextUsername.getText().toString();
if (!TextUtils.isEmpty(username)) {
getStarredRepos(username);
}
}
});
}

@Override protected void onDestroy() {
if (subscription != null && !subscription.isUnsubscribed()) {
subscription.unsubscribe();
}
super.onDestroy();
@NonNull
@Override
public ViewModel createViewModel() {
return new MainViewModel();
}

private void getStarredRepos(String username) {
subscription = GitHubClient.getInstance()
.getStarredRepos(username)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<List<GitHubRepo>>() {
@Override public void onCompleted() {
Log.d(TAG, "In onCompleted()");
}

@Override public void onError(Throwable e) {
e.printStackTrace();
Log.d(TAG, "In onError()");
}

@Override public void onNext(List<GitHubRepo> gitHubRepos) {
Log.d(TAG, "In onNext()");
adapter.setGitHubRepos(gitHubRepos);
}
});
@Override
public int getLayoutId() {
return R.layout.activity_main;
}
}
47 changes: 47 additions & 0 deletions app/src/main/java/com/chrisarriola/githubrxjava/MainViewModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.chrisarriola.githubrxjava;

import android.databinding.ObservableField;

import com.manaschaudhari.android_mvvm.ViewModel;

import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;
import rx.subjects.PublishSubject;

import static com.manaschaudhari.android_mvvm.FieldUtils.toObservable;

public class MainViewModel implements ViewModel {
public final Observable<List<GithubRepoVM>> repositories;
public final ObservableField<String> username = new ObservableField<>();
public final PublishSubject<Void> onSearchClick = PublishSubject.create();

public MainViewModel() {
repositories =
toObservable(this.username)
.sample(onSearchClick)
.switchMap(new Func1<String, Observable<List<GithubRepoVM>>>() {
@Override
public Observable<List<GithubRepoVM>> call(String username) {
return GitHubClient.getInstance()
.getStarredRepos(username)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.map(new Func1<List<GitHubRepo>, List<GithubRepoVM>>() {
@Override
public List<GithubRepoVM> call(List<GitHubRepo> gitHubRepos) {
List<GithubRepoVM> vms = new ArrayList<>();
for (GitHubRepo repo : gitHubRepos) {
vms.add(new GithubRepoVM(repo));
}
return vms;
}
});
}
});
}
}
63 changes: 39 additions & 24 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,34 +1,49 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ListView
android:id="@+id/list_view_repos"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bind="http://schemas.android.com/apk/res-auto">

<data>

<variable
name="vm"
type="com.chrisarriola.githubrxjava.MainViewModel" />
</data>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
android:layout_height="match_parent"
android:orientation="vertical">

<EditText
android:id="@+id/edit_text_username"
android:layout_width="0dp"
android:layout_height="wrap_content"
<android.support.v7.widget.RecyclerView
android:id="@+id/list_view_repos"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:hint="@string/username"/>
bind:items="@{vm.repositories}"
bind:layout_vertical="@{true}"
bind:view_provider="@{@layout/item_github_repo}" />

<Button
android:id="@+id/button_search"
android:layout_width="wrap_content"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/search"/>
android:orientation="horizontal">

</LinearLayout>
<EditText
android:id="@+id/edit_text_username"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="@string/username"
android:text="@={vm.username}" />

<Button
android:id="@+id/button_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="@{vm.onSearchClick}"
android:text="@string/search" />

</LinearLayout>
</LinearLayout>

</LinearLayout>
</layout>
Loading