Skip to content

Commit

Permalink
add sample project
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianTerhorst committed May 10, 2016
1 parent 68c0167 commit 94298d9
Show file tree
Hide file tree
Showing 24 changed files with 343 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ proguard/

# Android Studio captures folder
captures/

*.iml
36 changes: 36 additions & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Built application files
*.apk
*.ap_

# Files for the Dalvik VM
*.dex

*.idea

# Java class files
*.class

# Generated files
bin/
gen/

# Gradle files
.gradle/
build/

# Local configuration file (sdk path, etc)
local.properties

# Proguard folder generated by Eclipse
proguard/

# Log Files
*.log

# Android Studio Navigation editor temp files
.navigation/

# Android Studio captures folder
captures/

*.iml
34 changes: 34 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda'
apply plugin: 'realm-android'

android {
compileSdkVersion 23
buildToolsVersion "23.0.3"

defaultConfig {
applicationId "io.fabianterhorst.apiclient.app"
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {
retrolambdaConfig 'net.orfjackal.retrolambda:retrolambda:2.3.0'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:recyclerview-v7:23.3.0'
compile 'com.trello:rxlifecycle-components:0.6.0'
compile project (':apiclient')
}
17 changes: 17 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /Users/fabianterhorst/Desktop/sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
23 changes: 23 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.fabianterhorst.apiclient.app">

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

<application
android:name=".MyApplication"
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"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>

</manifest>
23 changes: 23 additions & 0 deletions app/src/main/java/io/fabianterhorst/apiclient/app/Github.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.fabianterhorst.apiclient.app;

import java.util.List;

import io.fabianterhorst.apiclient.ApiClient;
import retrofit2.http.Path;
import rx.Observable;

public class Github extends ApiClient<GithubApi> implements GithubApi {

public Github() {
super(GithubApi.class, GithubApi.END_POINT);
}

public static void init() {
init(new Github());
}

@Override
public Observable<List<Repository>> getRepositories(@Path("user") String user) {
return getApiObservable(getApi().getRepositories(user), Repository.class);
}
}
15 changes: 15 additions & 0 deletions app/src/main/java/io/fabianterhorst/apiclient/app/GithubApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.fabianterhorst.apiclient.app;

import java.util.List;

import retrofit2.http.GET;
import retrofit2.http.Path;
import rx.Observable;

public interface GithubApi {

String END_POINT = "https://api.github.com";

@GET("/users/{user}/repos")
Observable<List<Repository>> getRepositories(@Path("user") String user);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.fabianterhorst.apiclient.app;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.trello.rxlifecycle.components.support.RxAppCompatActivity;

public class MainActivity extends RxAppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.repositories);
RepositoriesAdapter adapter = new RepositoriesAdapter();
if(recyclerView != null) {
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
//Github github = Github.getInstance(); is also working, but does not un subscribe on orientation change for example
Github github = Github.getInstance(bindToLifecycle());
github.getRepositories("realm").subscribe(adapter::setRepositories);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.fabianterhorst.apiclient.app;

import android.app.Application;

import io.realm.Realm;
import io.realm.RealmConfiguration;

public class MyApplication extends Application {

@Override
public void onCreate() {
super.onCreate();
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this)
.deleteRealmIfMigrationNeeded()
.build();
Realm.setDefaultConfiguration(realmConfiguration);
Github.init();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package io.fabianterhorst.apiclient.app;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

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

public class RepositoriesAdapter extends RecyclerView.Adapter<RepositoriesAdapter.ViewHolder> {

private List<Repository> repositories;

public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView name;
public ViewHolder(View view) {
super(view);
name = (TextView) view.findViewById(R.id.repository_name);
}
}

public RepositoriesAdapter() {
setHasStableIds(true);
repositories = new ArrayList<>();
}

public void setRepositories(List<Repository> repositories) {
this.repositories = repositories;
notifyDataSetChanged();
}

@Override
public long getItemId(int position) {
return repositories.get(position).getId();
}

@Override
public RepositoriesAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_repository, parent, false);
return new ViewHolder(v);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.name.setText(repositories.get(position).getName());
}

@Override
public int getItemCount() {
return repositories.size();
}
}
34 changes: 34 additions & 0 deletions app/src/main/java/io/fabianterhorst/apiclient/app/Repository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.fabianterhorst.apiclient.app;

import io.realm.RealmObject;
import io.realm.annotations.PrimaryKey;
import io.realm.annotations.RealmClass;

@RealmClass
public class Repository extends RealmObject {

@PrimaryKey
private long id;

private String name;

public Repository() {

}

public void setId(long id) {
this.id = id;
}

public void setName(String name) {
this.name = name;
}

public long getId() {
return id;
}

public String getName() {
return name;
}
}
14 changes: 14 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="io.fabianterhorst.apiclient.app.MainActivity">

<android.support.v7.widget.RecyclerView
android:id="@+id/repositories"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
13 changes: 13 additions & 0 deletions app/src/main/res/layout/item_repository.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/repository_name"
android:layout_width="wrap_content"
android:textColor="#000000"
android:layout_height="wrap_content"/>

</LinearLayout>
Binary file added app/src/main/res/mipmap-hdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/mipmap-mdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/mipmap-xhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/mipmap-xxhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions app/src/main/res/values-w820dp/dimens.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<resources>
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
(such as screen margins) for screens with more than 820dp of available width. This
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
<dimen name="activity_horizontal_margin">64dp</dimen>
</resources>
6 changes: 6 additions & 0 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
</resources>
5 changes: 5 additions & 0 deletions app/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<resources>
<string name="app_name">App</string>
</resources>
11 changes: 11 additions & 0 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>

</resources>
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1 +1 @@
include ':apiclient', ':apiclient-components', ':apiclient-accountmanager'
include ':apiclient', ':apiclient-components', ':apiclient-accountmanager', ':app'

0 comments on commit 94298d9

Please sign in to comment.