Skip to content

Commit

Permalink
Add date to list and sort by date
Browse files Browse the repository at this point in the history
- date is shown in the list for many plants at once
- plants are sorted by creation, latest first
  • Loading branch information
niccokunzmann committed Aug 7, 2018
1 parent 533db32 commit 90ad22d
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 27 deletions.
7 changes: 4 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@ android {
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:25.4.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:support-v4:25.4.0'
implementation 'com.android.support:design:25.4.0'
testImplementation 'junit:junit:4.12'

// this is the last support test for api >= 9
androidTestImplementation 'com.android.support.test:runner:1.0.0'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.0'
implementation 'com.android.support:recyclerview-v7:25.4.0'
implementation "commons-io:commons-io:+" // from https://stackoverflow.com/a/23810852/1320237
implementation 'commons-io:commons-io:2.4'
// from https://stackoverflow.com/a/23810852/1320237
// from https://stackoverflow.com/a/31372941/1320237
implementation files('libs/commons-lang3-3.7.jar')
}
Binary file added app/libs/commons-lang3-3.7.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class PlantDetailFragment extends Fragment {
* The dummy content this fragment is presenting.
*/
private Plant plant;
private boolean showDate;

/**
* Mandatory empty constructor for the fragment manager to instantiate the
Expand All @@ -48,8 +49,7 @@ public void onCreate(Bundle savedInstanceState) {
// Load the dummy content specified by the fragment
// arguments. In a real-world scenario, use a Loader
// to load content from a content provider.
String id = getArguments().getString(ARG_PLANT_ID);
plant = Plant.withId(id);
plant = Plant.withId(getArguments().getString(ARG_PLANT_ID));

Activity activity = this.getActivity();
CollapsingToolbarLayout appBarLayout = (CollapsingToolbarLayout) activity.findViewById(R.id.toolbar_layout);
Expand Down Expand Up @@ -80,7 +80,8 @@ public View onCreateView(final LayoutInflater inflater, final ViewGroup containe
public void onClick(View v) {
Intent intent = new Intent(context, LoginActivity.class);
// intent.putExtra(NewPlantActivity.ARG_PLANT_ID, plantId);
context.startActivity(intent); }
context.startActivity(intent);
}
});
}
return rootView;
Expand Down
37 changes: 35 additions & 2 deletions app/src/main/java/eu/quelltext/mundraub/PlantListActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.Collections;
import java.util.Date;
import java.util.List;

import eu.quelltext.mundraub.plant.Plant;
Expand Down Expand Up @@ -112,6 +115,7 @@ public void onClick(View view) {
List<Plant> items,
boolean twoPane) {
mValues = items;
Collections.sort(mValues);
mParentActivity = parent;
mTwoPane = twoPane;
}
Expand All @@ -127,6 +131,16 @@ public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
public void onBindViewHolder(final ViewHolder holder, int position) {
Plant plant = mValues.get(position);
holder.fillFromPlant(plant);
if (position == 0) {
holder.showDate(plant);
} else {
Plant lastPlant = mValues.get(position - 1);
if (plant.getCreationDay().equals(lastPlant.getCreationDay())) {
holder.hideDate();
} else {
holder.showDate(plant);
}
}

}

Expand All @@ -138,11 +152,17 @@ public int getItemCount() {
class ViewHolder extends RecyclerView.ViewHolder {
private final ImageView plantImage;
private final TextView plantCategoryText;
private final LinearLayout dateContainer;
private final String dateFormat;
private final TextView dateText;

ViewHolder(View view) {
super(view);
plantImage = (ImageView) view.findViewById(R.id.image_plant);
plantCategoryText = (TextView) view.findViewById(R.id.plant_category);
dateContainer = (LinearLayout) view.findViewById(R.id.new_day);
dateFormat = view.getResources().getString(R.string.plant_list_date_format);
dateText = (TextView) view.findViewById(R.id.date);
}

public void fillFromPlant(Plant plant) {
Expand All @@ -151,7 +171,20 @@ public void fillFromPlant(Plant plant) {
String textWithCount = Integer.toString(plant.getCount()) + "x " + plantCategoryText.getText().toString();
plantCategoryText.setText(textWithCount);
itemView.setTag(plant);
itemView.setOnClickListener(mOnClickListener); }
itemView.setOnClickListener(mOnClickListener);
}

public void showDate(Plant plant) {
Date date = plant.getCreationDay();
String text = DateFormat.format(dateFormat, date).toString();
dateText.setText(text);
dateContainer.setVisibility(View.VISIBLE);
}

public void hideDate() {
// from https://stackoverflow.com/a/7348547/1320237
dateContainer.setVisibility(View.GONE);
}
}
}
}
20 changes: 19 additions & 1 deletion app/src/main/java/eu/quelltext/mundraub/plant/Plant.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,23 @@
import android.location.Location;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.util.Log;
import android.widget.ImageView;

import org.apache.commons.lang3.time.DateUtils;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Calendar;


public class Plant {

public class Plant implements Comparable<Plant> {

private static final PlantCollection plants = new PersistentPlantCollection();
private static final String JSON_LONGITUDE = "longitude";
Expand Down Expand Up @@ -213,4 +218,17 @@ public boolean movePictureTo(File file) {
}
return false;
}

public Date createdAt() {
return plants.getCreationDate(this);
}

@Override
public int compareTo(@NonNull Plant other) {
return -createdAt().compareTo(other.createdAt());
}

public Date getCreationDay() {
return DateUtils.truncate(createdAt(), Calendar.DAY_OF_MONTH);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

import android.text.format.DateFormat;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PlantCollection {

private Map<String, Plant> idToPlant;
private final String ID_FORMAT = "yyyy-MM-dd-kk-mm-ss";

public PlantCollection() {
idToPlant = new HashMap<String, Plant>();
Expand All @@ -23,7 +27,7 @@ public List<Plant> all() {
public String newId() {
// from https://stackoverflow.com/a/6953926
// from https://stackoverflow.com/a/24423756/1320237
return DateFormat.format("yyyy-MM-dd-kk-mm-ss", Calendar.getInstance().getTime()).toString();
return DateFormat.format(ID_FORMAT, Calendar.getInstance().getTime()).toString();
}

public Plant withId(String id) {
Expand All @@ -41,4 +45,14 @@ public void delete(Plant plant) {
public boolean contains(String id) {
return idToPlant.containsKey(id);
}

public Date getCreationDate(Plant plant) {
SimpleDateFormat parser = new SimpleDateFormat(ID_FORMAT);
try {
return parser.parse(plant.getId());
} catch (ParseException e) {
e.printStackTrace();
return Calendar.getInstance().getTime();
}
}
}
64 changes: 48 additions & 16 deletions app/src/main/res/layout/plant_list_content.xml
Original file line number Diff line number Diff line change
@@ -1,28 +1,60 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
android:orientation="vertical">

<ImageView
android:id="@+id/image_plant"
android:layout_width="wrap_content"
android:layout_height="match_parent"
<LinearLayout
android:id="@+id/new_day"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:adjustViewBounds="true"
android:cropToPadding="true"
android:scaleType="center"
app:srcCompat="@android:drawable/ic_menu_gallery" />
android:orientation="vertical">

<View
android:id="@+id/divider"
android:layout_width="match_parent"
android:layout_height="3dp"
android:layout_weight="1"
android:background="@color/colorPrimary" />

<TextView
android:id="@+id/plant_category"
<TextView
android:id="@+id/date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:layout_weight="1"
android:text="12. Juni 2016" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin"
android:layout_weight="1"
android:text="@string/unknown_plant"
android:textAppearance="?attr/textAppearanceListItem"
android:textSize="24sp" />
android:orientation="horizontal">

<ImageView
android:id="@+id/image_plant"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:adjustViewBounds="true"
android:cropToPadding="true"
android:scaleType="center"
app:srcCompat="@android:drawable/ic_menu_gallery" />

<TextView
android:id="@+id/plant_category"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin"
android:layout_weight="1"
android:text="@string/unknown_plant"
android:textAppearance="?attr/textAppearanceListItem"
android:textSize="24sp" />
</LinearLayout>

</LinearLayout>
2 changes: 1 addition & 1 deletion app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimary">#3f51b5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@
<string name="error_incorrect_password">This password is incorrect</string>
<string name="error_field_required">This field is required</string>
<string name="upload_plant">Upload Plant</string>
<string name="plant_list_date_format">dd. MM. yyyy</string>
</resources>

0 comments on commit 90ad22d

Please sign in to comment.