Personal analysis of the Daily project on GitHub

20498 단어 project
Analysis of other people's projects on github. I think he framed it well. Learn from him well. https://github.com/spring2613/Daily The project has a lot of highlights. I found the points that I think are good, and analyzed them.

Let's start analyzing:

Application–>


I usually start analyzing the code from the Application, so let's take a look at how other people's Applications do it. In the manifest file:
    ".App"
        android:allowBackup="true"
        android:fullBackupContent="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:theme="@style/BaseAppTheme">
    </application>
Detailed explanation of Android application and activity tags: http://blog.csdn.net/self_study/article/details/54020909
In the Application, it is also the App, that is, various initializations.

setupDatabase


SplashActivity–>


In the manifest file:
        <activity
            android:name=".mvp.ui.activities.SplashActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:theme="@style/AppTheme.FullScreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            intent-filter>
        activity>
The first started Activity configuration Details about Intent: http://blog.csdn.net/u012637501/article/details/41080891

SplashActivity–>


Take a look at this SplashActivity again:

Property Animation


ButterKnife


RxAndroid RxJava


BaseActivity–>


Abstract, generic:


public abstract class BaseActivity<T extends BasePresenter> extends AppCompatActivity {
    protected T mPresenter;

    public abstract int getLayoutId();

    public abstract void initInjector();

    public abstract void initViews();
 }

ActivityComponent


DrawerLayout


Toolbar


onNewIntent:


    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
    }

RefWatcher


NewsActivity–>


DrawerLayout



CoordinatorLayout


AppBarLayout


Toolbar


TabLayout


ViewPager


FloatingActionButton


Snackbar


MVP


Rx


butterknife


dagger


So many things combined to play together. It feels like a must-play for the mainstream now.

BaseFragment–>


Similar to Activity, it is also associated with Presenter through generics

NewsListFragment


RecyclerView


SwipeRefreshLayout


Rx


butterknife


dagger


MVP


BaseRecyclerViewAdapter–>


Generics


Commonly used additions, deletions and revisions


Footer load more


setItemAppearAnimation


    protected void setItemAppearAnimation(RecyclerView.ViewHolder holder, int position, @AnimRes int type) {
        if (position > mLastPosition/* && !isFooterPosition(position)*/) {
            Animation animation = AnimationUtils.loadAnimation(holder.itemView.getContext(), type);
            holder.itemView.startAnimation(animation);
            mLastPosition = position;
        }
    }

NewsListAdapter–>


onCreateViewHolder


Wonderful usage:
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, final int viewType) {
        View view;
        switch (viewType) {
            case TYPE_FOOTER:
                view = getView(parent, R.layout.item_news_footer);
                return new FooterViewHolder(view);
            case TYPE_ITEM:
                view = getView(parent, R.layout.item_news);
                final ItemViewHolder itemViewHolder = new ItemViewHolder(view);
                setItemOnClickEvent(itemViewHolder, false);
                return itemViewHolder;
            case TYPE_PHOTO_ITEM:
                view = getView(parent, R.layout.item_news_photo);
                final PhotoViewHolder photoItemViewHolder = new PhotoViewHolder(view);
                setItemOnClickEvent(photoItemViewHolder, true);
                return photoItemViewHolder;
            default:
                throw new RuntimeException("there is no type that matches the type " +
                        viewType + " + make sure your using types correctly");
        }
    }

Glide


Google's recommended image loading library Glide

A variety of flexible use of ViewHolder


butterknife


NewsActivity in MVP–>


NewsActivity–>NewsView–>NewsPresenterImpl–>NewsPresenter–>NewsInteractorImpl–>NewsInteractor–>NewsChannelTable–>NewsChannelTableManager


The Subscription of Rx is used in NewsInteractor


public interface NewsInteractor {

    Subscription lodeNewsChannels(RequestCallBack callBack);
}

Rx's Observable is used in NewsInteractorImpl


@Inject using dagger
public class NewsInteractorImpl implements NewsInteractor> {

    @Inject
    public NewsInteractorImpl() {

    }

    @Override
    public Subscription lodeNewsChannels(final RequestCallBack> callBack) {
        return Observable
                .create(new Observable.OnSubscribe>() {
                    @Override
                    public void call(Subscriber super List> subscriber) {
                        NewsChannelTableManager.initDB();
                        subscriber.onNext(NewsChannelTableManager.loadNewsChannelsMine());
                        subscriber.onCompleted();
                    }
                })
                .compose(TransformUtils.>defaultSchedulers())
                .subscribe(new Subscriber>() {
                               @Override
                               public void onCompleted() {

                               }

                               @Override
                               public void onError(Throwable e) {
                                   callBack.onError(App.getAppContext().getString(R.string.db_error));
                               }

                               @Override
                               public void onNext(List newsChannelTables) {
                                   callBack.success(newsChannelTables);
                               }
                           }
                );
    }
}

greendao is used in NewsChannelTable


NewsChannelTableManager


Repository (warehouse) is divided into (db) and (network) NewsChannelTableManager belongs to obtaining data from db repository

NewsListFragment in MVP–>


NewsListFragment–>NewsListView–>NewsListPresenterImpl–>NewsListPresenter–>NewsListInteractorImpl–>NewsListInteractor–>NewsSummary–>RetrofitManager–>NetUtil


Rx's Observable is used in NewsListInteractor


public interface NewsListInteractor {
    Subscription loadNews(RequestCallBack listener, String type, String id, int startPage);
}

NewsListInteractorImpl (I don't understand it yet.)


public class NewsListInteractorImpl implements NewsListInteractor> {

    //    private boolean mIsNetError;
    @Inject
    public NewsListInteractorImpl() {
    }

    @Override
    public Subscription loadNews(final RequestCallBack> listener, String type,
                                 final String id, int startPage) {
//        mIsNetError = false;
        //  API observeOn(MainThread) , , onComplete ,
        // unsubscribe , call.cancel NetworkOnMainThreadException
        //  unsubscribeOn(io)
        return RetrofitManager.getInstance(HostType.NETEASE_NEWS_VIDEO).getNewsListObservable(type, id, startPage)
                .flatMap(new Func1>, Observable>() {
                    @Override
                    public Observable call(Map> map) {
                        if (id.endsWith(ApiConstants.HOUSE_ID)) {
                            //  id key 
                            return Observable.from(map.get(" "));
                        }
                        return Observable.from(map.get(id));
                    }
                })
                .map(new Func1() {
                    @Override
                    public NewsSummary call(NewsSummary newsSummary) {
                        String ptime = MyUtils.formatDate(newsSummary.getPtime());
                        newsSummary.setPtime(ptime);
                        return newsSummary;
                    }
                })
//                .toList()
                .distinct()
                .toSortedList(new Func2() {
                    @Override
                    public Integer call(NewsSummary newsSummary, NewsSummary newsSummary2) {
                        return newsSummary2.getPtime().compareTo(newsSummary.getPtime());
                    }
                })
                .compose(TransformUtils.>defaultSchedulers())
                .subscribe(new Subscriber>() {
                    @Override
                    public void onCompleted() {
                        KLog.d();
//                        checkNetState(listener);
                    }

                    @Override
                    public void onError(Throwable e) {
                        KLog.e(e.toString());
//                        checkNetState(listener);
//                        if (!NetUtil.isNetworkAvailable(App.getAppContext())) {
                        listener.onError(MyUtils.analyzeNetworkError(e));
//                        }
                    }

                    @Override
                    public void onNext(List newsSummaries) {
                        KLog.d();
                        listener.success(newsSummaries);
                    }
                });

    }

//    private void checkNetState(RequestCallBack> listener) {
//        if (!NetUtil.isNetworkAvailable(App.getAppContext())) {
//            mIsNetError = true;
//            listener.onError(App.getAppContext().getString(R.string.internet_error));
//        } else {
//            mIsNetError = false;
//        }
//    }
}

greendao layer


DaoMaster


DaoSession


NewsChannelTableDao


common layer


ApiConstants


Constants


HostType


LoadNewsType


PhotoRequestType


utils layer


widget layer


Daily This is a framework template made by others, and there are many knowledge points in it. It is quite unfamiliar to me, and I have to work hard to learn one by one. MVP + Rx + Retrofit + OkHttp3 + Dagger is a good demo for getting started. If + modularized and componentized, those things will be even more powerful.

Attached:


https://GitHub.com/green robot/green DAO


HTTPS://GitHub.com/square/OK HTTP


https://GitHub.com/square/retrofit


https://GitHub.com/Re ActiveX/Rx Java


https://GitHub.com/Re ActiveX/Rx Android


https://GitHub.com/square/dagger


https://GitHub.com/Google samples/Android-architecture


https://GitHub.com/Jake Wharton/Butter knife

좋은 웹페이지 즐겨찾기