Skip to content

Commit 9f96ea1

Browse files
committed
Manage list items
1 parent 573b90b commit 9f96ea1

File tree

18 files changed

+390
-95
lines changed

18 files changed

+390
-95
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ It works as follows:
1818
- https://www.android-examples.com/android-swipe-down-to-refresh-recyclerview/
1919
- https://developer.android.com/studio/write/java8-support.html
2020
- https://stackoverflow.com/questions/31367599/how-to-update-recyclerview-adapter-data
21+
- https://zatackcoder.com/android-recyclerview-swipe-to-multiple-options/

app/src/main/java/org/docspell/docspellshare/Fun.java

Lines changed: 0 additions & 6 deletions
This file was deleted.

app/src/main/java/org/docspell/docspellshare/Lazy.java

Lines changed: 0 additions & 6 deletions
This file was deleted.

app/src/main/java/org/docspell/docspellshare/activity/AddUrlActivity.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,28 @@
88
import androidx.appcompat.app.AppCompatActivity;
99
import com.google.android.material.snackbar.Snackbar;
1010
import org.docspell.docspellshare.R;
11-
import org.docspell.docspellshare.Strings;
11+
import org.docspell.docspellshare.util.Strings;
1212
import org.docspell.docspellshare.data.UrlItem;
1313

1414
public class AddUrlActivity extends AppCompatActivity {
1515
private static final int GET_QR_CODE = 1;
1616

17+
public static final String URL_ITEM_EXTRA = "extraUrlItem";
18+
1719
@Override
1820
protected void onCreate(Bundle savedInstanceState) {
1921
super.onCreate(savedInstanceState);
2022
setContentView(R.layout.activity_add_url);
23+
24+
Intent intent = getIntent();
25+
UrlItem item = (UrlItem) intent.getSerializableExtra(URL_ITEM_EXTRA);
26+
if (item != null) {
27+
EditText nameField = findViewById(R.id.urlNameField);
28+
nameField.setText(item.getName());
29+
nameField.setEnabled(false);
30+
EditText urlField = findViewById(R.id.urlValueField);
31+
urlField.setText(item.getUrl());
32+
}
2133
}
2234

2335
public void addUrl(View view) {
Lines changed: 133 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,132 +1,163 @@
11
package org.docspell.docspellshare.activity;
22

3-
import android.content.Context;
3+
import android.app.Activity;
44
import android.content.Intent;
5-
import android.content.SharedPreferences;
5+
import android.graphics.Color;
66
import android.os.Bundle;
77
import android.util.Log;
88
import android.view.LayoutInflater;
99
import android.view.View;
1010
import android.view.ViewGroup;
11-
import android.widget.ImageButton;
11+
import android.widget.Switch;
1212
import android.widget.TextView;
13+
14+
import androidx.annotation.ColorInt;
1315
import androidx.annotation.NonNull;
1416
import androidx.annotation.Nullable;
1517
import androidx.appcompat.app.AppCompatActivity;
1618
import androidx.appcompat.widget.Toolbar;
19+
import androidx.cardview.widget.CardView;
1720
import androidx.recyclerview.widget.LinearLayoutManager;
1821
import androidx.recyclerview.widget.RecyclerView;
1922
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
23+
24+
import org.docspell.docspellshare.R;
25+
import org.docspell.docspellshare.data.Option;
26+
import org.docspell.docspellshare.data.UrlItem;
27+
import org.docspell.docspellshare.util.DataStore;
28+
2029
import java.util.ArrayList;
2130
import java.util.Collections;
2231
import java.util.HashMap;
2332
import java.util.List;
2433
import java.util.Map;
2534

26-
import org.docspell.docspellshare.R;
27-
import org.docspell.docspellshare.data.UrlItem;
28-
2935
public class MainActivity extends AppCompatActivity {
30-
private static final int URL_ITEM_RESULT = 1;
31-
private static final String SHARED_PREF_KEY = "org.docspell.docspellshare.ITEMS_KEY";
36+
private static final int ADD_RESULT = 1;
37+
private static final int EDIT_RESULT = 2;
3238

3339
private SwipeRefreshLayout swipeView;
3440
private RecyclerView urlListView;
41+
private DataStore dataStore;
3542

3643
@Override
3744
protected void onCreate(Bundle savedInstanceState) {
3845
super.onCreate(savedInstanceState);
46+
dataStore = new DataStore(this);
3947
setContentView(R.layout.activity_main);
4048
Toolbar toolbar = findViewById(R.id.toolbar);
4149
setSupportActionBar(toolbar);
4250

4351
urlListView = findViewById(R.id.urlListView);
4452
urlListView.setLayoutManager(new LinearLayoutManager(this));
45-
urlListView.setAdapter(new UrlItemAdapter(loadData()));
53+
urlListView.setAdapter(new UrlItemAdapter(dataStore.loadAll(), dataStore.getDefault(), this));
4654

4755
swipeView = findViewById(R.id.swipeCnt);
4856
swipeView.setOnRefreshListener(
4957
() -> {
5058
UrlItemAdapter ua = (UrlItemAdapter) urlListView.getAdapter();
5159
if (ua != null) {
52-
ua.replaceAll(loadData());
60+
ua.replaceAll(dataStore.loadAll(), dataStore.getDefault());
5361
}
5462
swipeView.setRefreshing(false);
5563
});
5664
}
5765

58-
private List<UrlItem> loadData() {
59-
SharedPreferences prefs = getSharedPreferences(SHARED_PREF_KEY, Context.MODE_PRIVATE);
60-
List<UrlItem> result = new ArrayList<>();
61-
for (String key : prefs.getAll().keySet()) {
62-
result.add(new UrlItem(key, prefs.getString(key, "")));
66+
public void addNewUrl(View view) {
67+
Intent intent = new Intent(this, AddUrlActivity.class);
68+
startActivityForResult(intent, ADD_RESULT);
69+
}
70+
71+
public void editUrl(UrlItem item) {
72+
if (item != null) {
73+
Intent intent = new Intent(this, AddUrlActivity.class);
74+
intent.putExtra(AddUrlActivity.URL_ITEM_EXTRA, item);
75+
startActivityForResult(intent, EDIT_RESULT);
76+
} else {
77+
Log.e("not-found", "No item found in holder");
6378
}
64-
Collections.sort(result);
65-
return result;
6679
}
6780

68-
public void addNewUrl(View view) {
69-
Intent intent = new Intent(this, AddUrlActivity.class);
70-
startActivityForResult(intent, URL_ITEM_RESULT);
81+
public void deleteUrl(UrlItemAdapter adapter, UrlItem item) {
82+
if (item != null) {
83+
dataStore.remove(item.getName());
84+
adapter.remove(item);
85+
}
86+
}
87+
88+
public void toggleDefault(UrlItemAdapter adapter, UrlItem item) {
89+
if (item != null) {
90+
adapter.toggleDefaultItem(item);
91+
if (dataStore.isDefault(item.getName())) {
92+
dataStore.removeDefault();
93+
} else {
94+
dataStore.setDefault(item.getName());
95+
}
96+
}
7197
}
7298

7399
@Override
74100
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
75101
super.onActivityResult(requestCode, resultCode, data);
76-
if (data != null && requestCode == URL_ITEM_RESULT && resultCode == RESULT_OK) {
102+
if (data != null
103+
&& (requestCode == ADD_RESULT || requestCode == EDIT_RESULT)
104+
&& resultCode == RESULT_OK) {
77105
UrlItem item = (UrlItem) data.getSerializableExtra(UrlItem.class.getName());
78106
Log.i("add url", "item is " + item);
79107
if (item != null) {
80-
SharedPreferences prefs = getSharedPreferences(SHARED_PREF_KEY, Context.MODE_PRIVATE);
81-
SharedPreferences.Editor edit = prefs.edit();
82-
edit.putString(item.getName(), item.getUrl());
83-
edit.apply();
84-
108+
dataStore.addOrReplace(item);
85109
UrlItemAdapter ua = (UrlItemAdapter) urlListView.getAdapter();
86110
if (ua != null) {
87111
ua.add(item);
112+
if (ua.items.size() == 1) {
113+
dataStore.setDefault(item.getName());
114+
ua.toggleDefaultItem(item);
115+
}
88116
}
89117
}
90118
}
91119
}
92120

93121
static class UrlItemAdapter extends RecyclerView.Adapter<UrlItemHolder> {
94122
private final Map<String, UrlItem> items;
123+
private Option<String> defaultItem = Option.empty();
124+
private final MainActivity activity;
95125

96-
public UrlItemAdapter(List<UrlItem> items) {
126+
public UrlItemAdapter(List<UrlItem> items, Option<String> defaultItem, MainActivity activity) {
127+
this.activity = activity;
97128
this.items = new HashMap<>();
129+
this.defaultItem = defaultItem;
98130
for (UrlItem item : items) {
99131
this.items.put(item.getName(), item);
100132
}
101133
}
102134

135+
private void toggleCards(View v) {
136+
View mainCard = v.findViewById(R.id.itemCard);
137+
View actionCard = v.findViewById(R.id.cardActions);
138+
boolean mainVisible = mainCard.getVisibility() == View.VISIBLE;
139+
if (mainVisible) {
140+
mainCard.setVisibility(View.GONE);
141+
actionCard.setVisibility(View.VISIBLE);
142+
} else {
143+
mainCard.setVisibility(View.VISIBLE);
144+
actionCard.setVisibility(View.GONE);
145+
}
146+
}
147+
103148
@NonNull
104149
@Override
105150
public UrlItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
106151
View v =
107152
LayoutInflater.from(parent.getContext()).inflate(R.layout.url_item_layout, parent, false);
108-
UrlItemHolder holder = new UrlItemHolder(v);
109-
ImageButton deleteBtn = v.findViewById(R.id.deleteItemBtn);
110-
deleteBtn.setOnClickListener(
111-
view -> {
112-
try {
113-
UrlItem item = holder.item;
114-
if (item != null) {
115-
items.remove(item.getName());
116-
notifyDataSetChanged();
117-
SharedPreferences prefs =
118-
parent.getContext().getSharedPreferences(SHARED_PREF_KEY, Context.MODE_PRIVATE);
119-
SharedPreferences.Editor edit = prefs.edit();
120-
edit.remove(item.getName());
121-
edit.apply();
122-
}
123-
} catch (Exception e) {
124-
Log.e("remove-item", "Cannot remove item from position: " + holder.position);
125-
}
126-
});
127-
v.setOnClickListener(view -> {
128-
129-
});
153+
UrlItemHolder holder = new UrlItemHolder(v, activity, this);
154+
155+
View backBtn = v.findViewById(R.id.itemCardCancelBtn);
156+
backBtn.setOnClickListener(view -> toggleCards(v));
157+
158+
View mainCard = v.findViewById(R.id.itemCard);
159+
mainCard.setOnClickListener(view -> toggleCards(v));
160+
130161
return holder;
131162
}
132163

@@ -137,7 +168,7 @@ public void onBindViewHolder(@NonNull UrlItemHolder holder, int position) {
137168
String name = names.get(position);
138169
UrlItem item = items.get(name);
139170
if (item != null) {
140-
holder.setContent(item, position);
171+
holder.setContent(item, defaultItem, items.size());
141172
}
142173
}
143174

@@ -146,8 +177,9 @@ public int getItemCount() {
146177
return items.size();
147178
}
148179

149-
public void replaceAll(List<UrlItem> newData) {
180+
public void replaceAll(List<UrlItem> newData, Option<String> defaultItem) {
150181
items.clear();
182+
this.defaultItem = defaultItem;
151183
for (UrlItem item : newData) {
152184
this.items.put(item.getName(), item);
153185
}
@@ -156,29 +188,72 @@ public void replaceAll(List<UrlItem> newData) {
156188

157189
public void add(UrlItem item) {
158190
items.put(item.getName(), item);
191+
if (items.size() == 2) {
192+
notifyItemRangeChanged(0, items.size());
193+
} else {
194+
notifyDataSetChanged();
195+
}
196+
}
197+
198+
public void remove(UrlItem item) {
199+
items.remove(item.getName());
159200
notifyDataSetChanged();
160201
}
202+
203+
public void toggleDefaultItem(UrlItem item) {
204+
Log.w("debug", "Current default is: " + defaultItem + "; want it to be: " + item.getName());
205+
Option<String> di = Option.of(item.getName());
206+
if (defaultItem.equals(di)) {
207+
this.defaultItem = Option.empty();
208+
} else {
209+
this.defaultItem = di;
210+
}
211+
Log.w("debug", "Now default is: " + defaultItem);
212+
notifyItemRangeChanged(0, items.size());
213+
}
161214
}
162215

163216
static class UrlItemHolder extends RecyclerView.ViewHolder {
164217
private final View view;
165-
private int position = -1;
166-
private UrlItem item;
218+
private final MainActivity activity;
219+
private final UrlItemAdapter adapter;
167220

168-
public UrlItemHolder(View v) {
221+
public UrlItemHolder(View v, MainActivity activity, UrlItemAdapter adapter) {
169222
super(v);
170223
this.view = v;
224+
this.activity = activity;
225+
this.adapter = adapter;
171226
}
172227

173-
public void setContent(UrlItem item, int position) {
174-
this.position = position;
175-
this.item = item;
228+
public void setContent(UrlItem item, Option<String> defaultItem, int size) {
229+
View actionCard = view.findViewById(R.id.cardActions);
230+
actionCard.setVisibility(View.GONE);
176231

177-
TextView nameField = view.findViewById(R.id.itemName);
232+
CardView mainCard = view.findViewById(R.id.itemCard);
233+
mainCard.setVisibility(View.VISIBLE);
234+
235+
TextView nameField = view.findViewById(R.id.itemName1);
236+
nameField.setText(item.getName());
237+
nameField = view.findViewById(R.id.itemName2);
178238
nameField.setText(item.getName());
179239

180240
TextView urlField = view.findViewById(R.id.itemUrl);
181241
urlField.setText(item.getUrl());
242+
243+
Switch defaultSwitch = view.findViewById(R.id.itemCardDefaultSwitch);
244+
boolean isDefault = defaultItem.equals(Option.of(item.getName()));
245+
defaultSwitch.setChecked(isDefault);
246+
int newColor =
247+
isDefault
248+
? mainCard.getResources().getColor(R.color.isDefault)
249+
: mainCard.getResources().getColor(R.color.dsWhite);
250+
mainCard.setCardBackgroundColor(newColor);
251+
252+
View deleteBtn = view.findViewById(R.id.itemCardDeleteBtn);
253+
deleteBtn.setOnClickListener(view -> activity.deleteUrl(adapter, item));
254+
View editBtn = view.findViewById(R.id.itemCardEditBtn);
255+
editBtn.setOnClickListener(view -> activity.editUrl(item));
256+
defaultSwitch.setOnClickListener(view -> activity.toggleDefault(adapter, item));
182257
}
183258
}
184259
}

app/src/main/java/org/docspell/docspellshare/activity/ShareActivity.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,17 @@
44
import android.content.Intent;
55
import android.net.Uri;
66
import android.os.Bundle;
7-
import android.os.Process;
87
import android.util.Log;
98
import android.widget.ProgressBar;
109
import android.widget.TextView;
1110

1211
import androidx.appcompat.app.AppCompatActivity;
1312

14-
import org.docspell.docspellshare.HttpRequest;
13+
import org.docspell.docspellshare.http.HttpRequest;
1514
import org.docspell.docspellshare.R;
16-
import org.docspell.docspellshare.Strings;
17-
import org.docspell.docspellshare.UploadManager;
15+
import org.docspell.docspellshare.util.Strings;
16+
import org.docspell.docspellshare.http.UploadManager;
1817

19-
import java.io.FileNotFoundException;
2018
import java.util.Collections;
2119
import java.util.List;
2220

0 commit comments

Comments
 (0)