Skip to content
Snippets Groups Projects
Commit 19671e64 authored by ankitgusai's avatar ankitgusai
Browse files

-Modified AppListingFragment

 Multiple item selection implemented
parent 151ea9c5
No related branches found
Tags tbb-8.0.6-build1
No related merge requests found
......@@ -9,6 +9,7 @@
<entry key="app/src/main/res/layout/fragment_home.xml" value="0.1" />
<entry key="app/src/main/res/layout/fragment_log.xml" value="0.1" />
<entry key="app/src/main/res/layout/fragment_notifications.xml" value="0.1" />
<entry key="app/src/main/res/layout/item_apps.xml" value="0.5" />
<entry key="app/src/main/res/layout/log_item.xml" value="0.1" />
<entry key="app/src/main/res/menu/bottom_nav_menu.xml" value="0.18125" />
<entry key="app/src/main/res/menu/menu_logs.xml" value="0.14479166666666668" />
......
......@@ -5,10 +5,14 @@ import android.graphics.drawable.Drawable;
class AppItem implements Comparable<AppItem> {
String appName;
Drawable appIcon;
boolean isSelected;
int appUID;
public AppItem(String appName, Drawable appIcon) {
public AppItem(String appName, Drawable appIcon, boolean isSelected, int appUID) {
this.appName = appName;
this.appIcon = appIcon;
this.isSelected = isSelected;
this.appUID = appUID;
}
@Override
......
package org.torproject.artitoyvpn.ui.appListing;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
......@@ -11,8 +12,13 @@ import org.torproject.artitoyvpn.databinding.ItemAppsBinding;
import java.util.ArrayList;
import java.util.List;
class AppListAdapter extends RecyclerView.Adapter<AppListViewHolder> {
class AppListAdapter extends RecyclerView.Adapter<AppListAdapter.AppListViewHolder> {
private final List<AppItem> items = new ArrayList<>();
private final AppListingViewModel viewModel;
public AppListAdapter(AppListingViewModel viewModel) {
this.viewModel = viewModel;
}
@NonNull
@Override
......@@ -22,7 +28,7 @@ class AppListAdapter extends RecyclerView.Adapter<AppListViewHolder> {
@Override
public void onBindViewHolder(@NonNull AppListViewHolder holder, int position) {
holder.setAppDetail(items.get(position));
holder.setAppDetail(items.get(position), position);
}
@Override
......@@ -35,18 +41,31 @@ class AppListAdapter extends RecyclerView.Adapter<AppListViewHolder> {
items.addAll(list);
notifyDataSetChanged();
}
}
class AppListViewHolder extends RecyclerView.ViewHolder {
private final ItemAppsBinding mBinding;
public AppListViewHolder(@NonNull ItemAppsBinding mBinding) {
super(mBinding.getRoot());
this.mBinding = mBinding;
}
class AppListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private final ItemAppsBinding mBinding;
private int pos = -1;
public void setAppDetail(AppItem appItem) {
mBinding.imageView.setImageDrawable(appItem.appIcon);
mBinding.textView.setText(appItem.appName);
public AppListViewHolder(@NonNull ItemAppsBinding mBinding) {
super(mBinding.getRoot());
this.mBinding = mBinding;
}
public void setAppDetail(AppItem appItem, int pos) {
this.pos = pos;
mBinding.imageView.setImageDrawable(appItem.appIcon);
mBinding.textView.setText(appItem.appName);
mBinding.textView3.setText(String.valueOf(appItem.appUID));
mBinding.checkBox.setChecked(appItem.isSelected);
mBinding.checkBox.setOnClickListener(this);
}
@Override
public void onClick(View v) {
AppListAdapter.this.viewModel.rowClicked(pos, mBinding.checkBox.isChecked());
}
}
}
......@@ -18,6 +18,7 @@ import org.torproject.artitoyvpn.databinding.AppListingFragmentBinding;
public class AppListingFragment extends Fragment {
private AppListingViewModel mViewModel;
private AppListingFragmentBinding mBinding;
private AppListAdapter appListAdapter;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
......@@ -29,15 +30,15 @@ public class AppListingFragment extends Fragment {
LinearLayoutManager layoutManager = new LinearLayoutManager(requireContext());
mBinding.appList.setLayoutManager(layoutManager);
AppListAdapter appListAdapter = new AppListAdapter();
appListAdapter = new AppListAdapter(mViewModel);
mBinding.appList.setAdapter(appListAdapter);
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(requireContext(), layoutManager.getOrientation());
mBinding.appList.addItemDecoration(dividerItemDecoration);
mViewModel.getList().observe(getViewLifecycleOwner(), appListAdapter::setData);
mViewModel.isLoading().observe(getViewLifecycleOwner(), this::toggleProgressBar);
mViewModel.getAppCount().observe(getViewLifecycleOwner(), this::setAppCount);
mViewModel.getItemChanged().observe(getViewLifecycleOwner(), this::setItemChange);
return mBinding.getRoot();
......@@ -51,6 +52,10 @@ public class AppListingFragment extends Fragment {
mBinding.textView2.setText(getString(R.string.info_app_count, String.valueOf(count)));
}
private void setItemChange(int pos) {
appListAdapter.notifyItemChanged(pos);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
......
......@@ -20,6 +20,7 @@ import java.util.List;
public class AppListingViewModel extends AndroidViewModel {
private final MutableLiveData<List<AppItem>> appList = new MutableLiveData<>();
private final MutableLiveData<Integer> installedAppCount = new MutableLiveData<>();
private final MutableLiveData<Integer> itemChanged = new MutableLiveData<>();
private final MutableLiveData<Boolean> isLoading = new MutableLiveData<>();
public AppListingViewModel(@NonNull Application application) {
......@@ -35,7 +36,8 @@ public class AppListingViewModel extends AndroidViewModel {
for (int i = 0; i < appInfos.size(); i++) {
String appName = (String) pm.getApplicationLabel(appInfos.get(i));
Drawable appIcon = appInfos.get(i).loadIcon(pm);
list.add(new AppItem(appName, appIcon));
int appUID = appInfos.get(i).uid;
list.add(new AppItem(appName, appIcon, false, appUID));
}
installedAppCount.postValue(appInfos.size());
isLoading.postValue(false);
......@@ -48,6 +50,10 @@ public class AppListingViewModel extends AndroidViewModel {
return appList;
}
public LiveData<Integer> getItemChanged() {
return itemChanged;
}
public LiveData<Boolean> isLoading() {
return isLoading;
}
......@@ -56,6 +62,12 @@ public class AppListingViewModel extends AndroidViewModel {
return installedAppCount;
}
public void rowClicked(int pos, boolean checked){
appList.getValue().get(pos).isSelected = checked;
}
}
......
......@@ -9,11 +9,10 @@
android:id="@+id/imageView"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintStart_toEndOf="@+id/checkBox"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="@tools:sample/avatars" />
......@@ -28,4 +27,22 @@
android:textStyle="bold"
app:layout_constraintStart_toEndOf="@+id/imageView"
app:layout_constraintTop_toTopOf="@+id/imageView" />
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
app:layout_constraintBottom_toBottomOf="@+id/imageView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/imageView" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
app:layout_constraintStart_toStartOf="@+id/textView"
app:layout_constraintTop_toBottomOf="@+id/textView"
tools:text="1121231212312313" />
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment