About page update to show SDK version

TaskDataAccess now implements AutoCloseable
Correction of TaskDataAccess exception due to closing when SnackBar was present and tabs were fast switched
Detection of initial scroll value to toggle arrows visibility (but there is still an issue with right arrow)
This commit is contained in:
2016-01-26 17:50:19 -05:00
parent c810f3775a
commit 133df4c414
6 changed files with 111 additions and 81 deletions

View File

@@ -1,5 +1,6 @@
package com.wismna.geoffroy.donext.activities; package com.wismna.geoffroy.donext.activities;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.widget.TextView; import android.widget.TextView;
@@ -14,7 +15,11 @@ public class AboutActivity extends AppCompatActivity {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about); setContentView(R.layout.activity_about);
TextView version = (TextView) findViewById(R.id.version); TextView versionDonext = (TextView) findViewById(R.id.version_donext);
version.setText(getResources().getString(R.string.about_version, BuildConfig.VERSION_NAME)); versionDonext.setText(getResources().getString(R.string.about_version_donext, BuildConfig.VERSION_NAME));
TextView versionAndroid = (TextView) findViewById(R.id.version_android);
versionAndroid.setText(getResources().getString(R.string.about_version_android, Build.VERSION.SDK_INT));
} }
} }

View File

@@ -3,6 +3,7 @@ package com.wismna.geoffroy.donext.activities;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.graphics.Point; import android.graphics.Point;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.design.widget.FloatingActionButton; import android.support.design.widget.FloatingActionButton;
@@ -44,6 +45,7 @@ public class MainActivity extends AppCompatActivity implements TasksFragment.Tas
* The {@link ViewPager} that will host the section contents. * The {@link ViewPager} that will host the section contents.
*/ */
private ViewPager mViewPager; private ViewPager mViewPager;
private TabLayout tabLayout;
private List<TaskList> taskLists; private List<TaskList> taskLists;
@Override @Override
@@ -80,26 +82,20 @@ public class MainActivity extends AppCompatActivity implements TasksFragment.Tas
PreferenceManager.getDefaultSharedPreferences(MainActivity.this); PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
mViewPager.setCurrentItem(sharedPref.getInt("last_opened_tab", 0)); mViewPager.setCurrentItem(sharedPref.getInt("last_opened_tab", 0));
// TODO: hide arrows on start when not needed tabLayout = (TabLayout) findViewById(R.id.tabs);
final TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager); tabLayout.setupWithViewPager(mViewPager);
tabLayout.setOnScrollChangeListener(new View.OnScrollChangeListener() {
@Override
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
// Hide left arrow when scrolled to the left
View leftArrow = findViewById(R.id.left_arrow);
if (scrollX <= 1) leftArrow.setVisibility(View.GONE);
else leftArrow.setVisibility(View.VISIBLE);
// Hide right arrow when scrolled to the right toggleTabLayoutArrows(tabLayout.getScrollX());
View rightArrow = findViewById(R.id.right_arrow); // Handles scroll detection (only available for SDK version >=23)
Point size = new Point(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
getWindowManager().getDefaultDisplay().getSize(size); //tabLayout.setScrollIndicators(TabLayout.SCROLL_INDICATOR_LEFT | TabLayout.SCROLL_INDICATOR_RIGHT);
if (scrollX == tabLayout.getChildAt(0).getMeasuredWidth() - size.x) tabLayout.setOnScrollChangeListener(new View.OnScrollChangeListener() {
rightArrow.setVisibility(View.GONE); @Override
else rightArrow.setVisibility(View.VISIBLE); public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
} toggleTabLayoutArrows(scrollX);
}); }
});
}
// Hide or show new task floating button // Hide or show new task floating button
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
@@ -218,6 +214,23 @@ public class MainActivity extends AppCompatActivity implements TasksFragment.Tas
return (TaskRecyclerViewAdapter) recyclerView.getAdapter(); return (TaskRecyclerViewAdapter) recyclerView.getAdapter();
} }
/** Toggles scrolling arrows visibility */
private void toggleTabLayoutArrows(int scrollX){
// Hide left arrow when scrolled to the left
View leftArrow = findViewById(R.id.left_arrow);
if (scrollX <= 1) leftArrow.setVisibility(View.GONE);
else leftArrow.setVisibility(View.VISIBLE);
// TODO: hide right arrow when no need to scroll
// Hide right arrow when scrolled to the right
View rightArrow = findViewById(R.id.right_arrow);
Point size = new Point();
getWindowManager().getDefaultDisplay().getSize(size);
if (scrollX == tabLayout.getChildAt(0).getMeasuredWidth() - size.x)
rightArrow.setVisibility(View.GONE);
else rightArrow.setVisibility(View.VISIBLE);
}
/** /**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages. * one of the sections/tabs/pages.

View File

@@ -16,7 +16,12 @@ import java.util.List;
* Created by geoffroy on 15-11-27. * Created by geoffroy on 15-11-27.
* Data access class that handles Tasks * Data access class that handles Tasks
*/ */
public class TaskDataAccess { public class TaskDataAccess implements AutoCloseable {
public enum MODE {
READ,
WRITE
}
private SQLiteDatabase database; private SQLiteDatabase database;
private DatabaseHelper dbHelper; private DatabaseHelper dbHelper;
private String[] taskColumns = { private String[] taskColumns = {
@@ -27,15 +32,21 @@ public class TaskDataAccess {
private List<String> priorities = new ArrayList<>(); private List<String> priorities = new ArrayList<>();
public TaskDataAccess(Context context) { public TaskDataAccess(Context context) {
this(context, MODE.READ);
}
public TaskDataAccess(Context context, MODE writeMode) {
dbHelper = new DatabaseHelper(context); dbHelper = new DatabaseHelper(context);
priorities.add(context.getString(R.string.new_task_priority_low)); priorities.add(context.getString(R.string.new_task_priority_low));
priorities.add(context.getString(R.string.new_task_priority_normal)); priorities.add(context.getString(R.string.new_task_priority_normal));
priorities.add(context.getString(R.string.new_task_priority_high)); priorities.add(context.getString(R.string.new_task_priority_high));
open(writeMode);
} }
public void open() throws SQLException { public void open(MODE writeMode) throws SQLException {
database = dbHelper.getWritableDatabase(); if (writeMode == MODE.WRITE) database = dbHelper.getWritableDatabase();
else database = dbHelper.getReadableDatabase();
} }
public void close() { public void close() {

View File

@@ -48,7 +48,7 @@ public class TasksFragment extends Fragment implements
private static final String TASK_LIST_ID = "task_list_id"; private static final String TASK_LIST_ID = "task_list_id";
private long taskListId = -1; private long taskListId = -1;
private TaskDataAccess taskDataAccess; //private TaskDataAccess taskDataAccess;
private TaskRecyclerViewAdapter taskRecyclerViewAdapter; private TaskRecyclerViewAdapter taskRecyclerViewAdapter;
private View view; private View view;
private RecyclerView recyclerView; private RecyclerView recyclerView;
@@ -86,8 +86,6 @@ public class TasksFragment extends Fragment implements
view = inflater.inflate(R.layout.fragment_tasks, container, false); view = inflater.inflate(R.layout.fragment_tasks, container, false);
final Context context = view.getContext(); final Context context = view.getContext();
taskDataAccess = new TaskDataAccess(view.getContext());
taskDataAccess.open();
// Set the Recycler view // Set the Recycler view
recyclerView = (RecyclerView) view.findViewById(R.id.task_list_view); recyclerView = (RecyclerView) view.findViewById(R.id.task_list_view);
@@ -95,9 +93,11 @@ public class TasksFragment extends Fragment implements
// Set RecyclerView Adapter // Set RecyclerView Adapter
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getContext()); SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getContext());
taskRecyclerViewAdapter = new TaskRecyclerViewAdapter( try (TaskDataAccess taskDataAccess = new TaskDataAccess(view.getContext())) {
taskDataAccess.getAllTasks(taskListId), taskRecyclerViewAdapter = new TaskRecyclerViewAdapter(
Integer.valueOf(sharedPref.getString("pref_conf_task_layout", "1"))); taskDataAccess.getAllTasks(taskListId),
Integer.valueOf(sharedPref.getString("pref_conf_task_layout", "1")));
}
recyclerView.setAdapter(taskRecyclerViewAdapter); recyclerView.setAdapter(taskRecyclerViewAdapter);
// Set ItemTouch helper in RecyclerView to handle swipe move on elements // Set ItemTouch helper in RecyclerView to handle swipe move on elements
@@ -166,16 +166,9 @@ public class TasksFragment extends Fragment implements
} }
@Override @Override
public void onDestroy() { public void onPause() {
super.onDestroy(); super.onPause();
if (snackbar != null) snackbar.dismiss(); if (snackbar != null) snackbar.dismiss();
taskDataAccess.close();
}
@Override
public void onResume() {
super.onResume();
taskDataAccess.open();
} }
/** Performs an action on a task: done, next or delete */ /** Performs an action on a task: done, next or delete */
@@ -236,21 +229,21 @@ public class TasksFragment extends Fragment implements
// When clicked on undo, do not write to DB // When clicked on undo, do not write to DB
if (event == DISMISS_EVENT_ACTION) return; if (event == DISMISS_EVENT_ACTION) return;
// TODO: correct bug when fast switching between tabs
// Commit the changes to DB // Commit the changes to DB
switch (direction) try (TaskDataAccess taskDataAccess = new TaskDataAccess(view.getContext(), TaskDataAccess.MODE.WRITE)) {
{ switch (direction) {
// Mark item as Done // Mark item as Done
case ItemTouchHelper.LEFT: case ItemTouchHelper.LEFT:
taskDataAccess.setDone(itemId); taskDataAccess.setDone(itemId);
break; break;
// Increase task cycle count // Increase task cycle count
case ItemTouchHelper.RIGHT: case ItemTouchHelper.RIGHT:
taskDataAccess.increaseCycle(task.getCycle(), itemId); taskDataAccess.increaseCycle(task.getCycle(), itemId);
break; break;
case -1: case -1:
// Commit the changes to DB // Commit the changes to DB
taskDataAccess.deleteTask(itemId); taskDataAccess.deleteTask(itemId);
}
} }
} }
}).show(); }).show();
@@ -308,34 +301,36 @@ public class TasksFragment extends Fragment implements
TaskList taskList = (TaskList) listSpinner.getSelectedItem(); TaskList taskList = (TaskList) listSpinner.getSelectedItem();
// Add the task to the database // Add the task to the database
Task newTask = taskDataAccess.createOrUpdateTask(id, try (TaskDataAccess taskDataAccess = new TaskDataAccess(view.getContext(), TaskDataAccess.MODE.WRITE)) {
nameText.getText().toString(), Task newTask = taskDataAccess.createOrUpdateTask(id,
descText.getText().toString(), nameText.getText().toString(),
priorityRadio.getText().toString(), descText.getText().toString(),
taskList.getId()); priorityRadio.getText().toString(),
taskList.getId());
Bundle args = dialog.getArguments(); Bundle args = dialog.getArguments();
// Should never happen because we will have to be on this tab to open the dialog // Should never happen because we will have to be on this tab to open the dialog
if (taskRecyclerViewAdapter == null) return; if (taskRecyclerViewAdapter == null) return;
// Add the task // Add the task
if (task == null) { if (task == null) {
taskRecyclerViewAdapter.add(newTask, 0); taskRecyclerViewAdapter.add(newTask, 0);
recyclerView.scrollToPosition(0); recyclerView.scrollToPosition(0);
} }
// Update the task // Update the task
else { else {
int position = args.getInt("position"); int position = args.getInt("position");
// Check if task list was changed // Check if task list was changed
if (task.getTaskListId() != taskList.getId()) if (task.getTaskListId() != taskList.getId())
{ {
// Remove item from current tab // Remove item from current tab
taskRecyclerViewAdapter.remove(position); taskRecyclerViewAdapter.remove(position);
//UpdateCycleCount(); //UpdateCycleCount();
// Add it to the corresponding tab provided it is already instanciated // Add it to the corresponding tab provided it is already instantiated
mAdapter.onTaskListChanged(newTask, listSpinner.getSelectedItemPosition()); mAdapter.onTaskListChanged(newTask, listSpinner.getSelectedItemPosition());
} else taskRecyclerViewAdapter.update(newTask, position); } else taskRecyclerViewAdapter.update(newTask, position);
}
} }
} }

View File

@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
@@ -7,9 +7,14 @@
android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.wismna.geoffroy.donext.activities.AboutActivity"> tools:context="com.wismna.geoffroy.donext.activities.AboutActivity">
<TextView <TextView
android:id="@+id/version" android:id="@+id/version_donext"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" /> android:layout_height="wrap_content" />
</RelativeLayout> <TextView
android:id="@+id/version_android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

View File

@@ -90,5 +90,6 @@
<string name="title_activity_task_list">TaskListActivity</string> <string name="title_activity_task_list">TaskListActivity</string>
<!-- Strings related to About --> <!-- Strings related to About -->
<string name="about_version">Version %s</string> <string name="about_version_donext">DoNext version %s</string>
<string name="about_version_android">Android version %s</string>
</resources> </resources>