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;
|
|
|
|
|
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;
|
2017-07-12 03:49:46 +02:00
|
|
|
import android.net.Uri;
|
2017-06-29 17:55:39 +02:00
|
|
|
import android.os.AsyncTask;
|
|
|
|
import android.os.Bundle;
|
2017-07-05 16:36:14 +02:00
|
|
|
import android.support.v7.app.ActionBar;
|
2017-06-29 17:55:39 +02:00
|
|
|
import android.support.v7.widget.DividerItemDecoration;
|
|
|
|
import android.support.v7.widget.LinearLayoutManager;
|
|
|
|
import android.support.v7.widget.RecyclerView;
|
|
|
|
import android.support.v7.widget.Toolbar;
|
2017-07-12 03:49:46 +02:00
|
|
|
import android.util.Log;
|
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
|
|
|
|
2017-07-12 03:49:46 +02:00
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.google.gson.reflect.TypeToken;
|
2017-06-29 17:55:39 +02:00
|
|
|
import com.keylesspalace.tusky.adapter.SavedTootAdapter;
|
|
|
|
import com.keylesspalace.tusky.db.TootDao;
|
|
|
|
import com.keylesspalace.tusky.db.TootEntity;
|
|
|
|
import com.keylesspalace.tusky.util.ThemeUtils;
|
|
|
|
|
2017-07-12 03:49:46 +02:00
|
|
|
import java.util.ArrayList;
|
2017-06-29 17:55:39 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
2017-07-05 16:36:14 +02:00
|
|
|
public class SavedTootActivity extends BaseActivity implements SavedTootAdapter.SavedTootAction {
|
2017-10-18 00:20:26 +02:00
|
|
|
private static final String TAG = "SavedTootActivity"; // logging tag
|
2017-06-29 17:55:39 +02:00
|
|
|
|
|
|
|
// dao
|
|
|
|
private static TootDao tootDao = TuskyApplication.getDB().tootDao();
|
|
|
|
|
|
|
|
// ui
|
|
|
|
private SavedTootAdapter adapter;
|
2017-07-05 16:36:14 +02:00
|
|
|
private TextView noContent;
|
2017-06-29 17:55:39 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
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-07-05 16:36:14 +02:00
|
|
|
// req
|
2017-06-29 17:55:39 +02:00
|
|
|
getAllToot();
|
|
|
|
}
|
|
|
|
|
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-10-18 00:20:26 +02:00
|
|
|
private void getAllToot() {
|
2017-06-29 17:55:39 +02:00
|
|
|
new AsyncTask<Void, Void, List<TootEntity>>() {
|
|
|
|
@Override
|
|
|
|
protected List<TootEntity> doInBackground(Void... params) {
|
|
|
|
return tootDao.loadAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(List<TootEntity> tootEntities) {
|
|
|
|
super.onPostExecute(tootEntities);
|
2017-07-05 16:36:14 +02:00
|
|
|
// set ui
|
|
|
|
setNoContent(tootEntities.size());
|
2017-07-07 12:32:04 +02:00
|
|
|
if (adapter != null) {
|
|
|
|
adapter.setItems(tootEntities);
|
|
|
|
adapter.notifyDataSetChanged();
|
|
|
|
}
|
2017-06-29 17:55:39 +02:00
|
|
|
}
|
|
|
|
}.execute();
|
|
|
|
}
|
|
|
|
|
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) {
|
2017-07-12 03:49:46 +02:00
|
|
|
// Delete any media files associated with the status.
|
|
|
|
ArrayList<String> uris = new Gson().fromJson(item.getUrls(),
|
|
|
|
new TypeToken<ArrayList<String>>() {}.getType());
|
2017-07-15 09:10:55 +02:00
|
|
|
if (uris != null) {
|
|
|
|
for (String uriString : uris) {
|
|
|
|
Uri uri = Uri.parse(uriString);
|
|
|
|
if (getContentResolver().delete(uri, null, null) == 0) {
|
|
|
|
Log.e(TAG, String.format("Did not delete file %s.", uriString));
|
|
|
|
}
|
2017-07-12 03:49:46 +02:00
|
|
|
}
|
|
|
|
}
|
2017-07-05 16:36:14 +02:00
|
|
|
// update DB
|
|
|
|
tootDao.delete(item);
|
|
|
|
// update adapter
|
|
|
|
if (adapter != null) {
|
|
|
|
adapter.removeItem(position);
|
|
|
|
setNoContent(adapter.getItemCount());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@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())
|
|
|
|
.build(this);
|
2017-07-05 16:36:14 +02:00
|
|
|
startActivity(intent);
|
|
|
|
}
|
2017-06-29 17:55:39 +02:00
|
|
|
}
|