package io.pivotal.arca.fragments;

import android.database.Cursor;
import android.view.View;
import android.widget.Toast;

import io.pivotal.arca.dispatcher.Error;
import io.pivotal.arca.dispatcher.QueryResult;

public class ArcaViewManager {

    private final View mView;

    private int mProgressId = android.R.id.progress;
    private int mEmptyId = android.R.id.empty;
    private int mContentId = android.R.id.content;
    private int mListId = android.R.id.list;

    public ArcaViewManager(final View view) {
        mView = view;
    }

    public void setProgressId(final int id) {
        mProgressId = id;
    }

    public void setEmptyId(final int id) {
        mEmptyId = id;
    }

    public void setContentId(final int id) {
        mContentId = id;
    }

    private View getView() {
        return mView;
    }

    private View getProgressView() {
        return getView().findViewById(mProgressId);
    }

    private View getEmptyView() {
        return getView().findViewById(mEmptyId);
    }

    private View getContentView() {
        final View content = getView().findViewById(mContentId);
        return content != null ? content : getView().findViewById(mListId);
    }

    public void showProgressView() {
        getContentView().setVisibility(View.INVISIBLE);
        getProgressView().setVisibility(View.VISIBLE);
        getEmptyView().setVisibility(View.INVISIBLE);
    }

    public void showContentView() {
        getContentView().setVisibility(View.VISIBLE);
        getProgressView().setVisibility(View.INVISIBLE);
        getEmptyView().setVisibility(View.INVISIBLE);
    }

    public void showEmptyView() {
        getContentView().setVisibility(View.INVISIBLE);
        getProgressView().setVisibility(View.INVISIBLE);
        getEmptyView().setVisibility(View.VISIBLE);
    }

    public void toastError(final Error error) {
        final String message = String.format("%s", error.getMessage());
        Toast.makeText(mView.getContext(), message, Toast.LENGTH_SHORT).show();
    }

    public void checkResult(final QueryResult result) {
        final Cursor data = result.getData();

        if (data != null && data.getCount() > 0) {
            showContentView();
        } else if (!result.isSyncing()) {
            showEmptyView();
        }
    }

    public void checkError(final Error error) {
        if (error != null) {
            showEmptyView();
            toastError(error);
        }
    }
}

@@ -269,16 +269,67 @@
 mView;%0A%0A
+    private int mContentId = android.R.id.content;%0A
     priv
@@ -424,59 +424,8 @@
 ty;%0A
-    private int mContentId = android.R.id.content;%0A
     
@@ -533,32 +533,109 @@
  = view;%0A    %7D%0A%0A
+    public void setContentId(final int id) %7B%0A        mContentId = id;%0A    %7D%0A%0A
     public void 
@@ -774,85 +774,8 @@
  %7D%0A%0A
-    public void setContentId(final int id) %7B%0A        mContentId = id;%0A    %7D%0A%0A
     
@@ -844,32 +844,31 @@
 ate View get
-Progress
+Content
 View() %7B%0A   
@@ -864,38 +864,52 @@
 iew() %7B%0A        
-return
+final View content =
  getView().findV
@@ -917,24 +917,23 @@
 ewById(m
-Progress
+Content
 Id);%0A   
@@ -937,59 +937,46 @@
     
-%7D%0A%0A
     
-private View getEmptyView() %7B%0A        return
+return content != null ? content :
  get
@@ -996,21 +996,20 @@
 ewById(m
-Empty
+List
 Id);%0A   
@@ -1024,39 +1024,40 @@
 private View get
-Content
+Progress
 View() %7B%0A       
@@ -1057,36 +1057,22 @@
         
-final View content =
+return
  getView
@@ -1080,39 +1080,40 @@
 ).findViewById(m
-Content
+Progress
 Id);%0A        ret
@@ -1109,46 +1109,59 @@
     
+%7D%0A%0A
     
-return content != null ? content :
+private View getEmptyView() %7B%0A        return
  get
@@ -1173,36 +1173,37 @@
 ).findViewById(m
-List
+Empty
 Id);%0A    %7D%0A%0A    
@@ -1218,24 +1218,23 @@
 oid show
-Progress
+Content
 View() %7B
@@ -1233,32 +1233,62 @@
 View() %7B%0A       
+ if (getContentView() != null)
  getContentView(
@@ -1300,34 +1300,32 @@
 Visibility(View.
-IN
 VISIBLE);%0A      
@@ -1317,32 +1317,63 @@
 ISIBLE);%0A       
+ if (getProgressView() != null)
  getProgressView
@@ -1386,32 +1386,34 @@
 Visibility(View.
+IN
 VISIBLE);%0A      
@@ -1405,32 +1405,60 @@
 ISIBLE);%0A       
+ if (getEmptyView() != null)
  getEmptyView().
@@ -1507,39 +1507,40 @@
 public void show
-Content
+Progress
 View() %7B%0A       
@@ -1531,32 +1531,62 @@
 View() %7B%0A       
+ if (getContentView() != null)
  getContentView(
@@ -1598,32 +1598,34 @@
 Visibility(View.
+IN
 VISIBLE);%0A      
@@ -1617,32 +1617,63 @@
 ISIBLE);%0A       
+ if (getProgressView() != null)
  getProgressView
@@ -1686,34 +1686,32 @@
 Visibility(View.
-IN
 VISIBLE);%0A      
@@ -1703,32 +1703,60 @@
 ISIBLE);%0A       
+ if (getEmptyView() != null)
  getEmptyView().
@@ -1830,24 +1830,54 @@
 () %7B%0A       
+ if (getContentView() != null)
  getContentV
@@ -1916,24 +1916,55 @@
 LE);%0A       
+ if (getProgressView() != null)
  getProgress
@@ -2008,16 +2008,44 @@
 %0A       
+ if (getEmptyView() != null)
  getEmpt
@@ -2104,21 +2104,20 @@
 ic void 
-toast
+show
 Error(fi
@@ -2689,13 +2689,12 @@
     
-toast
+show
 Erro

Update view manager to ignore null views