2017-06-29 17:55:39 +02:00
|
|
|
/* Copyright 2017 Andrew Dawson
|
|
|
|
*
|
|
|
|
* This file is a part of Tusky.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
|
|
|
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with Tusky; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
|
|
|
|
package com.keylesspalace.tusky;
|
|
|
|
|
2018-12-17 15:25:35 +01:00
|
|
|
import androidx.lifecycle.Lifecycle;
|
2017-07-05 16:36:14 +02:00
|
|
|
import android.content.Intent;
|
2017-06-29 17:55:39 +02:00
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
import android.os.AsyncTask;
|
|
|
|
import android.os.Bundle;
|
2018-12-17 15:25:35 +01:00
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
import androidx.appcompat.app.ActionBar;
|
|
|
|
import androidx.recyclerview.widget.DividerItemDecoration;
|
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
import androidx.appcompat.widget.Toolbar;
|
2017-07-05 16:36:14 +02:00
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.TextView;
|
2017-06-29 17:55:39 +02:00
|
|
|
|
|
|
|
import com.keylesspalace.tusky.adapter.SavedTootAdapter;
|
2018-05-27 10:22:12 +02:00
|
|
|
import com.keylesspalace.tusky.appstore.EventHub;
|
|
|
|
import com.keylesspalace.tusky.appstore.StatusComposedEvent;
|
|
|
|
import com.keylesspalace.tusky.db.AppDatabase;
|
2017-06-29 17:55:39 +02:00
|
|
|
import com.keylesspalace.tusky.db.TootDao;
|
|
|
|
import com.keylesspalace.tusky.db.TootEntity;
|
2018-05-27 10:22:12 +02:00
|
|
|
import com.keylesspalace.tusky.di.Injectable;
|
2018-04-13 22:37:21 +02:00
|
|
|
import com.keylesspalace.tusky.util.SaveTootHelper;
|
2017-06-29 17:55:39 +02:00
|
|
|
import com.keylesspalace.tusky.util.ThemeUtils;
|
|
|
|
|
2017-11-16 19:18:11 +01:00
|
|
|
import java.lang.ref.WeakReference;
|
2017-07-12 03:49:46 +02:00
|
|
|
import java.util.ArrayList;
|
2017-06-29 17:55:39 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
2018-05-27 10:22:12 +02:00
|
|
|
import javax.inject.Inject;
|
2017-06-29 17:55:39 +02:00
|
|
|
|
2018-05-27 10:22:12 +02:00
|
|
|
import io.reactivex.android.schedulers.AndroidSchedulers;
|
|
|
|
|
|
|
|
import static com.uber.autodispose.AutoDispose.autoDisposable;
|
|
|
|
import static com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider.from;
|
|
|
|
|
|
|
|
public final class SavedTootActivity extends BaseActivity implements SavedTootAdapter.SavedTootAction,
|
|
|
|
Injectable {
|
2017-06-29 17:55:39 +02:00
|
|
|
|
2018-04-13 22:37:21 +02:00
|
|
|
private SaveTootHelper saveTootHelper;
|
|
|
|
|
2017-06-29 17:55:39 +02:00
|
|
|
// ui
|
|
|
|
private SavedTootAdapter adapter;
|
2017-07-05 16:36:14 +02:00
|
|
|
private TextView noContent;
|
2017-06-29 17:55:39 +02:00
|
|
|
|
2017-11-16 19:18:11 +01:00
|
|
|
private List<TootEntity> toots = new ArrayList<>();
|
2018-05-27 10:22:12 +02:00
|
|
|
@Nullable
|
|
|
|
private AsyncTask<?, ?, ?> asyncTask;
|
2017-11-16 19:18:11 +01:00
|
|
|
|
2018-05-27 10:22:12 +02:00
|
|
|
@Inject
|
|
|
|
EventHub eventHub;
|
|
|
|
@Inject
|
|
|
|
AppDatabase database;
|
2018-04-13 22:37:21 +02:00
|
|
|
|
2017-06-29 17:55:39 +02:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
2018-04-13 22:37:21 +02:00
|
|
|
|
2018-05-27 10:22:12 +02:00
|
|
|
saveTootHelper = new SaveTootHelper(database.tootDao(), this);
|
2018-04-13 22:37:21 +02:00
|
|
|
|
2018-05-27 10:22:12 +02:00
|
|
|
eventHub.getEvents()
|
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
|
|
.ofType(StatusComposedEvent.class)
|
|
|
|
.as(autoDisposable(from(this, Lifecycle.Event.ON_DESTROY)))
|
|
|
|
.subscribe((__) -> this.fetchToots());
|
2018-04-13 22:37:21 +02:00
|
|
|
|
2017-06-29 17:55:39 +02:00
|
|
|
setContentView(R.layout.activity_saved_toot);
|
|
|
|
|
2017-10-18 00:20:26 +02:00
|
|
|
Toolbar toolbar = findViewById(R.id.toolbar);
|
2017-06-29 17:55:39 +02:00
|
|
|
setSupportActionBar(toolbar);
|
2017-07-05 16:36:14 +02:00
|
|
|
ActionBar bar = getSupportActionBar();
|
|
|
|
if (bar != null) {
|
|
|
|
bar.setTitle(getString(R.string.title_saved_toot));
|
|
|
|
bar.setDisplayHomeAsUpEnabled(true);
|
|
|
|
bar.setDisplayShowHomeEnabled(true);
|
|
|
|
}
|
2017-06-29 17:55:39 +02:00
|
|
|
|
2017-10-18 00:20:26 +02:00
|
|
|
RecyclerView recyclerView = findViewById(R.id.recycler_view);
|
|
|
|
noContent = findViewById(R.id.no_content);
|
2017-06-29 17:55:39 +02:00
|
|
|
recyclerView.setHasFixedSize(true);
|
|
|
|
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
|
|
|
|
recyclerView.setLayoutManager(layoutManager);
|
|
|
|
DividerItemDecoration divider = new DividerItemDecoration(
|
|
|
|
this, layoutManager.getOrientation());
|
|
|
|
Drawable drawable = ThemeUtils.getDrawable(this, R.attr.status_divider_drawable,
|
|
|
|
R.drawable.status_divider_dark);
|
|
|
|
divider.setDrawable(drawable);
|
|
|
|
recyclerView.addItemDecoration(divider);
|
2017-07-05 16:36:14 +02:00
|
|
|
adapter = new SavedTootAdapter(this);
|
2017-06-29 17:55:39 +02:00
|
|
|
recyclerView.setAdapter(adapter);
|
2017-07-07 12:32:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onResume() {
|
|
|
|
super.onResume();
|
2017-11-16 19:18:11 +01:00
|
|
|
fetchToots();
|
|
|
|
}
|
2017-07-07 12:32:04 +02:00
|
|
|
|
2017-11-16 19:18:11 +01:00
|
|
|
@Override
|
|
|
|
protected void onPause() {
|
|
|
|
super.onPause();
|
|
|
|
if (asyncTask != null) asyncTask.cancel(true);
|
2017-06-29 17:55:39 +02:00
|
|
|
}
|
|
|
|
|
2017-07-05 16:36:14 +02:00
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case android.R.id.home: {
|
|
|
|
onBackPressed();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
|
2017-11-16 19:18:11 +01:00
|
|
|
private void fetchToots() {
|
2018-05-27 10:22:12 +02:00
|
|
|
asyncTask = new FetchPojosTask(this, database.tootDao())
|
2017-11-16 19:18:11 +01:00
|
|
|
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
2017-06-29 17:55:39 +02:00
|
|
|
}
|
|
|
|
|
2017-07-05 16:36:14 +02:00
|
|
|
private void setNoContent(int size) {
|
|
|
|
if (size == 0) {
|
|
|
|
noContent.setVisibility(View.VISIBLE);
|
|
|
|
} else {
|
|
|
|
noContent.setVisibility(View.INVISIBLE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void delete(int position, TootEntity item) {
|
2018-04-13 22:37:21 +02:00
|
|
|
|
|
|
|
saveTootHelper.deleteDraft(item);
|
|
|
|
|
2017-11-16 19:18:11 +01:00
|
|
|
toots.remove(position);
|
2017-07-05 16:36:14 +02:00
|
|
|
// update adapter
|
|
|
|
if (adapter != null) {
|
|
|
|
adapter.removeItem(position);
|
2017-11-16 19:18:11 +01:00
|
|
|
setNoContent(toots.size());
|
2017-07-05 16:36:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void click(int position, TootEntity item) {
|
2017-11-01 20:59:29 +01:00
|
|
|
Intent intent = new ComposeActivity.IntentBuilder()
|
|
|
|
.savedTootUid(item.getUid())
|
|
|
|
.savedTootText(item.getText())
|
|
|
|
.contentWarning(item.getContentWarning())
|
|
|
|
.savedJsonUrls(item.getUrls())
|
2018-11-11 19:25:45 +01:00
|
|
|
.savedJsonDescriptions(item.getDescriptions())
|
2017-11-16 19:18:11 +01:00
|
|
|
.inReplyToId(item.getInReplyToId())
|
|
|
|
.repyingStatusAuthor(item.getInReplyToUsername())
|
|
|
|
.replyingStatusContent(item.getInReplyToText())
|
2018-08-23 20:50:50 +02:00
|
|
|
.savedVisibility(item.getVisibility())
|
2017-11-01 20:59:29 +01:00
|
|
|
.build(this);
|
2017-07-05 16:36:14 +02:00
|
|
|
startActivity(intent);
|
|
|
|
}
|
2017-11-16 19:18:11 +01:00
|
|
|
|
|
|
|
static final class FetchPojosTask extends AsyncTask<Void, Void, List<TootEntity>> {
|
|
|
|
|
|
|
|
private final WeakReference<SavedTootActivity> activityRef;
|
2018-05-27 10:22:12 +02:00
|
|
|
private final TootDao tootDao;
|
2017-11-16 19:18:11 +01:00
|
|
|
|
2018-05-27 10:22:12 +02:00
|
|
|
FetchPojosTask(SavedTootActivity activity, TootDao tootDao) {
|
2017-11-16 19:18:11 +01:00
|
|
|
this.activityRef = new WeakReference<>(activity);
|
2018-05-27 10:22:12 +02:00
|
|
|
this.tootDao = tootDao;
|
2017-11-16 19:18:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected List<TootEntity> doInBackground(Void... voids) {
|
|
|
|
return tootDao.loadAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(List<TootEntity> pojos) {
|
|
|
|
super.onPostExecute(pojos);
|
|
|
|
SavedTootActivity activity = activityRef.get();
|
|
|
|
if (activity == null) return;
|
|
|
|
|
2018-05-27 10:22:12 +02:00
|
|
|
activity.toots.clear();
|
2017-11-16 19:18:11 +01:00
|
|
|
activity.toots.addAll(pojos);
|
|
|
|
|
|
|
|
// set ui
|
|
|
|
activity.setNoContent(pojos.size());
|
2018-05-27 10:22:12 +02:00
|
|
|
activity.adapter.setItems(activity.toots);
|
2017-11-16 19:18:11 +01:00
|
|
|
activity.adapter.notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
}
|
2017-06-29 17:55:39 +02:00
|
|
|
}
|