Skip to content

Commit df0bfae

Browse files
committed
Implement file selection.
1 parent 61c9193 commit df0bfae

7 files changed

Lines changed: 126 additions & 4 deletions

File tree

app/src/main/java/com/paulds/simpleftp/presentation/model/ExplorerViewModel.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import android.databinding.Bindable;
99
import android.databinding.ObservableArrayList;
1010
import android.databinding.ObservableBoolean;
11+
import android.databinding.ObservableInt;
1112
import android.os.Handler;
1213
import android.support.v7.app.AlertDialog;
1314
import android.support.v7.view.menu.MenuBuilder;
@@ -69,6 +70,16 @@ public class ExplorerViewModel extends BaseObservable {
6970
@Bindable
7071
public ObservableArrayList<FileViewModel> files;
7172

73+
/**
74+
* Indicates whether the view is in selection mode.
75+
*/
76+
public ObservableBoolean isSelectionMode;
77+
78+
/**
79+
* The number of selected items
80+
*/
81+
public ObservableInt numberSelectedItems;
82+
7283
/**
7384
* Default constructor.
7485
* @param context The context of the current activity.
@@ -77,6 +88,8 @@ public ExplorerViewModel(Context context) {
7788
this.context = context;
7889
this.isLoading = new ObservableBoolean(false);
7990
this.files = new ObservableArrayList<FileViewModel>();
91+
this.isSelectionMode = new ObservableBoolean(false);
92+
this.numberSelectedItems = new ObservableInt(0);
8093
this.changeDirectory("/");
8194
}
8295

@@ -236,4 +249,31 @@ else if(item.getItemId() == KEY_ADD_NEW_FAVORITE) {
236249

237250
popupMenu.show();
238251
}
252+
253+
/**
254+
* Refresh the number of selected items.
255+
*/
256+
public void refreshSelectedItems() {
257+
int total = 0;
258+
259+
for (FileViewModel file: files) {
260+
if(file.isSelected.get()) {
261+
total++;
262+
}
263+
}
264+
265+
this.numberSelectedItems.set(total);
266+
}
267+
268+
/**
269+
* Clear the selection.
270+
* @param view The current view.
271+
*/
272+
public void clearSelection(View view) {
273+
for (FileViewModel file: files) {
274+
file.isSelected.set(false);
275+
}
276+
277+
this.isSelectionMode.set(false);
278+
}
239279
}

app/src/main/java/com/paulds/simpleftp/presentation/model/FileViewModel.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.paulds.simpleftp.presentation.model;
22

33
import android.databinding.Bindable;
4+
import android.databinding.ObservableBoolean;
45
import android.view.View;
56

67
import com.paulds.simpleftp.R;
@@ -42,12 +43,18 @@ public class FileViewModel {
4243
*/
4344
private int icon;
4445

46+
/**
47+
* Indicates whether the file item is selected.
48+
*/
49+
public ObservableBoolean isSelected;
50+
4551
/**
4652
* Default constructor.
4753
* @param mainViewModel The parent view model.
4854
*/
4955
public FileViewModel(ExplorerViewModel mainViewModel) {
5056
this.mainViewModel = mainViewModel;
57+
this.isSelected = new ObservableBoolean(false);
5158
}
5259

5360
/**
@@ -174,11 +181,39 @@ else if(size > 1024) {
174181
}
175182
}
176183

184+
/**
185+
* Gets the main model.
186+
* @return The main model.
187+
*/
188+
public ExplorerViewModel getMainModel() {
189+
return this.mainViewModel;
190+
}
191+
177192
/**
178193
* Called when the file item is clicked.
179194
* @param view The current view.
180195
*/
181196
public void onItemClick(View view) {
182-
this.mainViewModel.selectFile(this);
197+
if(this.mainViewModel.isSelectionMode.get()) {
198+
this.isSelected.set(!this.isSelected.get());
199+
this.mainViewModel.refreshSelectedItems();
200+
}
201+
else {
202+
this.mainViewModel.selectFile(this);
203+
}
204+
}
205+
206+
/**
207+
* Called when the file item is long clicked.
208+
* @param view The current view.
209+
*/
210+
public boolean onItemLongClick(View view) {
211+
if(!this.mainViewModel.isSelectionMode.get()) {
212+
this.mainViewModel.isSelectionMode.set(true);
213+
this.isSelected.set(true);
214+
this.mainViewModel.refreshSelectedItems();
215+
}
216+
217+
return true;
183218
}
184219
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportWidth="24.0"
5+
android:viewportHeight="24.0">
6+
<path
7+
android:fillColor="#FFFFFFFF"
8+
android:pathData="M19,6.41L17.59,5 12,10.59 6.41,5 5,6.41 10.59,12 5,17.59 6.41,19 12,13.41 17.59,19 19,17.59 13.41,12z"/>
9+
</vector>

app/src/main/res/layout/fragment_explorer.xml

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
android:layout_width="match_parent"
4242
android:layout_height="@dimen/toolbar_size"
4343
android:background="@color/toolbar"
44-
android:elevation="2dp">
44+
android:elevation="2dp"
45+
android:visibility="@{model.isSelectionMode ? View.GONE : View.VISIBLE}">
4546

4647
<TextView
4748
android:layout_width="wrap_content"
@@ -67,6 +68,34 @@
6768

6869
</android.support.v7.widget.Toolbar>
6970

71+
<android.support.v7.widget.Toolbar
72+
android:layout_width="match_parent"
73+
android:layout_height="@dimen/toolbar_size"
74+
android:background="@color/selection_toolbar"
75+
android:elevation="2dp"
76+
android:visibility="@{model.isSelectionMode ? View.VISIBLE : View.GONE}">
77+
78+
<ImageView
79+
android:layout_width="wrap_content"
80+
android:layout_height="match_parent"
81+
android:layout_gravity="center_vertical"
82+
android:src="@drawable/ic_clear_24dp"
83+
android:onClick="@{model.clearSelection}"
84+
android:clickable="true"
85+
android:focusable="true"
86+
android:focusableInTouchMode="true"
87+
android:paddingRight="15dp" />
88+
89+
<TextView
90+
android:layout_width="wrap_content"
91+
android:layout_height="wrap_content"
92+
android:text="@{model.numberSelectedItems + &quot; &quot; + (model.numberSelectedItems > 1 ? @string/activity_list_files_selected_items : @string/activity_list_files_selected_item)}"
93+
android:layout_gravity="left"
94+
android:textColor="@color/selection_toolbar_text"
95+
android:textSize="@dimen/toolbar_title_text_size" />
96+
97+
</android.support.v7.widget.Toolbar>
98+
7099
<android.support.design.widget.FloatingActionButton
71100
android:id="@+id/fabPlus"
72101
android:layout_width="wrap_content"

app/src/main/res/layout/row_file.xml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
xmlns:app="http://schemas.android.com/apk/res-auto">
44
<data>
55
<import type="com.paulds.simpleftp.presentation.model.FileViewModel" />
6+
<import type="com.paulds.simpleftp.R" />
7+
<import type="android.view.View"/>
68
<variable name="file" type="FileViewModel" />
79
</data>
810

@@ -13,8 +15,9 @@
1315
android:padding="5dp"
1416
android:clickable="true"
1517
android:focusable="true"
16-
android:background="?android:attr/selectableItemBackground"
17-
android:onClick="@{file.onItemClick}">
18+
app:backgroundResource="@{file.isSelected ? R.color.selected_item : R.color.unselected_item}"
19+
android:onClick="@{file.onItemClick}"
20+
android:onLongClick="@{file.onItemLongClick}">
1821

1922
<ImageView
2023
android:layout_width="48dp"

app/src/main/res/values/colors.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@
66
<color name="action_bar_text">#FFFFFF</color>
77
<color name="toolbar_text">#000000</color>
88
<color name="toolbar">#DDDDDD</color>
9+
<color name="selection_toolbar_text">#FFFFFF</color>
10+
<color name="selection_toolbar">#62890F</color>
11+
<color name="selected_item">#EBF2DC</color>
12+
<color name="unselected_item">#FFFFFF</color>
913
</resources>

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
<string name="activity_edit_server_is_anonymous">Anonymous connection</string>
1010
<string name="activity_edit_server_login">Login</string>
1111
<string name="activity_edit_server_password">Password</string>
12+
<string name="activity_list_files_selected_item">element selected</string>
13+
<string name="activity_list_files_selected_items">elements selected</string>
1214
<string name="dialog_create_folder_message">Enter the folder name:</string>
1315
<string name="dialog_create_folder_positive_button">Create</string>
1416
<string name="dialog_create_folder_negative_button">Cancel</string>

0 commit comments

Comments
 (0)