Skip to content

Commit 21fdd39

Browse files
authored
Refactor : NotificationActivity uses ViewBinding. (commons-app#5606)
1 parent c419402 commit 21fdd39

File tree

3 files changed

+36
-66
lines changed

3 files changed

+36
-66
lines changed

app/src/main/java/fr/free/nrw/commons/notification/NotificationActivity.java

Lines changed: 33 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,12 @@
99
import android.view.MenuInflater;
1010
import android.view.MenuItem;
1111
import android.view.View;
12-
import android.widget.ProgressBar;
13-
import android.widget.RelativeLayout;
14-
import android.widget.TextView;
15-
import android.widget.Toast;
16-
import androidx.appcompat.widget.Toolbar;
17-
import androidx.constraintlayout.widget.ConstraintLayout;
1812
import androidx.recyclerview.widget.DividerItemDecoration;
1913
import androidx.recyclerview.widget.LinearLayoutManager;
20-
import androidx.recyclerview.widget.RecyclerView;
21-
import butterknife.BindView;
22-
import butterknife.ButterKnife;
2314
import com.google.android.material.snackbar.Snackbar;
2415
import fr.free.nrw.commons.R;
2516
import fr.free.nrw.commons.Utils;
17+
import fr.free.nrw.commons.databinding.ActivityNotificationBinding;
2618
import fr.free.nrw.commons.notification.models.Notification;
2719
import fr.free.nrw.commons.theme.BaseActivity;
2820
import fr.free.nrw.commons.utils.NetworkUtils;
@@ -44,19 +36,7 @@
4436
*/
4537

4638
public class NotificationActivity extends BaseActivity {
47-
@BindView(R.id.listView)
48-
RecyclerView recyclerView;
49-
@BindView(R.id.progressBar)
50-
ProgressBar progressBar;
51-
@BindView(R.id.container)
52-
RelativeLayout relativeLayout;
53-
@BindView(R.id.no_notification_background)
54-
ConstraintLayout no_notification;
55-
@BindView(R.id.no_notification_text)
56-
TextView noNotificationText;
57-
@BindView(R.id.toolbar)
58-
Toolbar toolbar;
59-
39+
private ActivityNotificationBinding binding;
6040

6141
@Inject
6242
NotificationController controller;
@@ -75,13 +55,13 @@ public class NotificationActivity extends BaseActivity {
7555
protected void onCreate(Bundle savedInstanceState) {
7656
super.onCreate(savedInstanceState);
7757
isRead = getIntent().getStringExtra("title").equals("read");
78-
setContentView(R.layout.activity_notification);
79-
ButterKnife.bind(this);
58+
binding = ActivityNotificationBinding.inflate(getLayoutInflater());
59+
setContentView(binding.getRoot());
8060
mNotificationWorkerFragment = (NotificationWorkerFragment) getFragmentManager()
8161
.findFragmentByTag(TAG_NOTIFICATION_WORKER_FRAGMENT);
8262
initListView();
8363
setPageTitle();
84-
setSupportActionBar(toolbar);
64+
setSupportActionBar(binding.toolbar.toolbar);
8565
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
8666
}
8767

@@ -115,36 +95,33 @@ public void removeNotification(Notification notification) {
11595
notificationList.remove(notification);
11696
setItems(notificationList);
11797
adapter.notifyDataSetChanged();
118-
Snackbar snackbar = Snackbar
119-
.make(relativeLayout, getString(R.string.notification_mark_read), Snackbar.LENGTH_LONG);
120-
121-
snackbar.show();
98+
ViewUtil.showLongSnackbar(binding.container,getString(R.string.notification_mark_read));
12299
if (notificationList.size() == 0) {
123100
setEmptyView();
124-
relativeLayout.setVisibility(View.GONE);
125-
no_notification.setVisibility(View.VISIBLE);
101+
binding.container.setVisibility(View.GONE);
102+
binding.noNotificationBackground.setVisibility(View.VISIBLE);
126103
}
127104
} else {
128105
adapter.notifyDataSetChanged();
129106
setItems(notificationList);
130-
Toast.makeText(NotificationActivity.this, getString(R.string.some_error), Toast.LENGTH_SHORT).show();
107+
ViewUtil.showLongToast(this,getString(R.string.some_error));
131108
}
132109
}, throwable -> {
133110

134111
Timber.e(throwable, "Error occurred while loading notifications");
135112
throwable.printStackTrace();
136-
ViewUtil.showShortSnackbar(relativeLayout, R.string.error_notifications);
137-
progressBar.setVisibility(View.GONE);
113+
ViewUtil.showShortSnackbar(binding.container, R.string.error_notifications);
114+
binding.progressBar.setVisibility(View.GONE);
138115
});
139116
compositeDisposable.add(disposable);
140117
}
141118

142119

143120

144121
private void initListView() {
145-
recyclerView.setLayoutManager(new LinearLayoutManager(this));
146-
DividerItemDecoration itemDecor = new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL);
147-
recyclerView.addItemDecoration(itemDecor);
122+
binding.listView.setLayoutManager(new LinearLayoutManager(this));
123+
DividerItemDecoration itemDecor = new DividerItemDecoration(binding.listView.getContext(), DividerItemDecoration.VERTICAL);
124+
binding.listView.addItemDecoration(itemDecor);
148125
if (isRead) {
149126
refresh(true);
150127
} else {
@@ -156,27 +133,27 @@ private void initListView() {
156133
removeNotification(item);
157134
return Unit.INSTANCE;
158135
});
159-
recyclerView.setAdapter(this.adapter);
136+
binding.listView.setAdapter(adapter);
160137
}
161138

162139
private void refresh(boolean archived) {
163140
if (!NetworkUtils.isInternetConnectionEstablished(this)) {
164-
progressBar.setVisibility(View.GONE);
165-
Snackbar.make(relativeLayout, R.string.no_internet, Snackbar.LENGTH_INDEFINITE)
141+
binding.progressBar.setVisibility(View.GONE);
142+
Snackbar.make(binding.container, R.string.no_internet, Snackbar.LENGTH_INDEFINITE)
166143
.setAction(R.string.retry, view -> refresh(archived)).show();
167144
} else {
168145
addNotifications(archived);
169146
}
170-
progressBar.setVisibility(View.VISIBLE);
171-
no_notification.setVisibility(View.GONE);
172-
relativeLayout.setVisibility(View.VISIBLE);
147+
binding.progressBar.setVisibility(View.VISIBLE);
148+
binding.noNotificationBackground.setVisibility(View.GONE);
149+
binding.container.setVisibility(View.VISIBLE);
173150
}
174151

175152
@SuppressLint("CheckResult")
176153
private void addNotifications(boolean archived) {
177154
Timber.d("Add notifications");
178155
if (mNotificationWorkerFragment == null) {
179-
progressBar.setVisibility(View.VISIBLE);
156+
binding.progressBar.setVisibility(View.VISIBLE);
180157
compositeDisposable.add(controller.getNotifications(archived)
181158
.subscribeOn(Schedulers.io())
182159
.observeOn(AndroidSchedulers.mainThread())
@@ -186,16 +163,16 @@ private void addNotifications(boolean archived) {
186163
this.notificationList = notificationList;
187164
if (notificationList.size()==0){
188165
setEmptyView();
189-
relativeLayout.setVisibility(View.GONE);
190-
no_notification.setVisibility(View.VISIBLE);
166+
binding.container.setVisibility(View.GONE);
167+
binding.noNotificationBackground.setVisibility(View.VISIBLE);
191168
} else {
192169
setItems(notificationList);
193170
}
194-
progressBar.setVisibility(View.GONE);
171+
binding.progressBar.setVisibility(View.GONE);
195172
}, throwable -> {
196173
Timber.e(throwable, "Error occurred while loading notifications");
197-
ViewUtil.showShortSnackbar(relativeLayout, R.string.error_notifications);
198-
progressBar.setVisibility(View.GONE);
174+
ViewUtil.showShortSnackbar(binding.container, R.string.error_notifications);
175+
binding.progressBar.setVisibility(View.GONE);
199176
}));
200177
} else {
201178
notificationList = mNotificationWorkerFragment.getNotificationList();
@@ -237,16 +214,16 @@ private void handleUrl(String url) {
237214

238215
private void setItems(List<Notification> notificationList) {
239216
if (notificationList == null || notificationList.isEmpty()) {
240-
ViewUtil.showShortSnackbar(relativeLayout, R.string.no_notifications);
217+
ViewUtil.showShortSnackbar(binding.container, R.string.no_notifications);
241218
/*progressBar.setVisibility(View.GONE);
242219
recyclerView.setVisibility(View.GONE);*/
243-
relativeLayout.setVisibility(View.GONE);
220+
binding.container.setVisibility(View.GONE);
244221
setEmptyView();
245-
no_notification.setVisibility(View.VISIBLE);
222+
binding.noNotificationBackground.setVisibility(View.VISIBLE);
246223
return;
247224
}
248-
relativeLayout.setVisibility(View.VISIBLE);
249-
no_notification.setVisibility(View.GONE);
225+
binding.container.setVisibility(View.VISIBLE);
226+
binding.noNotificationBackground.setVisibility(View.GONE);
250227
adapter.setItems(notificationList);
251228
}
252229

@@ -269,9 +246,9 @@ private void setPageTitle() {
269246

270247
private void setEmptyView() {
271248
if (isRead) {
272-
noNotificationText.setText(R.string.no_read_notification);
249+
binding.noNotificationText.setText(R.string.no_read_notification);
273250
}else {
274-
noNotificationText.setText(R.string.no_notification);
251+
binding.noNotificationText.setText(R.string.no_notification);
275252
}
276253
}
277254

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
android:layout_width="match_parent"
1212
android:layout_height="match_parent">
1313

14-
<include layout="@layout/toolbar"/>
14+
<include
15+
android:id="@+id/toolbar"
16+
layout="@layout/toolbar"/>
1517

1618
<RelativeLayout
1719
android:id="@+id/container"

app/src/main/res/values-yue-hant/error.xml

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

0 commit comments

Comments
 (0)