Skip to content

Commit

Permalink
first, very basic sketch for the diary view, but at least the activit…
Browse files Browse the repository at this point in the history
…y selection is stored, so let me close #19
  • Loading branch information
ramack committed Nov 4, 2017
1 parent a9971e8 commit c5d6a31
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 13 deletions.
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
android:label="@string/activity_title_edit_new" />
<activity android:name=".ui.ManageActivity"
android:label="@string/activity_title_manage_activities" />
<activity android:name=".ui.HistoryActivity"
android:label="@string/activity_title_history" />
<provider
android:name=".db.ActivityDiaryContentProvider"
android:authorities="de.rampro.activitydiary"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,27 +277,34 @@ public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String
@Override
public int update(@NonNull Uri uri, @NonNull ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
String table;
boolean isID = false;
switch(sUriMatcher.match(uri)) {
case activities_ID:
isID = true;
table = LocalDBHelper.ACTIVITY_DB_TABLE;
break;
case conditions_ID:
isID = true;
table = LocalDBHelper.CONDITION_DB_TABLE;
break;
case diary_ID:
isID = true;
case diary:
table = LocalDBHelper.DIARY_DB_TABLE;
break;
default:
throw new IllegalArgumentException(
"Unsupported URI for insertion: " + uri);
"Unsupported URI for update: " + uri);
}
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
if(selection != null) {
selection = selection + " AND ";
}else{
selection = "";
if(isID) {
if (selection != null) {
selection = selection + " AND ";
} else {
selection = "";
}
selection = selection + "_id=" + uri.getLastPathSegment();
}
selection = selection + "_id=" + uri.getLastPathSegment();

int upds = db.update(table,
values,
Expand All @@ -308,7 +315,7 @@ public int update(@NonNull Uri uri, @NonNull ContentValues values, @Nullable Str
getContentResolver().
notifyChange(uri, null);

}else {
}else if(isID) {
throw new SQLException(
"Problem while updating uri: " + uri);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void onCreate(SQLiteDatabase db) {
ActivityDiaryContract.Diary._DELETED + " INTEGER DEFAULT 0," +
ActivityDiaryContract.Diary.ACT_ID + " INTEGER NOT NULL, " +
ActivityDiaryContract.Diary.START + " INTEGER NOT NULL, " +
ActivityDiaryContract.Diary.END + " INTEGER, " +
ActivityDiaryContract.Diary.END + " INTEGER DEFAULT NULL, " +
" FOREIGN KEY(act_id) REFERENCES activity(_id) " +
");");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
package de.rampro.activitydiary.model;

import android.content.AsyncQueryHandler;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Date;
Expand All @@ -34,6 +37,8 @@
*/
public class ActivityHelper extends AsyncQueryHandler{
private static final int QUERY_ALL_ACTIVITIES = 0;
private static final int UPDATE_CLOSE_ACTIVITY = 1;
private static final int INSERT_NEW_ACTIVITY = 2;
private static final String[] PROJECTION = new String[] {
ActivityDiaryContract.DiaryActivity._ID,
ActivityDiaryContract.DiaryActivity.NAME,
Expand Down Expand Up @@ -99,17 +104,41 @@ public DiaryActivity getCurrentActivity(){
}

public void setCurrentActivity(DiaryActivity activity){
/* update the current diary entry to "finish" it */
/* update the current diary entry to "finish" it
* in theory there should be only one entry with end = NULL in the diray table
* but who knows? -> Let's update all. */
ContentValues values = new ContentValues();
values.put(ActivityDiaryContract.Diary.END, System.currentTimeMillis());

/* create a new diary entry */
startUpdate(UPDATE_CLOSE_ACTIVITY, null, ActivityDiaryContract.Diary.CONTENT_URI,
values, ActivityDiaryContract.Diary.END + " is NULL", null);

/* TODO: create listener class and notify the listeners here... */
currentActivity = activity;
/* TODO insert into Diary here... */
/* TODO: create listener class and notify the listeners here... */
}

@Override
protected void onUpdateComplete(int token, Object cookie, int result) {
if(token == UPDATE_CLOSE_ACTIVITY) {
/* create a new diary entry */
ContentValues values = new ContentValues();
;
values.put(ActivityDiaryContract.Diary.ACT_ID, currentActivity.getId());
values.put(ActivityDiaryContract.Diary.START, System.currentTimeMillis());

startInsert(INSERT_NEW_ACTIVITY, null, ActivityDiaryContract.Diary.CONTENT_URI,
values);
}
}

@Override
protected void onInsertComplete(int token, Object cookie, Uri uri) {
if(token == INSERT_NEW_ACTIVITY){
Toast.makeText(ActivityDiaryApplication.getAppContext(), "inserted diary entry " + uri.toString(), Toast.LENGTH_LONG);
}
}

public void insertActivity(DiaryActivity act){
public void insertActivity(DiaryActivity act){
activities.add(act);
/* TODO: insert into ContentProvider and update id afterwards */
mDataChangeListeners.forEach(listener -> listener.onActivityDataChanged()) ;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ public boolean onNavigationItemSelected(MenuItem menuItem) {
startActivity(intentmanage);
break;
case R.id.nav_diary:
Intent intentdiary = new Intent(BaseActivity.this, HistoryActivity.class);
startActivity(intentdiary);
break;
case R.id.nav_settings:
case R.id.nav_about:
/* TODO: implement all those ... */
Expand Down
161 changes: 161 additions & 0 deletions app/src/main/java/de/rampro/activitydiary/ui/HistoryActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/*
* ActivityDiary
*
* Copyright (C) 2017 Raphael Mack http://www.raphael-mack.de
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package de.rampro.activitydiary.ui;

import android.app.LoaderManager;
import android.content.Context;
import android.content.Intent;
import android.content.Loader;
import android.content.CursorLoader;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ResourceCursorAdapter;
import android.widget.TextView;


import java.util.Date;

import de.rampro.activitydiary.R;
import de.rampro.activitydiary.db.ActivityDiaryContract;


/*
* Show this history in the Diary.
*
* */
public class HistoryActivity extends BaseActivity implements LoaderManager.LoaderCallbacks<Cursor> {
private static final String TAG = ManageActivity.class.getName();
private static final String[] PROJECTION = new String[] {
ActivityDiaryContract.Diary._ID,
ActivityDiaryContract.Diary.ACT_ID,
ActivityDiaryContract.Diary.START,
ActivityDiaryContract.Diary.END
};
private static final String SELECTION = ActivityDiaryContract.Diary._DELETED + "=0";

private ListView mList;
private class DiaryActivityAdapter extends ResourceCursorAdapter {

public DiaryActivityAdapter() {
super(HistoryActivity.this, R.layout.activity_row, null, 0);
}

@Override
public void bindView(View view, Context context, Cursor cursor){
long start = cursor.getLong(cursor.getColumnIndex(ActivityDiaryContract.Diary.START));
long end = cursor.getLong(cursor.getColumnIndex(ActivityDiaryContract.Diary.END));

TextView actName = (TextView) view.findViewById(R.id.activity_name);
actName.setText(new Date(start).toString() + " - " + new Date(end).toString());

ImageView imageView = (ImageView) view.findViewById(R.id.activity_image);
}
}

private DiaryActivityAdapter mActivitiyListAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

/* TODO: change to different XML resource */
View contentView = inflater.inflate(R.layout.activity_manage_content, null, false);

setContent(contentView);
mList = (ListView)findViewById(R.id.manage_activity_list);
mActivitiyListAdapter = new DiaryActivityAdapter();
mList.setAdapter(mActivitiyListAdapter);

mList.setOnItemClickListener(mOnClickListener);

// Prepare the loader. Either re-connect with an existing one,
// or start a new one.
getLoaderManager().initLoader(0, null, this);
mDrawerToggle.setDrawerIndicatorEnabled(false);
mNavigationView.getMenu().findItem(R.id.nav_diary).setChecked(true);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
/* todo change menu */
inflater.inflate(R.menu.manage_menu, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle your other action bar items...
switch(item.getItemId()) {
case R.id.action_add_activity:
Intent intentaddact = new Intent(HistoryActivity.this, EditActivity.class);
startActivity(intentaddact);
break;
case android.R.id.home:
finish();
break;
}
return super.onOptionsItemSelected(item);
}

// Called when a new Loader needs to be created
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// Now create and return a CursorLoader that will take care of
// creating a Cursor for the data being displayed.
return new CursorLoader(this, ActivityDiaryContract.Diary.CONTENT_URI,
PROJECTION, SELECTION, null, null);
}

// Called when a previously created loader has finished loading
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
// Swap the new cursor in. (The framework will take care of closing the
// old cursor once we return.)
mActivitiyListAdapter.swapCursor(data);
}

// Called when a previously created loader is reset, making the data unavailable
public void onLoaderReset(Loader<Cursor> loader) {
// This is called when the last Cursor provided to onLoadFinished()
// above is about to be closed. We need to make sure we are no
// longer using it.
mActivitiyListAdapter.swapCursor(null);
}

private AdapterView.OnItemClickListener mOnClickListener = new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
Cursor c = (Cursor)parent.getItemAtPosition(position);
/* TODO */
}
};

/* TODO: implement swipe for parent / child navigation */
/* TODO: add number of child activities in view */

}
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 @@ -23,6 +23,7 @@
<string name="activity_title_edit">Edit </string>
<string name="activity_title_edit_new">New Activity</string>
<string name="activity_title_manage_activities">Manage Activities</string>
<string name="activity_title_history">Diary</string>
<string name="activity_title_no_selected_act">&lt;No Activity&gt;</string>

<!-- actions -->
Expand Down

0 comments on commit c5d6a31

Please sign in to comment.