Use more databindings

This commit is contained in:
Phillip Thelen 2015-08-03 18:14:20 +02:00
parent bbe80a30c4
commit efecb79e81
10 changed files with 114 additions and 121 deletions

View file

@ -2,11 +2,11 @@
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<import type="com.magicmicky.habitrpgwrapper.lib.models.tasks.HabitItem" />
<import type="com.magicmicky.habitrpgwrapper.lib.models.tasks.Daily" />
<variable
name="daily"
type="HabitItem" />
type="Daily" />
</data>
<FrameLayout xmlns:app="http://schemas.android.com/apk/res-auto"
@ -24,7 +24,8 @@
app:cardCornerRadius="2dp"
app:cardElevation="2dp"
app:cardPreventCornerOverlap="false"
app:contentPadding="0dp">
app:contentPadding="0dp"
app:cardColor="@{daily.getLightTaskColor}">
<LinearLayout
android:orientation="horizontal"
@ -38,7 +39,8 @@
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:checked="false"
android:checked="@{daily.completed}"
app:backgroundColor="@{daily.getDarkTaskColor}"
android:gravity="center"/>
<LinearLayout

View file

@ -3,11 +3,12 @@
<data>
<import type="com.magicmicky.habitrpgwrapper.lib.models.tasks.HabitItem" />
<import type="com.magicmicky.habitrpgwrapper.lib.models.tasks.Habit" />
<import type="android.view.View"/>
<variable
name="habit"
type="HabitItem" />
type="Habit" />
</data>
<FrameLayout xmlns:app="http://schemas.android.com/apk/res-auto"
@ -25,7 +26,8 @@
app:cardCornerRadius="2dp"
app:cardElevation="2dp"
app:cardPreventCornerOverlap="false"
app:contentPadding="0dp">
app:contentPadding="0dp"
app:cardColor="@{habit.getLightTaskColor}">
<RelativeLayout
android:layout_width="match_parent"
@ -33,27 +35,33 @@
android:gravity="fill_horizontal|center_vertical|center_horizontal|fill|clip_vertical|fill_vertical|center|start|clip_horizontal|bottom|left|right|end|top"
android:minHeight="250dp">
<Button
android:id="@+id/btnPlus"
style="?android:attr/buttonStyleSmall"
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="+" />
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/btnLayout">
<Button
android:id="@+id/btnPlus"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:visibility="@{habit.up ? View.VISIBLE : View.GONE}"
android:text="+"
android:textSize="15sp"
app:backgroundColor="@{habit.getDarkTaskColor}" />
<Button
android:id="@+id/btnMinus"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btnPlus"
android:layout_alignStart="@+id/btnPlus"
android:layout_below="@+id/btnPlus"
android:backgroundTint="@color/best"
android:text="-" />
<Button
android:id="@+id/btnMinus"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:visibility="@{habit.down ? View.VISIBLE : View.GONE}"
app:backgroundColor="@{habit.getDarkTaskColor}"
android:text="-"
android:textSize="15sp"/>
</LinearLayout>
<CheckedTextView
android:id="@+id/checkedTextView"
@ -63,10 +71,11 @@
android:layout_alignParentBottom="false"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="@id/btnPlus"
android:layout_toEndOf="@id/btnPlus"
android:layout_toRightOf="@id/btnLayout"
android:layout_toEndOf="@id/btnLayout"
android:layout_alignParentTop="true"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingEnd="5dp"
android:paddingTop="5dp"

View file

@ -87,7 +87,8 @@
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dp" />
android:layout_marginLeft="5dp"
app:imageName="@{reward.id}"/>
</RelativeLayout>
</android.support.v7.widget.CardView>

View file

@ -2,11 +2,11 @@
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<import type="com.magicmicky.habitrpgwrapper.lib.models.tasks.HabitItem" />
<import type="com.magicmicky.habitrpgwrapper.lib.models.tasks.ToDo" />
<variable
name="todo"
type="HabitItem" />
type="ToDo" />
</data>
<FrameLayout xmlns:app="http://schemas.android.com/apk/res-auto"
@ -24,7 +24,8 @@
app:cardCornerRadius="2dp"
app:cardElevation="2dp"
app:cardPreventCornerOverlap="false"
app:contentPadding="0dp">
app:contentPadding="0dp"
app:cardColor="@{todo.getLightTaskColor}">
<LinearLayout
android:orientation="horizontal"
@ -38,7 +39,8 @@
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:checked="false"
android:checked="@{todo.completed}"
app:backgroundColor="@{todo.getDarkTaskColor}"
android:gravity="center"/>
<LinearLayout

View file

@ -1,6 +1,7 @@
package com.habitrpg.android.habitica.ui.adapter;
import android.content.Context;
import android.databinding.BindingAdapter;
import android.databinding.DataBindingUtil;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableList;
@ -17,6 +18,7 @@ import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CheckedTextView;
import android.widget.CompoundButton;
import android.widget.ImageView;
import com.habitrpg.android.habitica.R;
import com.habitrpg.android.habitica.databinding.DailyItemCardBinding;
@ -28,7 +30,6 @@ import com.habitrpg.android.habitica.events.HabitScoreEvent;
import com.habitrpg.android.habitica.events.TaskLongPressedEvent;
import com.habitrpg.android.habitica.events.TaskTappedEvent;
import com.habitrpg.android.habitica.events.TodoCheckedEvent;
import com.habitrpg.android.habitica.ui.helpers.HabitColorHelper;
import com.habitrpg.android.habitica.ui.helpers.ViewHelper;
import com.magicmicky.habitrpgwrapper.lib.models.tasks.Daily;
import com.magicmicky.habitrpgwrapper.lib.models.tasks.Habit;
@ -204,6 +205,26 @@ public class HabitItemRecyclerViewAdapter<THabitItem extends HabitItem>
handler.postDelayed(reloadContentRunable, 200);
}
@BindingAdapter("bind:imageName")
public static void loadImage(ImageView view, String imageName) {
Picasso.with(view.getContext()).load("https://habitica-assets.s3.amazonaws.com/mobileApp/images/shop_"+ imageName +".png").into(view);
}
@BindingAdapter("bind:cardColor")
public static void setCardColor(CardView cardView, int color) {
cardView.setCardBackgroundColor(cardView.getResources().getColor(color));
}
@BindingAdapter("app:backgroundColor")
public static void setBackgroundTintColor(CheckBox view, int color) {
ViewHelper.SetBackgroundTint(view, view.getResources().getColor(color));
}
@BindingAdapter("app:backgroundColor")
public static void setBackgroundTintColor(Button view, int color) {
ViewHelper.SetBackgroundTint(view, view.getResources().getColor(color));
}
public abstract class ViewHolder<THabitItem extends HabitItem> extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
@InjectView(R.id.card_view)
@ -216,12 +237,6 @@ public class HabitItemRecyclerViewAdapter<THabitItem extends HabitItem>
public THabitItem Item;
public void SetCardBackgroundColor(int color) {
if (cardView != null) {
cardView.setCardBackgroundColor(color);
}
}
public ViewHolder(View itemView) {
super(itemView);
@ -238,9 +253,6 @@ public class HabitItemRecyclerViewAdapter<THabitItem extends HabitItem>
public void bindHolder(THabitItem habitItem, int position) {
double itemvalue = habitItem.getValue();
int itemColorRes = HabitColorHelper.GetItemColorByValue(itemvalue);
SetCardBackgroundColor(resources.getColor(itemColorRes));
Item = habitItem;
}
@ -264,6 +276,7 @@ public class HabitItemRecyclerViewAdapter<THabitItem extends HabitItem>
return true;
}
}
public class HabitViewHolder extends ViewHolder<Habit> {
@ -309,12 +322,6 @@ public class HabitItemRecyclerViewAdapter<THabitItem extends HabitItem>
super.bindHolder(habitItem, position);
binding.setHabit(habitItem);
double itemvalue = habitItem.getValue();
int btnColorRes = HabitColorHelper.GetItemButtonColorByValue(itemvalue);
ViewHelper.SetBackgroundTint(btnPlus, resources.getColor(btnColorRes));
ViewHelper.SetBackgroundTint(btnMinus, resources.getColor(btnColorRes));
}
}
@ -336,11 +343,6 @@ public class HabitItemRecyclerViewAdapter<THabitItem extends HabitItem>
super.bindHolder(habitItem, position);
binding.setDaily(habitItem);
double itemvalue = habitItem.getValue();
int btnColorRes = HabitColorHelper.GetItemButtonColorByValue(itemvalue);
ViewHelper.SetBackgroundTint(checkbox, resources.getColor(btnColorRes));
}
}
@ -364,11 +366,6 @@ public class HabitItemRecyclerViewAdapter<THabitItem extends HabitItem>
super.bindHolder(habitItem, position);
binding.setTodo(habitItem);
double itemvalue = habitItem.getValue();
int btnColorRes = HabitColorHelper.GetItemButtonColorByValue(itemvalue);
ViewHelper.SetBackgroundTint(checkbox, resources.getColor(btnColorRes));
}
@Override
@ -439,12 +436,7 @@ public class HabitItemRecyclerViewAdapter<THabitItem extends HabitItem>
@Override
public void bindHolder(RewardItem habitItem, int position) {
super.bindHolder(habitItem, position);
binding.setReward(habitItem);
binding.imageView3.setImageBitmap(null);
Picasso.with(context).load("https://habitica-assets.s3.amazonaws.com/mobileApp/images/shop_"+ habitItem.getId() +".png").into(this);
}
@Override

View file

@ -1,50 +0,0 @@
package com.habitrpg.android.habitica.ui.helpers;
import com.habitrpg.android.habitica.R;
public class HabitColorHelper {
/**
* Get the color resources depending on a certain score
*
* @param d the score
* @return the color resource id
*/
public static int GetItemColorByValue(double d)
{
if (d < -20)
return R.color.worst;
if (d < -10)
return R.color.worse;
if (d < -1)
return R.color.bad;
if (d < 5)
return R.color.neutral;
if (d < 10)
return R.color.better;
return R.color.best;
}
/**
* Get the button color resources depending on a certain score
*
* @param d the score
* @return the color resource id
*/
public static int GetItemButtonColorByValue(double d)
{
if (d < -20)
return R.color.worst_btn;
if (d < -10)
return R.color.worse_btn;
if (d < -1)
return R.color.bad_btn;
if (d < 5)
return R.color.neutral_btn;
if (d < 10)
return R.color.better_btn;
return R.color.best_btn;
}
}

View file

@ -24,7 +24,7 @@ public class Daily extends Checklist{
private final HabitType type=HabitType.daily;
@Column
private Boolean completed;
public Boolean completed;
@Column
private String frequency;

View file

@ -16,10 +16,7 @@ public class Habit extends HabitItem{
private final HabitType type = HabitType.habit;
@Column
private Boolean up;
@Column
private Boolean down;
public Boolean up, down;
/**
* Create a new Habit based on all the information needed
* @param id the id of the habit

View file

@ -1,6 +1,9 @@
package com.magicmicky.habitrpgwrapper.lib.models.tasks;
import android.graphics.Color;
import com.habitrpg.android.habitica.HabitDatabase;
import com.habitrpg.android.habitica.R;
import com.raizlabs.android.dbflow.annotation.Column;
import com.raizlabs.android.dbflow.annotation.PrimaryKey;
import com.raizlabs.android.dbflow.annotation.Table;
@ -195,4 +198,41 @@ public abstract class HabitItem extends BaseModel implements Serializable {
public void setAttribute(String attribute) {
this.attribute = attribute;
}
public int getLightTaskColor()
{
if (this.value < -20)
return R.color.worst;
if (this.value < -10)
return R.color.worse;
if (this.value < -1)
return R.color.bad;
if (this.value < 5)
return R.color.neutral;
if (this.value < 10)
return R.color.better;
return R.color.best;
}
/**
* Get the button color resources depending on a certain score
*
* @param d the score
* @return the color resource id
*/
public int getDarkTaskColor()
{
if (this.value < -20)
return R.color.worst_btn;
if (this.value < -10)
return R.color.worse_btn;
if (this.value < -1)
return R.color.bad_btn;
if (this.value < 5)
return R.color.neutral_btn;
if (this.value < 10)
return R.color.better_btn;
return R.color.best_btn;
}
}

View file

@ -23,10 +23,10 @@ public class ToDo extends Checklist{
private final HabitType type=HabitType.todo;
@Column
private Boolean completed;
public Boolean completed;
@Column
private String date;
public String date;
/**
* Construct a daily based on all the information needed
* @param id the id of the daily