Paged change details fragment: Add tab to reply

The PostReviewView already implements the controls for replying to a
change, however for displaying it in a tab the layout needs to be a
bit different (e.g. the panel with the publish buttons should be shown
at the bottom and not at the top). In addition when it is displayed in
a tab there is no need to show the commit message and the approvals as
there are extra tabs for this and it's easy to switch back and forth
between tabs. To support both layouts PostReviewView now provides 2
variants of itself.

Change-Id: Id99e18a5503e3707758d49dc18c868d5a3250576
Signed-off-by: Edwin Kempin <ekempin@google.com>
diff --git a/app/src/main/java/com/google/reviewit/PagedChangeDetailsFragment.java b/app/src/main/java/com/google/reviewit/PagedChangeDetailsFragment.java
index 45c4eed..72f5de8 100644
--- a/app/src/main/java/com/google/reviewit/PagedChangeDetailsFragment.java
+++ b/app/src/main/java/com/google/reviewit/PagedChangeDetailsFragment.java
@@ -64,7 +64,11 @@
     fileListFragment.setChange(change);
     fragments.add(fileListFragment);
 
-    // TODO add more tabs, e.g. for change messages, approvals and post review
+    ReplyFragment replyFragment = new ReplyFragment();
+    replyFragment.setChange(change);
+    fragments.add(replyFragment);
+
+    // TODO add more tabs, e.g. for change messages and approvals
 
     ViewPager pager = ((ViewPager) v(R.id.pager));
     pager.setAdapter(
diff --git a/app/src/main/java/com/google/reviewit/PostReviewFragment.java b/app/src/main/java/com/google/reviewit/PostReviewFragment.java
index f855c53..d85b636 100644
--- a/app/src/main/java/com/google/reviewit/PostReviewFragment.java
+++ b/app/src/main/java/com/google/reviewit/PostReviewFragment.java
@@ -56,7 +56,8 @@
     Change change = getApp().getCurrentChange();
 
     setTitle(getString(R.string.detailed_change_title, change.info._number));
-    ((PostReviewView)v(R.id.postReview)).init(getApp(), this, vote,
-        SortChangesFragment.class);
+    ((PostReviewView) v(R.id.postReview))
+        .initWithExpandableCommitMessageAndApprovals(getApp(), this, vote,
+            SortChangesFragment.class);
   }
 }
diff --git a/app/src/main/java/com/google/reviewit/ReplyFragment.java b/app/src/main/java/com/google/reviewit/ReplyFragment.java
new file mode 100644
index 0000000..33fecb8
--- /dev/null
+++ b/app/src/main/java/com/google/reviewit/ReplyFragment.java
@@ -0,0 +1,65 @@
+// Copyright (C) 2016 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.reviewit;
+
+import android.os.Bundle;
+import android.support.annotation.StringRes;
+
+import com.google.gerrit.extensions.common.ApprovalInfo;
+import com.google.reviewit.app.Change;
+import com.google.reviewit.widget.PostReviewView;
+
+import java.util.List;
+
+public class ReplyFragment extends PageFragment {
+  private Change change;
+
+  @Override
+  protected int getLayout() {
+    return R.layout.content_reply;
+  }
+
+  @Override
+  public @StringRes
+  int getTitle() {
+    return R.string.reply_title;
+  }
+
+  @Override
+  public void onActivityCreated(Bundle savedInstanceState) {
+    super.onActivityCreated(savedInstanceState);
+
+    List<ApprovalInfo> codeReviewApprovals =
+        (change.info.labels != null
+            && change.info.labels.get("Code-Review") != null)
+            ? change.info.labels.get("Code-Review").all : null;
+    int vote = 0;
+    if (codeReviewApprovals != null) {
+      for (ApprovalInfo approval : codeReviewApprovals) {
+        if (approval._accountId.equals(getApp().getSelf()._accountId)) {
+          vote = approval.value;
+          break;
+        }
+      }
+    }
+
+    ((PostReviewView) v(R.id.postReview)).initForDisplayInTab(
+        getApp(), this, vote, PagedChangeDetailsFragment.class);
+  }
+
+  public void setChange(Change change) {
+    this.change = change;
+  }
+}
diff --git a/app/src/main/java/com/google/reviewit/widget/PostReviewView.java b/app/src/main/java/com/google/reviewit/widget/PostReviewView.java
index 1d62d19..1e657a1 100644
--- a/app/src/main/java/com/google/reviewit/widget/PostReviewView.java
+++ b/app/src/main/java/com/google/reviewit/widget/PostReviewView.java
@@ -53,26 +53,47 @@
   public PostReviewView(Context context, AttributeSet attrs, int defStyle) {
     super(context, attrs, defStyle);
     this.widgetUtil = new WidgetUtil(getContext());
-    inflate(context, R.layout.post_review, this);
   }
 
-  public void init(ReviewItApp app, BaseFragment origin, int vote,
-                   Class<? extends BaseFragment> target) {
+  public void initWithExpandableCommitMessageAndApprovals(
+      ReviewItApp app, BaseFragment origin, int vote,
+      Class<? extends BaseFragment> target) {
+    inflate(getContext(), R.layout.post_review, this);
+    init(app, origin, vote, target);
+  }
+
+  public void initForDisplayInTab(
+      ReviewItApp app, BaseFragment origin, int vote,
+      Class<? extends BaseFragment> target) {
+    inflate(getContext(), R.layout.post_review_tab, this);
+    init(app, origin, vote, target);
+  }
+
+  private void init(
+      ReviewItApp app, BaseFragment origin, int vote,
+      Class<? extends BaseFragment> target) {
     update(vote);
     Change change = app.getCurrentChange();
 
     init(origin, change, target);
     initLabels(change, vote);
-    ((ApprovalsView) findViewById(R.id.approvals)).displayApprovals(app,
-        change.info, origin);
+
+    ExpandableCommitMessageView expandableCommitMessage =
+        (ExpandableCommitMessageView) findViewById(R.id.commitMessage);
+    if (expandableCommitMessage != null) {
+      expandableCommitMessage.init(change);
+    }
+
+    ApprovalsView approvalsView =
+        ((ApprovalsView) findViewById(R.id.approvals));
+    if (approvalsView != null) {
+      approvalsView.displayApprovals(app, change.info, origin);
+    }
   }
 
   private void init(
       final BaseFragment origin, final Change change,
       final Class<? extends BaseFragment> target) {
-    ((ExpandableCommitMessageView)findViewById(R.id.commitMessage))
-        .init(change);
-
     findViewById(R.id.postReviewButton).setOnClickListener(
         new View.OnClickListener() {
           @Override
diff --git a/app/src/main/res/layout/content_reply.xml b/app/src/main/res/layout/content_reply.xml
new file mode 100644
index 0000000..b4cfd23
--- /dev/null
+++ b/app/src/main/res/layout/content_reply.xml
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<!-- Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License. -->
+
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+  android:orientation="vertical"
+  android:layout_width="match_parent"
+  android:layout_height="match_parent">
+
+  <com.google.reviewit.widget.PostReviewView
+    android:id="@+id/postReview"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"/>
+</LinearLayout>
diff --git a/app/src/main/res/layout/post_review_tab.xml b/app/src/main/res/layout/post_review_tab.xml
new file mode 100644
index 0000000..82fe696
--- /dev/null
+++ b/app/src/main/res/layout/post_review_tab.xml
@@ -0,0 +1,126 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<!-- Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License. -->
+
+<LinearLayout
+  xmlns:android="http://schemas.android.com/apk/res/android"
+  xmlns:app="http://schemas.android.com/apk/res-auto"
+  android:layout_width="match_parent"
+  android:layout_height="match_parent"
+  android:orientation="vertical"
+  app:layout_behavior="@string/appbar_scrolling_view_behavior">
+
+  <include layout="@layout/progress"/>
+
+  <ScrollView
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:paddingLeft="5dp"
+    android:paddingRight="5dp"
+    android:layout_marginTop="5dp"
+    android:layout_weight="1">
+
+    <LinearLayout
+      android:orientation="vertical"
+      android:layout_width="match_parent"
+      android:layout_height="wrap_content">
+
+      <TextView
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:text="@string/message"
+        android:textSize="15dp"/>
+
+      <EditText
+        android:id="@+id/changeMessageInput"
+        android:inputType="textMultiLine"
+        android:lines="6"
+        android:gravity="top|left"
+        android:layout_height="wrap_content"
+        android:layout_width="match_parent"
+        android:scrollbars="vertical"/>
+
+      <LinearLayout
+        android:id="@+id/voteInput"
+        android:orientation="vertical"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:layout_marginTop="10dp"
+        android:layout_marginBottom="10dp">
+      </LinearLayout>
+    </LinearLayout>
+  </ScrollView>
+
+  <LinearLayout
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:orientation="vertical"
+    android:layout_marginTop="5dp"
+    android:background="@drawable/navigation_button_bar">
+
+    <LinearLayout
+      android:layout_width="match_parent"
+      android:layout_height="wrap_content"
+      android:orientation="horizontal">
+
+      <ImageView
+        android:id="@+id/emoticon"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_marginLeft="7dp"
+        android:layout_marginTop="3dp"
+        android:clickable="true"/>
+
+      <View
+        android:layout_width="0dp"
+        android:layout_height="0dp"
+        android:layout_weight="1"/>
+
+      <Button
+        android:id="@+id/postReviewButton"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:background="@color/button"
+        android:textColor="@color/buttonFont"
+        android:layout_marginTop="5dp"
+        android:layout_marginBottom="5dp"
+        android:paddingLeft="5dp"
+        android:paddingRight="5dp"
+        android:text="@string/publish"/>
+
+      <View
+        android:layout_width="0dp"
+        android:layout_height="0dp"
+        android:layout_weight="1"/>
+
+      <Button
+        android:id="@+id/postReviewAndSubmitButton"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:background="@color/button"
+        android:textColor="@color/buttonFont"
+        android:layout_marginTop="5dp"
+        android:layout_marginBottom="5dp"
+        android:paddingLeft="5dp"
+        android:paddingRight="5dp"
+        android:text="@string/publishAndSubmit"/>
+
+      <View
+        android:layout_width="0dp"
+        android:layout_height="0dp"
+        android:layout_weight="1"/>
+    </LinearLayout>
+  </LinearLayout>
+</LinearLayout>
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 08a5cc4..e79741b 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -253,6 +253,9 @@
   <!-- FileListFragment -->
   <string name="file_list_title">Files</string>
 
+  <!-- ReplyFragment -->
+  <string name="reply_title">Reply</string>
+
   <!-- RelativeDateFormatter -->
   <string name="in_the_future">in the future</string>
   <string name="month">m</string>