Improve AddFeedActivity UI

This commit is contained in:
Shinokuni 2019-10-08 17:44:17 +02:00
parent e51b9e8a7e
commit 5d28f34a8d
2 changed files with 177 additions and 186 deletions

View File

@ -2,27 +2,23 @@ package com.readrops.app.activities;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Patterns;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.widget.NestedScrollView;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.textfield.TextInputEditText;
import com.mikepenz.fastadapter.FastAdapter;
import com.mikepenz.fastadapter.adapters.ItemAdapter;
import com.mikepenz.fastadapter.commons.utils.DiffCallback;
@ -31,6 +27,7 @@ import com.readrops.app.R;
import com.readrops.app.adapters.AccountArrayAdapter;
import com.readrops.app.database.entities.Feed;
import com.readrops.app.database.entities.account.Account;
import com.readrops.app.databinding.ActivityAddFeedBinding;
import com.readrops.app.utils.FeedInsertionResult;
import com.readrops.app.utils.ParsingResult;
import com.readrops.app.utils.ReadropsItemTouchCallback;
@ -47,19 +44,6 @@ import io.reactivex.schedulers.Schedulers;
public class AddFeedActivity extends AppCompatActivity implements View.OnClickListener {
private TextInputEditText feedInput;
private Button load;
private ProgressBar progressBar;
private Button validate;
private RecyclerView parseResultsRecyclerView;
private TextView resultsTextView;
private NestedScrollView rootLayout;
private ProgressBar feedInsertionProgressBar;
private RecyclerView insertionResultsRecyclerView;
private Spinner accountSpinner;
private AccountArrayAdapter arrayAdapter;
private ItemAdapter<ParsingResult> parseItemsAdapter;
@ -69,34 +53,25 @@ public class AddFeedActivity extends AppCompatActivity implements View.OnClickLi
private AddFeedsViewModel viewModel;
private ArrayList<Feed> feedsToUpdate;
private ActivityAddFeedBinding binding;
@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_feed);
binding = DataBindingUtil.setContentView(this, R.layout.activity_add_feed);
feedInput = findViewById(R.id.add_feed_text_input);
load = findViewById(R.id.add_feed_load);
validate = findViewById(R.id.add_feed_ok);
progressBar = findViewById(R.id.add_feed_loading);
parseResultsRecyclerView = findViewById(R.id.add_feed_results);
resultsTextView = findViewById(R.id.add_feed_results_text_view);
feedInsertionProgressBar = findViewById(R.id.add_feed_insert_progressbar);
insertionResultsRecyclerView = findViewById(R.id.add_feed_inserted_results_recyclerview);
accountSpinner = findViewById(R.id.add_feed_account_spinner);
binding.addFeedLoad.setOnClickListener(this);
binding.addFeedOk.setOnClickListener(this);
binding.addFeedOk.setEnabled(false);
rootLayout = findViewById(R.id.add_feed_root);
load.setOnClickListener(this);
validate.setOnClickListener(this);
validate.setEnabled(false);
feedInput.setOnTouchListener((v, event) -> {
binding.addFeedTextInput.setOnTouchListener((v, event) -> {
final int DRAWABLE_RIGHT = 2;
int drawablePos = (feedInput.getRight() - feedInput.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width());
int drawablePos = (binding.addFeedTextInput.getRight() -
binding.addFeedTextInput.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width());
if (event.getAction() == MotionEvent.ACTION_UP && event.getRawX() >= drawablePos) {
feedInput.setText("");
binding.addFeedTextInput.setText("");
return true;
}
@ -117,39 +92,41 @@ public class AddFeedActivity extends AppCompatActivity implements View.OnClickLi
fastAdapter.notifyAdapterItemChanged(position);
}
validate.setEnabled(recyclerViewHasCheckedItems());
binding.addFeedOk.setEnabled(recyclerViewHasCheckedItems());
return true;
});
parseResultsRecyclerView.setAdapter(fastAdapter);
binding.addFeedResults.setAdapter(fastAdapter);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
parseResultsRecyclerView.setLayoutManager(layoutManager);
binding.addFeedResults.setLayoutManager(layoutManager);
new ItemTouchHelper(new ReadropsItemTouchCallback(this,
new ReadropsItemTouchCallback.Config.Builder()
.swipeDirs(ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT)
.leftDraw(Color.RED, R.drawable.ic_delete)
.rightDraw(Color.RED, R.drawable.ic_delete)
.swipeCallback((viewHolder, direction) -> {
parseItemsAdapter.remove(viewHolder.getAdapterPosition());
if (parseItemsAdapter.getAdapterItemCount() == 0) {
resultsTextView.setVisibility(View.GONE);
parseResultsRecyclerView.setVisibility(View.GONE);
binding.addFeedResultsTextView.setVisibility(View.GONE);
binding.addFeedResults.setVisibility(View.GONE);
}
})
.build()))
.attachToRecyclerView(parseResultsRecyclerView);
.attachToRecyclerView(binding.addFeedResults);
insertionResultsAdapter = new ItemAdapter<>();
RecyclerView.LayoutManager layoutManager1 = new LinearLayoutManager(this);
insertionResultsRecyclerView.setAdapter(FastAdapter.with(insertionResultsAdapter));
insertionResultsRecyclerView.setLayoutManager(layoutManager1);
binding.addFeedInsertedResultsRecyclerview.setAdapter(FastAdapter.with(insertionResultsAdapter));
binding.addFeedInsertedResultsRecyclerview.setLayoutManager(layoutManager1);
viewModel.getAccounts().observe(this, accounts -> {
arrayAdapter = new AccountArrayAdapter(this, accounts);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
accountSpinner.setAdapter(arrayAdapter);
binding.addFeedAccountSpinner.setAdapter(arrayAdapter);
});
feedsToUpdate = new ArrayList<>();
@ -160,7 +137,7 @@ public class AddFeedActivity extends AppCompatActivity implements View.OnClickLi
switch (v.getId()) {
case R.id.add_feed_load:
if (isValidUrl()) {
progressBar.setVisibility(View.VISIBLE);
binding.addFeedLoading.setVisibility(View.VISIBLE);
loadFeed();
}
break;
@ -172,13 +149,13 @@ public class AddFeedActivity extends AppCompatActivity implements View.OnClickLi
}
private boolean isValidUrl() {
String url = feedInput.getText().toString().trim();
String url = binding.addFeedTextInput.getText().toString().trim();
if (url.isEmpty()) {
feedInput.setError(getString(R.string.empty_field));
binding.addFeedTextInput.setError(getString(R.string.empty_field));
return false;
} else if (!Patterns.WEB_URL.matcher(url).matches()) {
feedInput.setError(getString(R.string.wrong_url));
binding.addFeedTextInput.setError(getString(R.string.wrong_url));
return false;
} else
return true;
@ -203,7 +180,7 @@ public class AddFeedActivity extends AppCompatActivity implements View.OnClickLi
}
private void loadFeed() {
String url = feedInput.getText().toString().trim();
String url = binding.addFeedTextInput.getText().toString().trim();
final String finalUrl;
if (!(url.contains(Utils.HTTP_PREFIX) || url.contains(Utils.HTTPS_PREFIX)))
@ -222,17 +199,18 @@ public class AddFeedActivity extends AppCompatActivity implements View.OnClickLi
@Override
public void onError(Throwable e) {
Utils.showSnackbar(rootLayout, e.getMessage());
progressBar.setVisibility(View.GONE);
Utils.showSnackbar(binding.addFeedRoot, e.getMessage());
binding.addFeedLoading.setVisibility(View.GONE);
}
});
}
private void displayParseResults(List<ParsingResult> parsingResultList) {
binding.addFeedLoading.setVisibility(View.GONE);
if (!parsingResultList.isEmpty()) {
parseResultsRecyclerView.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.GONE);
resultsTextView.setVisibility(View.VISIBLE);
binding.addFeedResultsTextView.setVisibility(View.VISIBLE);
binding.addFeedResults.setVisibility(View.VISIBLE);
DiffUtil.DiffResult diffResult = FastAdapterDiffUtil.calculateDiff(parseItemsAdapter, parsingResultList, new DiffCallback<ParsingResult>() {
@Override
@ -255,14 +233,13 @@ public class AddFeedActivity extends AppCompatActivity implements View.OnClickLi
}, false);
FastAdapterDiffUtil.set(parseItemsAdapter, diffResult);
validate.setEnabled(recyclerViewHasCheckedItems());
} else
progressBar.setVisibility(View.GONE);
binding.addFeedOk.setEnabled(recyclerViewHasCheckedItems());
}
}
private void insertFeeds() {
feedInsertionProgressBar.setVisibility(View.VISIBLE);
validate.setEnabled(false);
binding.addFeedInsertProgressbar.setVisibility(View.VISIBLE);
binding.addFeedOk.setEnabled(false);
List<ParsingResult> feedsToInsert = new ArrayList<>();
for (ParsingResult result : parseItemsAdapter.getAdapterItems()) {
@ -270,7 +247,7 @@ public class AddFeedActivity extends AppCompatActivity implements View.OnClickLi
feedsToInsert.add(result);
}
Account account = (Account) accountSpinner.getSelectedItem();
Account account = (Account) binding.addFeedAccountSpinner.getSelectedItem();
account.setLogin(SharedPreferencesManager.readString(this, account.getLoginKey()));
account.setPassword(SharedPreferencesManager.readString(this, account.getPasswordKey()));
@ -286,16 +263,16 @@ public class AddFeedActivity extends AppCompatActivity implements View.OnClickLi
@Override
public void onError(Throwable e) {
feedInsertionProgressBar.setVisibility(View.GONE);
validate.setEnabled(true);
Utils.showSnackbar(rootLayout, e.getMessage());
binding.addFeedInsertProgressbar.setVisibility(View.GONE);
binding.addFeedOk.setEnabled(true);
Utils.showSnackbar(binding.addFeedRoot, e.getMessage());
}
});
}
private void displayInsertionResults(List<FeedInsertionResult> feedInsertionResults) {
feedInsertionProgressBar.setVisibility(View.GONE);
insertionResultsRecyclerView.setVisibility(View.VISIBLE);
binding.addFeedInsertProgressbar.setVisibility(View.GONE);
binding.addFeedInsertedResultsRecyclerview.setVisibility(View.VISIBLE);
for (FeedInsertionResult feedInsertionResult : feedInsertionResults) {
if (feedInsertionResult.getFeed() != null)
@ -305,7 +282,7 @@ public class AddFeedActivity extends AppCompatActivity implements View.OnClickLi
}
insertionResultsAdapter.add(feedInsertionResults);
validate.setEnabled(recyclerViewHasCheckedItems());
binding.addFeedOk.setEnabled(recyclerViewHasCheckedItems());
}
@ -342,7 +319,7 @@ public class AddFeedActivity extends AppCompatActivity implements View.OnClickLi
public boolean onKeyUp(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
onClick(load);
onClick(binding.addFeedLoad);
return true;
default:
return super.onKeyUp(keyCode, event);

View File

@ -1,140 +1,154 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/add_feed_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.AddFeedActivity">
<androidx.constraintlayout.widget.ConstraintLayout
<androidx.core.widget.NestedScrollView
android:id="@+id/add_feed_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:padding="20dp">
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/add_feed_main_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
android:layout_height="match_parent"
android:animateLayoutChanges="true">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/add_feed_input_layout"
<Spinner
android:id="@+id/add_feed_account_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
app:layout_constraintTop_toTopOf="parent"
tools:listitem="@layout/account_type_item" />
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/add_feed_text_input"
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/add_feed_main_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/add_feed_account_spinner"
android:padding="16dp">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/add_feed_input_layout"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toStartOf="@id/add_feed_load">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/add_feed_text_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableEnd="@drawable/ic_cancel_grey"
android:hint="@string/feed_url"
android:inputType="text"/>
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="@+id/add_feed_load"
style="@style/GenericButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginStart="6dp"
android:text="@string/load"
app:layout_constraintStart_toEndOf="@id/add_feed_input_layout"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<ProgressBar
android:id="@+id/add_feed_loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:visibility="gone"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/add_feed_input_layout"
tools:visibility="gone" />
<TextView
android:id="@+id/add_feed_results_text_view"
style="@style/TextAppearance.Design.Hint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="@string/results"
android:visibility="gone"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/add_feed_loading"
tools:visibility="visible" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/add_feed_results"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableEnd="@drawable/ic_cancel_grey"
android:hint="@string/feed_url"
android:inputType="text"/>
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/add_feed_results_text_view"
tools:visibility="visible"
tools:listitem="@layout/add_feed_item"
tools:itemCount="2"/>
</com.google.android.material.textfield.TextInputLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<Button
android:id="@+id/add_feed_load"
style="@style/GenericButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="@string/load"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/add_feed_input_layout" />
<ProgressBar
android:id="@+id/add_feed_loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:visibility="gone"
app:layout_constraintStart_toEndOf="@+id/add_feed_load"
app:layout_constraintTop_toBottomOf="@+id/add_feed_input_layout"
tools:visibility="visible" />
<TextView
android:id="@+id/add_feed_results_text_view"
style="@style/TextAppearance.Design.Hint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/results"
android:visibility="gone"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/add_feed_load"
tools:visibility="visible" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/add_feed_results"
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/add_feed_results_text_view"
tools:visibility="visible"
tools:listitem="@layout/add_feed_item"
tools:itemCount="2"/>
android:layout_height="match_parent"
app:layout_constraintTop_toBottomOf="@id/add_feed_main_layout"
android:paddingStart="16dp"
android:paddingEnd="16dp">
<Button
android:id="@+id/add_feed_ok"
style="@style/GenericButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:enabled="true"
android:text="@string/validate"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.6"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ProgressBar
android:id="@+id/add_feed_insert_progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/add_feed_ok"
tools:visibility="gone" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/add_feed_inserted_results_recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.6"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/add_feed_insert_progressbar"
tools:visibility="visible"
tools:listitem="@layout/feed_insertion_result"
tools:itemCount="2"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<Spinner
android:id="@+id/add_feed_account_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/add_feed_main_layout"
tools:listitem="@layout/account_type_item"/>
</androidx.core.widget.NestedScrollView>
<Button
android:id="@+id/add_feed_ok"
style="@style/GenericButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:enabled="true"
android:text="@string/validate"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.6"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/add_feed_account_spinner" />
<ProgressBar
android:id="@+id/add_feed_insert_progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/add_feed_ok"
tools:visibility="gone" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/add_feed_inserted_results_recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.6"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/add_feed_insert_progressbar"
tools:visibility="visible"
tools:listitem="@layout/feed_insertion_result"
tools:itemCount="2"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
</layout>