Refactor : NotificationActivity uses ViewBinding. (#5606)

This commit is contained in:
Neel Doshi 2024-03-26 08:09:32 +05:30 committed by GitHub
parent c41940241b
commit 21fdd39d7c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 36 additions and 66 deletions

View file

@ -9,20 +9,12 @@ import android.view.Menu;
import android.view.MenuInflater; import android.view.MenuInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.widget.Toolbar;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.DividerItemDecoration; import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.google.android.material.snackbar.Snackbar; import com.google.android.material.snackbar.Snackbar;
import fr.free.nrw.commons.R; import fr.free.nrw.commons.R;
import fr.free.nrw.commons.Utils; import fr.free.nrw.commons.Utils;
import fr.free.nrw.commons.databinding.ActivityNotificationBinding;
import fr.free.nrw.commons.notification.models.Notification; import fr.free.nrw.commons.notification.models.Notification;
import fr.free.nrw.commons.theme.BaseActivity; import fr.free.nrw.commons.theme.BaseActivity;
import fr.free.nrw.commons.utils.NetworkUtils; import fr.free.nrw.commons.utils.NetworkUtils;
@ -44,19 +36,7 @@ import timber.log.Timber;
*/ */
public class NotificationActivity extends BaseActivity { public class NotificationActivity extends BaseActivity {
@BindView(R.id.listView) private ActivityNotificationBinding binding;
RecyclerView recyclerView;
@BindView(R.id.progressBar)
ProgressBar progressBar;
@BindView(R.id.container)
RelativeLayout relativeLayout;
@BindView(R.id.no_notification_background)
ConstraintLayout no_notification;
@BindView(R.id.no_notification_text)
TextView noNotificationText;
@BindView(R.id.toolbar)
Toolbar toolbar;
@Inject @Inject
NotificationController controller; NotificationController controller;
@ -75,13 +55,13 @@ public class NotificationActivity extends BaseActivity {
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
isRead = getIntent().getStringExtra("title").equals("read"); isRead = getIntent().getStringExtra("title").equals("read");
setContentView(R.layout.activity_notification); binding = ActivityNotificationBinding.inflate(getLayoutInflater());
ButterKnife.bind(this); setContentView(binding.getRoot());
mNotificationWorkerFragment = (NotificationWorkerFragment) getFragmentManager() mNotificationWorkerFragment = (NotificationWorkerFragment) getFragmentManager()
.findFragmentByTag(TAG_NOTIFICATION_WORKER_FRAGMENT); .findFragmentByTag(TAG_NOTIFICATION_WORKER_FRAGMENT);
initListView(); initListView();
setPageTitle(); setPageTitle();
setSupportActionBar(toolbar); setSupportActionBar(binding.toolbar.toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true);
} }
@ -115,26 +95,23 @@ public class NotificationActivity extends BaseActivity {
notificationList.remove(notification); notificationList.remove(notification);
setItems(notificationList); setItems(notificationList);
adapter.notifyDataSetChanged(); adapter.notifyDataSetChanged();
Snackbar snackbar = Snackbar ViewUtil.showLongSnackbar(binding.container,getString(R.string.notification_mark_read));
.make(relativeLayout, getString(R.string.notification_mark_read), Snackbar.LENGTH_LONG);
snackbar.show();
if (notificationList.size() == 0) { if (notificationList.size() == 0) {
setEmptyView(); setEmptyView();
relativeLayout.setVisibility(View.GONE); binding.container.setVisibility(View.GONE);
no_notification.setVisibility(View.VISIBLE); binding.noNotificationBackground.setVisibility(View.VISIBLE);
} }
} else { } else {
adapter.notifyDataSetChanged(); adapter.notifyDataSetChanged();
setItems(notificationList); setItems(notificationList);
Toast.makeText(NotificationActivity.this, getString(R.string.some_error), Toast.LENGTH_SHORT).show(); ViewUtil.showLongToast(this,getString(R.string.some_error));
} }
}, throwable -> { }, throwable -> {
Timber.e(throwable, "Error occurred while loading notifications"); Timber.e(throwable, "Error occurred while loading notifications");
throwable.printStackTrace(); throwable.printStackTrace();
ViewUtil.showShortSnackbar(relativeLayout, R.string.error_notifications); ViewUtil.showShortSnackbar(binding.container, R.string.error_notifications);
progressBar.setVisibility(View.GONE); binding.progressBar.setVisibility(View.GONE);
}); });
compositeDisposable.add(disposable); compositeDisposable.add(disposable);
} }
@ -142,9 +119,9 @@ public class NotificationActivity extends BaseActivity {
private void initListView() { private void initListView() {
recyclerView.setLayoutManager(new LinearLayoutManager(this)); binding.listView.setLayoutManager(new LinearLayoutManager(this));
DividerItemDecoration itemDecor = new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL); DividerItemDecoration itemDecor = new DividerItemDecoration(binding.listView.getContext(), DividerItemDecoration.VERTICAL);
recyclerView.addItemDecoration(itemDecor); binding.listView.addItemDecoration(itemDecor);
if (isRead) { if (isRead) {
refresh(true); refresh(true);
} else { } else {
@ -156,27 +133,27 @@ public class NotificationActivity extends BaseActivity {
removeNotification(item); removeNotification(item);
return Unit.INSTANCE; return Unit.INSTANCE;
}); });
recyclerView.setAdapter(this.adapter); binding.listView.setAdapter(adapter);
} }
private void refresh(boolean archived) { private void refresh(boolean archived) {
if (!NetworkUtils.isInternetConnectionEstablished(this)) { if (!NetworkUtils.isInternetConnectionEstablished(this)) {
progressBar.setVisibility(View.GONE); binding.progressBar.setVisibility(View.GONE);
Snackbar.make(relativeLayout, R.string.no_internet, Snackbar.LENGTH_INDEFINITE) Snackbar.make(binding.container, R.string.no_internet, Snackbar.LENGTH_INDEFINITE)
.setAction(R.string.retry, view -> refresh(archived)).show(); .setAction(R.string.retry, view -> refresh(archived)).show();
} else { } else {
addNotifications(archived); addNotifications(archived);
} }
progressBar.setVisibility(View.VISIBLE); binding.progressBar.setVisibility(View.VISIBLE);
no_notification.setVisibility(View.GONE); binding.noNotificationBackground.setVisibility(View.GONE);
relativeLayout.setVisibility(View.VISIBLE); binding.container.setVisibility(View.VISIBLE);
} }
@SuppressLint("CheckResult") @SuppressLint("CheckResult")
private void addNotifications(boolean archived) { private void addNotifications(boolean archived) {
Timber.d("Add notifications"); Timber.d("Add notifications");
if (mNotificationWorkerFragment == null) { if (mNotificationWorkerFragment == null) {
progressBar.setVisibility(View.VISIBLE); binding.progressBar.setVisibility(View.VISIBLE);
compositeDisposable.add(controller.getNotifications(archived) compositeDisposable.add(controller.getNotifications(archived)
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
@ -186,16 +163,16 @@ public class NotificationActivity extends BaseActivity {
this.notificationList = notificationList; this.notificationList = notificationList;
if (notificationList.size()==0){ if (notificationList.size()==0){
setEmptyView(); setEmptyView();
relativeLayout.setVisibility(View.GONE); binding.container.setVisibility(View.GONE);
no_notification.setVisibility(View.VISIBLE); binding.noNotificationBackground.setVisibility(View.VISIBLE);
} else { } else {
setItems(notificationList); setItems(notificationList);
} }
progressBar.setVisibility(View.GONE); binding.progressBar.setVisibility(View.GONE);
}, throwable -> { }, throwable -> {
Timber.e(throwable, "Error occurred while loading notifications"); Timber.e(throwable, "Error occurred while loading notifications");
ViewUtil.showShortSnackbar(relativeLayout, R.string.error_notifications); ViewUtil.showShortSnackbar(binding.container, R.string.error_notifications);
progressBar.setVisibility(View.GONE); binding.progressBar.setVisibility(View.GONE);
})); }));
} else { } else {
notificationList = mNotificationWorkerFragment.getNotificationList(); notificationList = mNotificationWorkerFragment.getNotificationList();
@ -237,16 +214,16 @@ public class NotificationActivity extends BaseActivity {
private void setItems(List<Notification> notificationList) { private void setItems(List<Notification> notificationList) {
if (notificationList == null || notificationList.isEmpty()) { if (notificationList == null || notificationList.isEmpty()) {
ViewUtil.showShortSnackbar(relativeLayout, R.string.no_notifications); ViewUtil.showShortSnackbar(binding.container, R.string.no_notifications);
/*progressBar.setVisibility(View.GONE); /*progressBar.setVisibility(View.GONE);
recyclerView.setVisibility(View.GONE);*/ recyclerView.setVisibility(View.GONE);*/
relativeLayout.setVisibility(View.GONE); binding.container.setVisibility(View.GONE);
setEmptyView(); setEmptyView();
no_notification.setVisibility(View.VISIBLE); binding.noNotificationBackground.setVisibility(View.VISIBLE);
return; return;
} }
relativeLayout.setVisibility(View.VISIBLE); binding.container.setVisibility(View.VISIBLE);
no_notification.setVisibility(View.GONE); binding.noNotificationBackground.setVisibility(View.GONE);
adapter.setItems(notificationList); adapter.setItems(notificationList);
} }
@ -269,9 +246,9 @@ public class NotificationActivity extends BaseActivity {
private void setEmptyView() { private void setEmptyView() {
if (isRead) { if (isRead) {
noNotificationText.setText(R.string.no_read_notification); binding.noNotificationText.setText(R.string.no_read_notification);
}else { }else {
noNotificationText.setText(R.string.no_notification); binding.noNotificationText.setText(R.string.no_notification);
} }
} }

View file

@ -11,7 +11,9 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
<include layout="@layout/toolbar"/> <include
android:id="@+id/toolbar"
layout="@layout/toolbar"/>
<RelativeLayout <RelativeLayout
android:id="@+id/container" android:id="@+id/container"

View file

@ -1,9 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Authors:
* Winston Sung
-->
<resources>
<string name="crash_dialog_title">同享壞咗</string>
<string name="crash_dialog_text">哎呀。出咗錯!</string>
<string name="crash_dialog_ok_toast">多謝你!</string>
</resources>