Skip to content

Commit

Permalink
添加Github搜索结果列表
Browse files Browse the repository at this point in the history
  • Loading branch information
TheseYears committed Apr 21, 2018
1 parent 8e8e036 commit 5d76408
Show file tree
Hide file tree
Showing 12 changed files with 200 additions and 19 deletions.
19 changes: 12 additions & 7 deletions CoreLibs/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ apply plugin: 'com.android.library'
apply plugin: 'com.jakewharton.butterknife'

android {
compileSdkVersion 26
compileSdkVersion 27

defaultConfig {
minSdkVersion 14
targetSdkVersion 26
targetSdkVersion 27
versionCode 120
versionName "1.2.0"
}
Expand All @@ -20,8 +20,10 @@ android {

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:26.1.0'
compile 'com.android.support:support-v4:26.1.0'
compile 'com.android.support:appcompat-v7:27.1.1'
compile 'com.android.support:support-v4:27.1.1'
compile 'com.android.support:recyclerview-v7:27.1.1'

compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.github.bumptech.glide:okhttp3-integration:1.4.0@aar'
compile 'jp.wasabeef:glide-transformations:2.0.1'
Expand All @@ -38,11 +40,14 @@ dependencies {
compile 'com.squareup.retrofit2:retrofit:2.4.0'
compile 'com.squareup.retrofit2:adapter-rxjava2:2.2.0'
compile 'com.squareup.retrofit2:converter-gson:2.2.0'
compile 'com.squareup.okhttp3:okhttp:3.7.0'
compile 'com.squareup.okhttp3:okhttp:3.10.0'

compile 'cn.finalteam:galleryfinal:1.4.8.7'
compile('cn.finalteam:galleryfinal:1.4.8.7') {
exclude module: 'support-v4'
}

compile('com.github.afollestad.material-dialogs:core:0.9.0.1@aar') {
compile('com.github.afollestad.material-dialogs:core:0.9.6.0@aar') {
transitive = true
exclude module: 'recyclerview-v7'
}
}
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 26
compileSdkVersion 27
defaultConfig {
applicationId "com.ryan.corelibs"
minSdkVersion 19
targetSdkVersion 26
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Expand All @@ -21,7 +21,7 @@ android {

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@
</intent-filter>
</activity>

<activity
android:name="com.corelibs.exception.ExceptionDialogActivity"
android:screenOrientation="portrait"
android:theme="@style/ExceptionDialogStyle" />

<activity
android:name="com.corelibs.exception.ExceptionDetailActivity"
android:screenOrientation="portrait" />

</application>

</manifest>
31 changes: 31 additions & 0 deletions app/src/main/java/com/ryan/corelibs/adapter/GithubAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.ryan.corelibs.adapter;

import android.content.Context;

import com.corelibs.utils.adapter.BaseAdapterHelper;
import com.corelibs.utils.adapter.recycler.RecyclerAdapter;
import com.ryan.corelibs.R;
import com.ryan.corelibs.model.entity.Repository;

import java.text.SimpleDateFormat;
import java.util.Locale;

public class GithubAdapter extends RecyclerAdapter<Repository> {

private SimpleDateFormat format;

public GithubAdapter(Context context) {
super(context, R.layout.i_github);
format = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
}

@Override
protected void convert(BaseAdapterHelper helper, Repository item, int position) {
helper.setText(R.id.tv_title, item.owner.login + "/" + item.name)
.setText(R.id.tv_desc, item.description)
.setText(R.id.tv_lang, item.language)
.setText(R.id.tv_star, String.valueOf(item.stargazers_count))
.setText(R.id.tv_update_date, "Updated at " + format.format(item.updated_at))
.setImageUrl(R.id.iv_avatar, item.owner.avatar_url);
}
}
8 changes: 8 additions & 0 deletions app/src/main/java/com/ryan/corelibs/model/entity/Owner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.ryan.corelibs.model.entity;

import java.io.Serializable;

public class Owner implements Serializable {
public String login;
public String avatar_url;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
package com.ryan.corelibs.model.entity;

import java.io.Serializable;
import java.util.Date;

public class Repository implements Serializable {
public Long id;
public String name;
public String description;
public Owner owner;
public int stargazers_count;
public String language;
public Date updated_at;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.ryan.corelibs.presenter;

import com.corelibs.api.ResponseTransformer;
import com.corelibs.pagination.presenter.PagePresenter;
import com.corelibs.pagination.presenter.ListPagePresenter;
import com.corelibs.subscriber.PaginationSubscriber;
import com.ryan.corelibs.model.api.GithubApi;
import com.ryan.corelibs.model.entity.Data;
Expand All @@ -10,7 +10,7 @@

import java.util.List;

public class MainPresenter extends PagePresenter<MainView> {
public class MainPresenter extends ListPagePresenter<MainView> {

private GithubApi api;

Expand All @@ -25,6 +25,9 @@ public void onStart() {
}

public void search(final boolean reload) {
if (!doPagination(reload)) return;
if (reload) view.showLoading();

api.searchRepositories("CoreLibs", getPageNo(), getPageSize())
.compose(new ResponseTransformer<Data<Repository>>())
.compose(this.<Data<Repository>>bindToLifeCycle())
Expand All @@ -36,7 +39,7 @@ protected void onDataNotNull(Data<Repository> data) {

@Override
protected Object getCondition(Data<Repository> data, boolean dataNotNull) {
return data.total_count;
return getListResult(data, dataNotNull);
}

@Override
Expand Down
47 changes: 45 additions & 2 deletions app/src/main/java/com/ryan/corelibs/view/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package com.ryan.corelibs.view;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.corelibs.base.BaseActivity;
import com.corelibs.utils.DisplayUtil;
import com.corelibs.views.cube.ptr.PtrFrameLayout;
import com.corelibs.views.ptr.layout.PtrAutoLoadMoreLayout;
import com.ryan.corelibs.R;
import com.ryan.corelibs.adapter.GithubAdapter;
import com.ryan.corelibs.model.entity.Repository;
import com.ryan.corelibs.presenter.MainPresenter;
import com.ryan.corelibs.view.interfaces.MainView;
Expand All @@ -21,6 +27,9 @@ public class MainActivity extends BaseActivity<MainView, MainPresenter> implemen
@BindView(R.id.nav) NavBar nav;
@BindView(R.id.ptr) PtrAutoLoadMoreLayout<RecyclerView> ptr;

private GithubAdapter adapter;
private Paint paint;

@Override
protected int getLayoutId() {
return R.layout.ac_main;
Expand All @@ -30,6 +39,28 @@ protected int getLayoutId() {
protected void init(Bundle savedInstanceState) {
nav.setTitle("search CoreLibs");

adapter = new GithubAdapter(this);

paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(0xFFDDDDDD);
paint.setStrokeWidth(DisplayUtil.dip2px(this, 0.5f));

ptr.getPtrView().setLayoutManager(new LinearLayoutManager(this));
ptr.getPtrView().addItemDecoration(new RecyclerView.ItemDecoration() {
@Override public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
int count = parent.getChildCount();
for (int i = 0; i < count; i++) {
View child = parent.getChildAt(i);
int position = parent.getChildAdapterPosition(child);
if (position > 0) {
c.drawLine(child.getLeft(), child.getTop(), child.getRight(),
child.getTop(), paint);
}
}
}
});

ptr.getPtrView().setAdapter(adapter);
ptr.setRefreshLoadCallback(new PtrAutoLoadMoreLayout.RefreshLoadCallback() {
@Override public void onLoading(PtrFrameLayout frame) {
presenter.search(false);
Expand All @@ -50,16 +81,28 @@ protected MainPresenter createPresenter() {
}

@Override
public void onLoadingCompleted() {
public void showLoading() {
ptr.setRefreshing();
}

@Override
public void hideLoading() {
ptr.complete();
}

@Override
public void onAllPageLoaded() {
public void onLoadingCompleted() {
hideLoading();
}

@Override
public void onAllPageLoaded() {
ptr.disableLoading();
}

@Override
public void renderResult(List<Repository> repositories, boolean reload) {
if (reload) adapter.replaceAll(repositories);
else adapter.addAll(repositories);
}
}
4 changes: 3 additions & 1 deletion app/src/main/res/layout/ac_main.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?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="match_parent">

Expand All @@ -12,7 +13,8 @@
<com.corelibs.views.ptr.layout.PtrAutoLoadMoreLayout
android:id="@+id/ptr"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:background="@color/white">

<com.corelibs.views.ptr.loadmore.widget.AutoLoadMoreRecyclerView
android:layout_width="match_parent"
Expand Down
73 changes: 73 additions & 0 deletions app/src/main/res/layout/i_github.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?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="wrap_content"
android:orientation="horizontal"
android:padding="15dp"
android:background="@color/transparent">

<ImageView
android:id="@+id/iv_avatar"
android:layout_width="40dp"
android:layout_height="40dp"
android:contentDescription="@null"
android:scaleType="centerCrop"
android:src="#eee"
android:layout_marginEnd="10dp"
android:layout_marginTop="8dp"/>

<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginEnd="10dp">

<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="@color/blue"
android:layout_marginBottom="3dp"/>

<TextView
android:id="@+id/tv_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="@color/text_hint"
android:layout_marginBottom="10dp"
android:maxLines="2"
android:ellipsize="end"/>

<TextView
android:id="@+id/tv_update_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/text_hint"
android:textSize="12sp"/>

</LinearLayout>

<TextView
android:id="@+id/tv_lang"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="@color/text_main"
android:layout_marginEnd="8dp"/>

<TextView
android:id="@+id/tv_star"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="@color/text_main"
android:layout_marginEnd="8dp"
android:drawableStart="@mipmap/star"
android:gravity="center_vertical"
android:drawablePadding="2dp"/>

</LinearLayout>
Binary file added app/src/main/res/mipmap-xxhdpi/star.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 2 additions & 3 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="text_main">#666666</color>
<color name="text_hint">#999999</color>
</resources>

0 comments on commit 5d76408

Please sign in to comment.