Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rengwuxian committed Mar 24, 2016
0 parents commit 69d35a3
Show file tree
Hide file tree
Showing 69 changed files with 2,355 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
RxSamples
================
RxJava 和 Retrofit 结合使用的几个最常见使用方式举例。

1. **基本使用**

实现最基本的网络请求和结果处理。
![screenshot_1](./images/screenshot_1.png)

2. **转换(map)**

把返回的数据转换成更方便处理的格式再交给 Observer。
![screenshot_2](./images/screenshot_2.png)

3. **压合(zip)**

将不同接口并行请求获取到的数据糅合在一起后再处理。
![screenshot_3](./images/screenshot_3.png)

4. **一次性 token**

需要先请求 token 再访问的接口,使用 flatMap() 将 token 的请求和实际数据的请求连贯地串起来,而不必写嵌套的 Callback 结构。
![screenshot_4](./images/screenshot_4.png)

5. **非一次性 token**

对于非一次性的 token (即可重复使用的 token),在获取 token 后将它保存起来反复使用,并通过 retryWhen() 实现 token 失效时的自动重新获取,将 token 获取的流程彻底透明化,简化开发流程。
![screenshot_5](./images/screenshot_5.png)

6. **缓存**

使用 BehaviorSubject 缓存数据。
![screenshot_6](./images/screenshot_6.png)
1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
40 changes: 40 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"

defaultConfig {
applicationId "com.rengwuxian.rxjavasamples"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:support-v4:23.2.1'
compile 'com.android.support:support-v13:23.2.1'
compile 'com.android.support:design:23.2.1'
compile 'com.android.support:cardview-v7:23.2.1'
compile 'com.squareup.okhttp:okhttp:2.7.4'
compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
compile 'com.squareup.retrofit:adapter-rxjava:2.0.0-beta2'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.jakewharton:butterknife:7.0.1'
compile 'io.reactivex:rxjava:1.1.0'
compile 'io.reactivex:rxandroid:1.1.0'
compile 'com.jakewharton.rxbinding:rxbinding:0.4.0'
compile 'io.reactivex:rxjava:1.0.10'
}
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/rengwuxian/.android-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 *;
#}
24 changes: 24 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rengwuxian.rxjavasamples">

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

<application
android:name=".App"
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>
19 changes: 19 additions & 0 deletions app/src/main/java/com/rengwuxian/rxjavasamples/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// (c)2016 Flipboard Inc, All Rights Reserved.

package com.rengwuxian.rxjavasamples;

import android.app.Application;

public class App extends Application {
private static App INSTANCE;

public static App getInstance() {
return INSTANCE;
}

@Override
public void onCreate() {
super.onCreate();
INSTANCE = this;
}
}
37 changes: 37 additions & 0 deletions app/src/main/java/com/rengwuxian/rxjavasamples/BaseFragment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// (c)2016 Flipboard Inc, All Rights Reserved.

package com.rengwuxian.rxjavasamples;

import android.app.AlertDialog;
import android.app.Fragment;

import butterknife.OnClick;
import rx.Subscription;

public abstract class BaseFragment extends Fragment {
protected Subscription subscription;

@OnClick(R.id.tipBt)
void tip() {
new AlertDialog.Builder(getActivity())
.setTitle(getTitleRes())
.setView(getActivity().getLayoutInflater().inflate(getDialogRes(), null))
.show();
}

@Override
public void onDestroyView() {
super.onDestroyView();
unsubscribe();
}

protected void unsubscribe() {
if (subscription != null && !subscription.isUnsubscribed()) {
subscription.unsubscribe();
}
}

protected abstract int getDialogRes();

protected abstract int getTitleRes();
}
82 changes: 82 additions & 0 deletions app/src/main/java/com/rengwuxian/rxjavasamples/MainActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.rengwuxian.rxjavasamples;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.app.Fragment;
import android.support.v13.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import com.rengwuxian.rxjavasamples.module.token_advanced_5.TokenAdvancedFragment;
import com.rengwuxian.rxjavasamples.module.token_4.TokenFragment;
import com.rengwuxian.rxjavasamples.module.cache_6.CacheFragment;
import com.rengwuxian.rxjavasamples.module.zip_3.ZipFragment;
import com.rengwuxian.rxjavasamples.module.elementary_1.ElementaryFragment;
import com.rengwuxian.rxjavasamples.module.map_2.MapFragment;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {
@Bind(android.R.id.tabs) TabLayout tabLayout;
@Bind(R.id.viewPager) ViewPager viewPager;
@Bind(R.id.toolBar) Toolbar toolBar;

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

setSupportActionBar(toolBar);

viewPager.setAdapter(new FragmentPagerAdapter(getFragmentManager()) {
@Override
public int getCount() {
return 6;
}

@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new ElementaryFragment();
case 1:
return new MapFragment();
case 2:
return new ZipFragment();
case 3:
return new TokenFragment();
case 4:
return new TokenAdvancedFragment();
case 5:
return new CacheFragment();
default:
return new ElementaryFragment();
}
}

@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getString(R.string.title_elementary);
case 1:
return getString(R.string.title_map);
case 2:
return getString(R.string.title_zip);
case 3:
return getString(R.string.title_token);
case 4:
return getString(R.string.title_token_advanced);
case 5:
return getString(R.string.title_cache);
default:
return getString(R.string.title_elementary);
}
}
});
tabLayout.setupWithViewPager(viewPager);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// (c)2016 Flipboard Inc, All Rights Reserved.

package com.rengwuxian.rxjavasamples.adapter;

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

import com.bumptech.glide.Glide;
import com.rengwuxian.rxjavasamples.R;
import com.rengwuxian.rxjavasamples.model.Item;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class ItemListAdapter extends RecyclerView.Adapter {
List<Item> images;

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_item, parent, false);
return new DebounceViewHolder(view);
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
DebounceViewHolder debounceViewHolder = (DebounceViewHolder) holder;
Item image = images.get(position);
Glide.with(holder.itemView.getContext()).load(image.imageUrl).into(debounceViewHolder.imageIv);
debounceViewHolder.descriptionTv.setText(image.description);
}

@Override
public int getItemCount() {
return images == null ? 0 : images.size();
}

public void setItems(List<Item> images) {
this.images = images;
notifyDataSetChanged();
}

static class DebounceViewHolder extends RecyclerView.ViewHolder {
@Bind(R.id.imageIv) ImageView imageIv;
@Bind(R.id.descriptionTv) TextView descriptionTv;
public DebounceViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// (c)2016 Flipboard Inc, All Rights Reserved.

package com.rengwuxian.rxjavasamples.adapter;

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

import com.bumptech.glide.Glide;
import com.rengwuxian.rxjavasamples.R;
import com.rengwuxian.rxjavasamples.model.ZhuangbiImage;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class ZhuangbiListAdapter extends RecyclerView.Adapter {
List<ZhuangbiImage> images;

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_item, parent, false);
return new DebounceViewHolder(view);
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
DebounceViewHolder debounceViewHolder = (DebounceViewHolder) holder;
ZhuangbiImage image = images.get(position);
Glide.with(holder.itemView.getContext()).load(image.image_url).into(debounceViewHolder.imageIv);
debounceViewHolder.descriptionTv.setText(image.description);
}

@Override
public int getItemCount() {
return images == null ? 0 : images.size();
}

public void setImages(List<ZhuangbiImage> images) {
this.images = images;
notifyDataSetChanged();
}

static class DebounceViewHolder extends RecyclerView.ViewHolder {
@Bind(R.id.imageIv) ImageView imageIv;
@Bind(R.id.descriptionTv) TextView descriptionTv;
public DebounceViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// (c)2016 Flipboard Inc, All Rights Reserved.

package com.rengwuxian.rxjavasamples.model;

public class FakeThing {
public int id;
public String name;
}
Loading

0 comments on commit 69d35a3

Please sign in to comment.