Skip to content

Commit

Permalink
Merge pull request #44 from CMPUT301F21T44/feat/view-habit-events
Browse files Browse the repository at this point in the history
Feature: View habit events
  • Loading branch information
CxLeo authored Oct 27, 2021
2 parents c8fd15b + aad3a0c commit 407f8b8
Show file tree
Hide file tree
Showing 23 changed files with 389 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package com.github.cmput301f21t44.hellohabits.db.habitevent;

import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update;

import java.util.List;

@Dao
public interface HabitEventDao {
@Query("SELECT * FROM habit_event WHERE habit_id=:id")
LiveData<List<HabitEventEntity>> getEventsByHabitId(String id);

@Insert
void insert(HabitEventEntity habitEvent);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,15 @@ public HabitEventEntity(@NonNull String habitEventId, @NonNull String habitId,
this.mEmbeddedLocation = embeddedLocation;
}

public HabitEventEntity(@NonNull String habitEventId, @NonNull String habitId,
@NonNull Instant date, String comment, String photoPath,
Location location) {
this(habitEventId, habitId, date, comment, photoPath, EmbeddedLocation.from(location));
}

public HabitEventEntity(@NonNull String habitId, @NonNull Instant date, String comment,
String photoPath, Location location) {
this(UUID.randomUUID().toString(), habitId, date, comment, photoPath, EmbeddedLocation.from(location));
this(UUID.randomUUID().toString(), habitId, date, comment, photoPath, location);
}

public static HabitEventEntity from(HabitEvent habitEvent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import android.app.Application;

import androidx.lifecycle.LiveData;

import com.github.cmput301f21t44.hellohabits.db.AppDatabase;
import com.github.cmput301f21t44.hellohabits.model.HabitEventRepository;
import com.github.cmput301f21t44.hellohabits.model.Location;

import java.time.Instant;
import java.util.List;

public class HabitEventEntityRepository implements HabitEventRepository<HabitEventEntity> {
private final HabitEventDao mHabitEventDao;
Expand All @@ -16,6 +19,10 @@ public HabitEventEntityRepository(Application application) {
mHabitEventDao = db.habitEventDao();
}

public LiveData<List<HabitEventEntity>> getEventsByHabitId(String habitId) {
return mHabitEventDao.getEventsByHabitId(habitId);
}

@Override
public void insert(String habitId, String comment) {
HabitEventEntity habitEvent = new HabitEventEntity(habitId, Instant.now(), comment, null,
Expand All @@ -29,8 +36,8 @@ public void delete(HabitEventEntity habitEvent) {
}

@Override
public HabitEventEntity update(String id, Instant date, String comment, String photoPath, Location location) {
HabitEventEntity updatedEvent = new HabitEventEntity(id, date, comment, photoPath, location);
public HabitEventEntity update(String id,String habitId, Instant date, String comment, String photoPath, Location location) {
HabitEventEntity updatedEvent = new HabitEventEntity(id,habitId, date, comment, photoPath, location);
AppDatabase.databaseWriteExecutor.execute(() -> mHabitEventDao.update(updatedEvent));
return updatedEvent;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ public interface HabitEventRepository<T extends HabitEvent> {

void delete(T habitEvent);

T update(String id, Instant date, String comment, String photoPath, Location location);
T update(String id, String habitId, Instant date, String comment, String photoPath, Location location);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,10 @@ public interface Location {
double getLatitude();

double getAccuracy();

default boolean equals(Location other) {
return this.getAccuracy() == other.getAccuracy()
&& this.getLatitude() == other.getLatitude()
&& this.getLongitude() == this.getLongitude();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@
import com.github.cmput301f21t44.hellohabits.R;
import com.github.cmput301f21t44.hellohabits.databinding.FragmentAllHabitsBinding;
import com.github.cmput301f21t44.hellohabits.model.Habit;
import com.github.cmput301f21t44.hellohabits.view.OnItemClickListener;
import com.github.cmput301f21t44.hellohabits.viewmodel.HabitViewModel;

import java.util.ArrayList;
import java.util.List;

public class AllHabitsFragment extends Fragment implements OnItemClickListener<Habit> {
public class AllHabitsFragment extends Fragment {
private FragmentAllHabitsBinding binding;
private HabitViewModel mHabitViewModel;
private HabitAdapter adapter;
Expand All @@ -35,13 +34,16 @@ public View onCreateView(@NonNull LayoutInflater inflater,
return binding.getRoot();
}


@Override
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mNavController = NavHostFragment.findNavController(this);
// attach the provider to activity instead of fragment so the fragments can share data
mHabitViewModel = new ViewModelProvider(requireActivity()).get(HabitViewModel.class);
adapter = new HabitAdapter(new HabitAdapter.HabitDiff(), this);
adapter = HabitAdapter.newInstance((habit) -> {
mHabitViewModel.select(habit);
mNavController.navigate(R.id.action_allHabitsFragment_to_viewHabitFragment);
});
binding.habitRecyclerView.setAdapter(adapter);
binding.habitRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

Expand All @@ -65,10 +67,4 @@ public void onDestroyView() {
super.onDestroyView();
binding = null;
}

@Override
public void onItemClick(Habit habit) {
mHabitViewModel.select(habit);
mNavController.navigate(R.id.action_allHabitsFragment_to_viewHabitFragment);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import java.time.format.DateTimeFormatter;

public class CreateEditHabitFragment extends Fragment {
private static final String ERROR_MESSAGE = "Input is too long";
public static final String ERROR_MESSAGE = "Input is too long";
private static final int MAX_TITLE_LEN = 20;
private static final int MAX_REASON_LEN = 30;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,33 @@
import androidx.recyclerview.widget.RecyclerView;

import com.github.cmput301f21t44.hellohabits.R;
import com.github.cmput301f21t44.hellohabits.databinding.LlistHabitItemBinding;
import com.github.cmput301f21t44.hellohabits.databinding.ListHabitItemBinding;
import com.github.cmput301f21t44.hellohabits.model.Habit;
import com.github.cmput301f21t44.hellohabits.view.OnItemClickListener;

import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
public class HabitAdapter extends ListAdapter<Habit, HabitAdapter.ViewHolder> {
private OnItemClickListener<Habit> listener;

public class HabitAdapter extends ListAdapter<Habit, HabitAdapter.HabitHolder> {
private final OnItemClickListener<Habit> listener;

public HabitAdapter(@NonNull DiffUtil.ItemCallback<Habit> diffCallback, OnItemClickListener<Habit> listener) {
public HabitAdapter(@NonNull DiffUtil.ItemCallback<Habit> diffCallback) {
super(diffCallback);
this.listener = listener;
}

public static HabitAdapter newInstance(OnItemClickListener<Habit> listener) {
HabitAdapter adapter = new HabitAdapter(new HabitDiff());
adapter.listener = listener;
return adapter;
}

@NonNull
@Override
public HabitHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LlistHabitItemBinding itemBinding = LlistHabitItemBinding.inflate(
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
ListHabitItemBinding itemBinding = ListHabitItemBinding.inflate(
LayoutInflater.from(parent.getContext()), parent, false);
return new HabitHolder(itemBinding);
return new ViewHolder(itemBinding);
}

@Override
public void onBindViewHolder(@NonNull HabitHolder holder, int position) {
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Habit current = getItem(position);
holder.bind(current, listener);
}
Expand All @@ -55,10 +54,10 @@ public boolean areContentsTheSame(@NonNull Habit oldItem, @NonNull Habit newItem
}
}

protected static class HabitHolder extends RecyclerView.ViewHolder {
private final LlistHabitItemBinding mItemBinding;
protected static class ViewHolder extends RecyclerView.ViewHolder {
private final ListHabitItemBinding mItemBinding;

HabitHolder(@NonNull LlistHabitItemBinding itemBinding) {
ViewHolder(@NonNull ListHabitItemBinding itemBinding) {
super(itemBinding.getRoot());
this.mItemBinding = itemBinding;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import com.github.cmput301f21t44.hellohabits.R;
import com.github.cmput301f21t44.hellohabits.databinding.FragmentTodaysHabitsBinding;
import com.github.cmput301f21t44.hellohabits.model.Habit;
import com.github.cmput301f21t44.hellohabits.view.OnItemClickListener;
import com.github.cmput301f21t44.hellohabits.viewmodel.HabitViewModel;

import java.time.Instant;
Expand All @@ -24,7 +23,7 @@
import java.util.ArrayList;
import java.util.List;

public class TodaysHabitsFragment extends Fragment implements OnItemClickListener<Habit> {
public class TodaysHabitsFragment extends Fragment {
private FragmentTodaysHabitsBinding binding;
private HabitViewModel mHabitViewModel;
private HabitAdapter adapter;
Expand All @@ -44,7 +43,10 @@ public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
mNavController = NavHostFragment.findNavController(this);
// attach the provider to activity instead of fragment so the fragments can share data
mHabitViewModel = new ViewModelProvider(requireActivity()).get(HabitViewModel.class);
adapter = new HabitAdapter(new HabitAdapter.HabitDiff(), this);
adapter = HabitAdapter.newInstance((habit) -> {
mHabitViewModel.select(habit);
mNavController.navigate(R.id.action_todaysHabitsFragment_to_viewHabitFragment);
});
binding.habitRecyclerView.setAdapter(adapter);
binding.habitRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

Expand Down Expand Up @@ -80,10 +82,4 @@ public void onDestroyView() {
super.onDestroyView();
binding = null;
}

@Override
public void onItemClick(Habit habit) {
mHabitViewModel.select(habit);
mNavController.navigate(R.id.action_todaysHabitsFragment_to_viewHabitFragment);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
.navigate(R.id.action_viewHabitFragment_to_createEditHabitEventFragment);
});

binding.buttonViewHabitEvents.setOnClickListener(v -> mNavController
.navigate(R.id.action_viewHabitFragment_to_viewHabitEventListFragment));


binding.buttonDeleteHabit.setOnClickListener(v ->
new AlertDialog.Builder(requireContext())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
import com.github.cmput301f21t44.hellohabits.R;
import com.github.cmput301f21t44.hellohabits.databinding.FragmentCreateEditHabitEventBinding;
import com.github.cmput301f21t44.hellohabits.model.HabitEvent;
import com.github.cmput301f21t44.hellohabits.view.habit.CreateEditHabitFragment;
import com.github.cmput301f21t44.hellohabits.viewmodel.HabitEventViewModel;
import com.github.cmput301f21t44.hellohabits.viewmodel.HabitViewModel;

import java.util.Objects;

public class CreateEditHabitEventFragment extends Fragment {
private static final int MAX_COMMENT_LEN = 20;
private FragmentCreateEditHabitEventBinding binding;
private HabitViewModel mHabitViewModel;
private HabitEventViewModel mHabitEventViewModel;
Expand All @@ -36,26 +38,37 @@ public View onCreateView(@NonNull LayoutInflater inflater,

}

private void submitHabitEvent() {
String comment = binding.editTextComment.getText().toString();
if (comment.length() > MAX_COMMENT_LEN) {
binding.editTextComment.setError(CreateEditHabitFragment.ERROR_MESSAGE);
binding.editTextComment.requestFocus();
return;
}
if (isEdit) {
HabitEvent updatedHabitEvent = mHabitEventViewModel.update(mHabitEvent.getId(),
mHabitEvent.getHabitId(), mHabitEvent.getDate(), comment, null, null);
mHabitEventViewModel.select(updatedHabitEvent);
} else {
String habitId = Objects.requireNonNull(mHabitViewModel.getSelected().getValue())
.getId();
mHabitEventViewModel.insert(habitId, comment);
}
mNavController
.navigate(isEdit ?
R.id.action_createEditHabitEventFragment_to_viewHabitEventListFragment :
R.id.action_createEditHabitEventFragment_to_viewHabitFragment);

}

public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mHabitViewModel = new ViewModelProvider(requireActivity()).get(HabitViewModel.class);
mHabitEventViewModel = new ViewModelProvider(requireActivity())
.get(HabitEventViewModel.class);
mNavController = NavHostFragment.findNavController(this);

binding.buttonAddHabitEvent.setOnClickListener(v -> {
if (isEdit) {
HabitEvent updatedHabitEvent = mHabitEventViewModel.update(mHabitEvent.getId(),
mHabitEvent.getDate(), binding.editTextComment.getText().toString(), null,
null);
mHabitEventViewModel.select(updatedHabitEvent);
} else {
mHabitEventViewModel.insert(
Objects.requireNonNull(mHabitViewModel.getSelected().getValue()).getId(),
binding.editTextComment.getText().toString());
}
mNavController.navigate(R.id.action_createEditHabitEventFragment_to_viewHabitFragment);
});
binding.buttonAddHabitEvent.setOnClickListener(v -> submitHabitEvent());

binding.buttonBack.setOnClickListener(v ->
mNavController
Expand Down
Loading

0 comments on commit 407f8b8

Please sign in to comment.