Skip to content

Commit

Permalink
Adding loader to MVP example
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Yotive committed Dec 27, 2016
1 parent 5925058 commit def5394
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;

import com.example.myotive.codemash_common.BaseApplication;
import com.example.myotive.codemash_common.network.CodeMashAPI;
Expand All @@ -22,6 +24,7 @@ protected void onCreate(Bundle savedInstanceState) {
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);

speakerListView = (SpeakerListView)findViewById(R.id.speakerView);

codeMashAPI = BaseApplication.getApplication(this).getApplicationComponent().CodeMashAPI();

speakerListPresenter = new SpeakerListPresenter(codeMashAPI, speakerListView);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
public interface SpeakerListContract {
interface View extends BaseView<Presenter>{
void updateSpeakerList(List<Speaker> speakers);
void showLoading();
void hideLoading();
}

interface Presenter extends BasePresenter{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.example.myotive.mvpexample.speakerlist;

import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;

import com.example.myotive.codemash_common.network.CodeMashAPI;
import com.example.myotive.codemash_common.network.models.Speaker;
Expand Down Expand Up @@ -28,7 +30,6 @@ public SpeakerListPresenter(CodeMashAPI codeMashAPI, SpeakerListContract.View vi
this.codeMashAPI = codeMashAPI;
this.view = view;


this.view.setPresenter(this);
}

Expand All @@ -46,6 +47,8 @@ public void stop() {

@Override
public void getSpeakerList() {

view.showLoading();
speakerCall = codeMashAPI.GetSpeakers();

speakerCall.enqueue(new Callback<List<Speaker>>() {
Expand All @@ -54,11 +57,13 @@ public void onResponse(Call<List<Speaker>> call, Response<List<Speaker>> respons
if(response.isSuccessful()){
view.updateSpeakerList(response.body());
}
view.hideLoading();
}

@Override
public void onFailure(Call<List<Speaker>> call, Throwable t) {
Log.e(TAG, "Error calling CodeMash API", t);
view.hideLoading();
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ProgressBar;

import com.example.myotive.codemash_common.network.models.Speaker;
import com.example.myotive.codemash_common.ui.SpeakerAdapter;
Expand All @@ -23,6 +24,7 @@
public class SpeakerListView extends FrameLayout implements SpeakerListContract.View {


private ProgressBar loader;
private RecyclerView speakerRecyclerView;
private SpeakerAdapter speakerAdapter;

Expand Down Expand Up @@ -58,6 +60,8 @@ public SpeakerListView(Context context, AttributeSet attrs, int defStyleAttr) {
private void init() {
inflate(getContext(), R.layout.view_speaker_list, this);

loader = (ProgressBar)findViewById(R.id.loading);

speakerAdapter = new SpeakerAdapter(getContext(), Collections.<Speaker>emptyList(), speakerClickListener);
speakerRecyclerView = (RecyclerView) this.findViewById(R.id.rv_speakers);
speakerRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
Expand All @@ -69,6 +73,18 @@ public void updateSpeakerList(List<Speaker> speakers) {
speakerAdapter.swap(speakers);
}

@Override
public void showLoading() {
loader.setVisibility(VISIBLE);
speakerRecyclerView.setVisibility(GONE);
}

@Override
public void hideLoading() {
loader.setVisibility(GONE);
speakerRecyclerView.setVisibility(VISIBLE);
}

@Override
public void setPresenter(SpeakerListContract.Presenter presenter) {
this.presenter = presenter;
Expand Down
7 changes: 7 additions & 0 deletions mvpexample/src/main/res/layout/view_speaker_list.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
tools:context="com.example.myotive.mvpexample.speakerlist.SpeakerListView"
tools:parentTag="FrameLayout">

<ProgressBar
android:id="@+id/loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>

<android.support.v7.widget.RecyclerView
android:id="@+id/rv_speakers"
android:layout_width="match_parent"
Expand Down

0 comments on commit def5394

Please sign in to comment.