Update and Delete Shares locally now possible
lbs-gitea/maptology/master This commit looks good

This commit is contained in:
Arman Arzani
2017-12-28 14:59:32 +01:00
parent 5ecca51d8d
commit 0b6ce421ca
24 changed files with 265 additions and 31 deletions
@@ -1,20 +1,33 @@
package com.maptology.android.app.share;
import android.app.AlertDialog;
import android.app.Dialog;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.*;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.maptology.android.app.R;
import com.maptology.android.app.fragment.AbstractFragment;
import com.maptology.android.app.fragment.DatePickerFragment;
import com.maptology.android.app.fragment.TimePickerFragment;
import com.maptology.android.app.model.Share;
import com.maptology.android.app.repository.IValueListener;
import com.maptology.android.app.repository.Value;
import javax.naming.Context;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import static com.maptology.android.app.share.ShareAdapter.id;
import static com.maptology.android.app.share.ShareAdapter.pos;
/**
* @author Arman
*/
@@ -24,6 +37,7 @@ public class GetSharesFragment extends AbstractFragment {
ShareAdapter shareAdapter;
List<Share> shares=new ArrayList<>();
IValueListener<Boolean> loadingSharesListener = new IValueListener<Boolean>() {
@@ -51,6 +65,8 @@ public class GetSharesFragment extends AbstractFragment {
return view;
}
@Override
public void onResume() {
shareAdapter=new ShareAdapter(getApp());
@@ -62,28 +78,150 @@ public class GetSharesFragment extends AbstractFragment {
recyclerView.setAdapter(shareAdapter);
//ShareStore shareStore=new ShareStore(getContext().getApplicationContext());
/*Cursor cursor = shareStore.getAllShares();
for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
Share s = new Share();
s.setCreationTime(cursor.g);
shares.add(cursor.getShort())
}*/
getRepository().loadingShares.addValueListener(loadingSharesListener,true);
super.onResume();
}
@Override
public boolean onContextItemSelected(MenuItem item) {
shareAdapter.getItemSelected(item);
//shareAdapter.getItemSelected(item);
CharSequence i = item.getTitle();
if (i.equals(ShareAdapter.RESHARE)) {
//regenerate the link again...
}else if(i.equals(ShareAdapter.EDIT)){
updateShareInDatabase();
}else{
deleteShareFromDatabase();
}
return super.onContextItemSelected(item);
}
public void updateShareInDatabase(){
View view=(LayoutInflater.from(getContext())).inflate(R.layout.user_input,null);
AlertDialog.Builder builder=new AlertDialog.Builder(getContext());
builder.setView(view);
TextView messageLable=(TextView)view.findViewById(R.id.messageLabel);
TextView destinationLabel=(TextView)view.findViewById(R.id.destinationLabel);
TextView dateLabel=(TextView)view.findViewById(R.id.dateLabel);
final EditText messageText=(EditText) view.findViewById(R.id.messageUpdateText);
final EditText destinationText=(EditText) view.findViewById(R.id.destinationUpdateText);
final TextView date=(TextView)view.findViewById(R.id.endDate);
TextView time=(TextView)view.findViewById(R.id.endTime);
Button ok=(Button)view.findViewById(R.id.okUpdateformButton);
Button cancel=(Button)view.findViewById(R.id.canecelUpdateFormButton);
builder.setTitle("Edit Share ");
builder.setIcon(R.drawable.ic_border_color_black_24dp);
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
getShares();
messageText.setText(shares.get(pos).getShareMessage());
destinationText.setText(shares.get(pos).getDestination());
// date.setText(Long.toString(shares.get(pos).getExpirationTime()));
}
}.execute();
final Dialog dialog=builder.create();
dialog.show();
date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getNowDate();
getDateTime(view);
}
});
time.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getNowDate();
getTime(view);
}
});
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getApp().getShareStore().updateShares(Integer.toString(id),
messageText.getText().toString(),
destinationText.getText().toString(),
date.getText().toString());
System.out.println("Share Edited");
dialog.dismiss();
}
});
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
}
});
}
DatePickerFragment datePickerFragment;
TimePickerFragment timePickerFragment;
private Calendar calendar;
int year,month,day,hour,minute;
private String DATE_TAG="Date";
private String TIME_TAG="Time";
private void getTime(View view) {
if(datePickerFragment!=null){
year=datePickerFragment.getYear();
month=datePickerFragment.getMonth();
day=datePickerFragment.getDay();
}
timePickerFragment=new TimePickerFragment();
timePickerFragment.setView(view);
timePickerFragment.setDate(year, month, day, hour, minute);
timePickerFragment.show(getFragmentManager(),TIME_TAG);
}
private void getDateTime(View view){
datePickerFragment=new DatePickerFragment();
datePickerFragment.setView(view);
datePickerFragment.setDate(year,month,day);
datePickerFragment.show(getFragmentManager(),DATE_TAG);
}
private void getNowDate(){
calendar = Calendar.getInstance();
year=calendar.get(Calendar.YEAR);
month=calendar.get(Calendar.MONTH);
day=calendar.get(Calendar.DAY_OF_MONTH);
hour=calendar.get(Calendar.HOUR_OF_DAY);
minute=calendar.get(Calendar.MINUTE);
}
public void deleteShareFromDatabase(){
getApp().getShareStore().removeShares(Integer.toString(id));
}
public void getShares(){
shares=getApp().getShareStore().getAllSharesAsList();
}
@Override
public void onPause() {
getRepository().loadingShares.removeValueListener(loadingSharesListener);
@@ -1,13 +1,19 @@
package com.maptology.android.app.share;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.*;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.maptology.android.app.R;
import com.maptology.android.app.fragment.AbstractFragment;
import com.maptology.android.app.model.Share;
import com.maptology.android.app.util.MaptologyApp;
@@ -17,13 +23,20 @@ import java.util.List;
/**
* @author Arman
*/
public class ShareAdapter extends RecyclerView.Adapter<ShareAdapter.ViewHolder> {
public class ShareAdapter extends RecyclerView.Adapter<ShareAdapter.ViewHolder>{
private static final String TAG = "ShareAdapter";
private List<Share> shares=new ArrayList<>();
public static final String RESHARE="Reshare";
public static final String EDIT="Edit";
public static final String DELETE="Delete";
public static int pos;
public static int id;
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener,View.OnLongClickListener{
private TextView creationTimeView;
@@ -34,6 +47,9 @@ public class ShareAdapter extends RecyclerView.Adapter<ShareAdapter.ViewHolder>
ILongClickListener longClickListener;
public ViewHolder(View itemView) {
super(itemView);
@@ -74,19 +90,19 @@ public class ShareAdapter extends RecyclerView.Adapter<ShareAdapter.ViewHolder>
@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
menu.setHeaderTitle("Select an Action");
menu.add(0,0,0,"Share Again");
menu.add(0,1,0,"Edit");
menu.add(0,2,0,"Delete");
menu.setHeaderIcon(R.drawable.ic_accessibility_black_24dp);
menu.add(RESHARE).setIcon(R.drawable.ic_compare_arrows_black_24dp).setTitle(RESHARE);
menu.add(EDIT).setIcon(R.drawable.ic_border_color_black_24dp).setTitle(EDIT);
menu.add(DELETE).setIcon(R.drawable.ic_delete_black_24dp).setTitle(DELETE);
}
@Override
public boolean onLongClick(View view) {
this.longClickListener.onItemClicklistener(getLayoutPosition());
return false;
}
}
}
private Cursor cursor;
@@ -131,14 +147,20 @@ public class ShareAdapter extends RecyclerView.Adapter<ShareAdapter.ViewHolder>
viewHolder.setLongClickListener(new ILongClickListener() {
@Override
public void onItemClicklistener(int position) {
pos=position;
destination=shares.get(position).getDestination();
Toast.makeText(app.getApplicationContext(),destination,Toast.LENGTH_LONG).show();
id=app.getShareStore().getShareId( shares.get(position).getCreationTime(),
shares.get(position).getExpirationTime());
Toast.makeText(app.getApplicationContext(),destination+ id,Toast.LENGTH_LONG).show();
}
});
}
@Override
public int getItemCount() {
if(cursor==null) return 0;
@@ -147,7 +169,6 @@ public class ShareAdapter extends RecyclerView.Adapter<ShareAdapter.ViewHolder>
}
public void getItemSelected(MenuItem item){
Toast.makeText(app.getApplicationContext(),destination+" :"+item.getTitle(),Toast.LENGTH_LONG);
}
@@ -96,7 +96,21 @@ public class ShareStore {
}
public int getShareId(long creationTime,long expirationTime){
int id=-1;
if (database == null) return 0;
Cursor cursor=database.rawQuery("select _id from " + ShareContract.Share.TABLE
+ " WHERE " + ShareContract.Share.CREATION_TIME+ " = "+creationTime+
" AND " + ShareContract.Share.EXPIRATION_TIME+ " = "+expirationTime,null);
if(cursor!=null) {
cursor.moveToFirst();
id = Integer.parseInt(cursor.getString(cursor.getColumnIndex("_id")));
System.out.println("id "+ id);
}cursor.close();
return id;
}
//check if share is there
public boolean containsShare(long id) {
@@ -133,19 +147,16 @@ public class ShareStore {
c.close();
return result;
}
//remove Shares
public void removeShares(long id) {
public void removeShares(String id) {
if (database == null) return;
// deletion will cascade (!) due to db schema
database.execSQL("DELETE FROM " + ShareContract.Share.TABLE
+ " WHERE " + ShareContract.Share.ID + " = " + id);
}
public void expireNotices(long currentTime) {
if (database == null) return;
database.execSQL("DELETE FROM " + ShareContract.Share.TABLE
+ " WHERE " + ShareContract.Share.EXPIRATION_TIME + " < " + currentTime);
}
public void removeSetting(@NonNull String key) {
if (database == null) return;
@@ -165,10 +176,13 @@ public class ShareStore {
}
public void updateShares(String id, String msg, String des, String expTime) {
if (database == null) return ;
database.execSQL("update " + ShareContract.Share.TABLE +
" SET " + ShareContract.Share.SHARE_MESSAGE + " = "+ msg +
" AND " + ShareContract.Share.SHARE_DESTINATION + " = "+ des +
" AND " + ShareContract.Share.EXPIRATION_TIME + " = "+ expTime +
" WHERE " + ShareContract.Share.ID + " = "+ id ,null);
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 163 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 187 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 161 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 155 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 120 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 153 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 123 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 111 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 163 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 204 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 179 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 148 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 214 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 260 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 229 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 191 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 267 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 300 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 289 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 237 B

@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="@dimen/globalLayoutMargin">
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Message"
android:id="@+id/messageLabel"/>
<EditText android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/messageUpdateText"/>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Destination"
android:id="@+id/destinationLabel"/>
<EditText android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/destinationUpdateText"/>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Expiration Time"
android:id="@+id/dateLabel"/>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/globalLayoutMargin">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/textSize"
android:text="00-00-00"
android:id="@+id/endDate"
android:inputType="datetime" android:layout_weight="0.5"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/textSize"
android:text="00:00"
android:id="@+id/endTime"
android:inputType="datetime" android:layout_weight="0.5"/>
</LinearLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/globalLayoutMargin">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:id="@+id/canecelUpdateFormButton"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ok"
android:id="@+id/okUpdateformButton"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>