From 87c1edb3d48fb4a5590eeb6fd537b67ed8ef1c3f Mon Sep 17 00:00:00 2001 From: torrentcome Date: Wed, 5 Jul 2017 16:36:14 +0200 Subject: [PATCH] (medias) try to add medias --- .../keylesspalace/tusky/ComposeActivity.java | 29 +++++++ .../tusky/SavedTootActivity.java | 58 ++++++++++++- .../tusky/adapter/SavedTootAdapter.java | 81 +++++++++---------- .../keylesspalace/tusky/db/AppDatabase.java | 5 +- .../keylesspalace/tusky/db/TootEntity.java | 11 +++ app/src/main/res/layout/item_saved_toot.xml | 2 + 6 files changed, 140 insertions(+), 46 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java b/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java index 04935f58..ef75d068 100644 --- a/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java @@ -77,6 +77,8 @@ import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; import com.keylesspalace.tusky.db.TootDao; import com.keylesspalace.tusky.db.TootEntity; import com.keylesspalace.tusky.entity.Account; @@ -296,6 +298,22 @@ public class ComposeActivity extends BaseActivity implements ComposeOptionsFragm startingContentWarning = intent.getStringExtra("content_warning"); } } + + /* If come from SavedTootActivity + * */ + String savedTootText = intent.getStringExtra("saved_toot_text"); + if (!TextUtils.isEmpty(savedTootText)) { + textEditor.append(savedTootText); + } + + String savedJsonUrls = intent.getStringExtra("saved_json_urls"); + if (!TextUtils.isEmpty(savedJsonUrls)) { + // try to redo a list of media + ArrayList playersList = new Gson().fromJson(savedJsonUrls, + new TypeToken>() { + }.getType()); + + } } /* If the currently logged in account is locked, its posts should default to private. This @@ -516,6 +534,17 @@ public class ComposeActivity extends BaseActivity implements ComposeOptionsFragm } else { final TootEntity toot = new TootEntity(); toot.setText(s); + if (mediaQueued != null && mediaQueued.size() > 0) { + List list = new ArrayList<>(); + for (QueuedMedia q : + mediaQueued) { + Log.d("list", "" + q.uri); + list.add(q.uri.toString()); + } + String json = new Gson().toJson(list); + toot.setUrls(json); + } + new AsyncTask() { @Override protected Void doInBackground(Void... params) { diff --git a/app/src/main/java/com/keylesspalace/tusky/SavedTootActivity.java b/app/src/main/java/com/keylesspalace/tusky/SavedTootActivity.java index f33ef5b3..b7e0f924 100644 --- a/app/src/main/java/com/keylesspalace/tusky/SavedTootActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/SavedTootActivity.java @@ -15,14 +15,19 @@ package com.keylesspalace.tusky; +import android.content.Intent; import android.graphics.drawable.Drawable; import android.os.AsyncTask; import android.os.Bundle; +import android.support.v7.app.ActionBar; import android.support.v7.widget.DividerItemDecoration; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; import android.util.Log; +import android.view.MenuItem; +import android.view.View; +import android.widget.TextView; import com.keylesspalace.tusky.adapter.SavedTootAdapter; import com.keylesspalace.tusky.db.TootDao; @@ -31,13 +36,14 @@ import com.keylesspalace.tusky.util.ThemeUtils; import java.util.List; -public class SavedTootActivity extends BaseActivity { +public class SavedTootActivity extends BaseActivity implements SavedTootAdapter.SavedTootAction { // dao private static TootDao tootDao = TuskyApplication.getDB().tootDao(); // ui private SavedTootAdapter adapter; + private TextView noContent; @Override protected void onCreate(Bundle savedInstanceState) { @@ -46,8 +52,15 @@ public class SavedTootActivity extends BaseActivity { Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); + ActionBar bar = getSupportActionBar(); + if (bar != null) { + bar.setTitle(getString(R.string.title_saved_toot)); + bar.setDisplayHomeAsUpEnabled(true); + bar.setDisplayShowHomeEnabled(true); + } RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view); + noContent = (TextView) findViewById(R.id.no_content); recyclerView.setHasFixedSize(true); LinearLayoutManager layoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(layoutManager); @@ -57,12 +70,24 @@ public class SavedTootActivity extends BaseActivity { R.drawable.status_divider_dark); divider.setDrawable(drawable); recyclerView.addItemDecoration(divider); - adapter = new SavedTootAdapter(); + adapter = new SavedTootAdapter(this); recyclerView.setAdapter(adapter); + // req getAllToot(); } + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: { + onBackPressed(); + return true; + } + } + return super.onOptionsItemSelected(item); + } + public void getAllToot() { new AsyncTask>() { @Override @@ -76,9 +101,38 @@ public class SavedTootActivity extends BaseActivity { for (TootEntity t : tootEntities) { Log.e("toot", "id=" + t.getUid() + "text=" + t.getText()); } + + // set ui + setNoContent(tootEntities.size()); adapter.addItems(tootEntities); } }.execute(); } + 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) { + // update DB + tootDao.delete(item); + // update adapter + if (adapter != null) { + adapter.removeItem(position); + setNoContent(adapter.getItemCount()); + } + } + + @Override + public void click(int position, TootEntity item) { + Intent intent = new Intent(this, ComposeActivity.class); + intent.putExtra("saved_toot_text", item.getText()); + intent.putExtra("saved_json_urls", item.getUrls()); + startActivity(intent); + } } diff --git a/app/src/main/java/com/keylesspalace/tusky/adapter/SavedTootAdapter.java b/app/src/main/java/com/keylesspalace/tusky/adapter/SavedTootAdapter.java index c8005347..3db08bc0 100644 --- a/app/src/main/java/com/keylesspalace/tusky/adapter/SavedTootAdapter.java +++ b/app/src/main/java/com/keylesspalace/tusky/adapter/SavedTootAdapter.java @@ -15,6 +15,7 @@ package com.keylesspalace.tusky.adapter; +import android.content.Context; import android.support.annotation.Nullable; import android.support.v7.widget.RecyclerView; import android.text.TextUtils; @@ -32,10 +33,12 @@ import java.util.List; public class SavedTootAdapter extends RecyclerView.Adapter { private List list; + private SavedTootAction handler; - public SavedTootAdapter() { + public SavedTootAdapter(Context context) { super(); list = new ArrayList<>(); + handler = (SavedTootAction) context; } @Override @@ -48,33 +51,12 @@ public class SavedTootAdapter extends RecyclerView.Adapter { @Override public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) { TootViewHolder holder = (TootViewHolder) viewHolder; - holder.bind(getItem(position)); + holder.bind(position, getItem(position)); } @Override public int getItemCount() { - return list.size() + 1; - } - - public void update(List newToot) { - if (newToot == null || newToot.isEmpty()) { - return; - } - if (list.isEmpty()) { - list = newToot; - } else { - int index = list.indexOf(newToot.get(newToot.size() - 1)); - for (int i = 0; i < index; i++) { - list.remove(0); - } - int newIndex = newToot.indexOf(list.get(0)); - if (newIndex == -1) { - list.addAll(0, newToot); - } else { - list.addAll(0, newToot.subList(0, newIndex)); - } - } - notifyDataSetChanged(); + return list.size(); } public void addItems(List newToot) { @@ -93,14 +75,6 @@ public class SavedTootAdapter extends RecyclerView.Adapter { return toot; } - public void addItem(TootEntity toot, int position) { - if (position < 0 || position > list.size()) { - return; - } - list.add(position, toot); - notifyItemInserted(position); - } - public TootEntity getItem(int position) { if (position >= 0 && position < list.size()) { return list.get(position); @@ -108,6 +82,13 @@ public class SavedTootAdapter extends RecyclerView.Adapter { return null; } + // handler saved toot + public interface SavedTootAction { + void delete(int position, TootEntity item); + + void click(int position, TootEntity item); + } + public static class ViewHolder extends RecyclerView.ViewHolder { TextView mTextView; @@ -117,21 +98,37 @@ public class SavedTootAdapter extends RecyclerView.Adapter { } } - private static class TootViewHolder extends RecyclerView.ViewHolder { - public TextView content; - public ImageButton suppr; + private class TootViewHolder extends RecyclerView.ViewHolder { + View view; + TextView content; + ImageButton suppr; TootViewHolder(View view) { super(view); - content = (TextView) view.findViewById(R.id.content); - suppr = (ImageButton) view.findViewById(R.id.suppr); + this.view = view; + this.content = (TextView) view.findViewById(R.id.content); + this.suppr = (ImageButton) view.findViewById(R.id.suppr); } - public void bind(TootEntity item) { - if (item != null && !TextUtils.isEmpty(item.getText())) - content.setText(item.getText()); - else - content.setText(""); + void bind(final int position, final TootEntity item) { + if (item != null) { + if (!TextUtils.isEmpty(item.getText())) + content.setText(item.getText()); + else + content.setText(""); + suppr.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + handler.delete(position, item); + } + }); + view.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + handler.click(position, item); + } + }); + } } } } diff --git a/app/src/main/java/com/keylesspalace/tusky/db/AppDatabase.java b/app/src/main/java/com/keylesspalace/tusky/db/AppDatabase.java index 00871429..0a4936d2 100644 --- a/app/src/main/java/com/keylesspalace/tusky/db/AppDatabase.java +++ b/app/src/main/java/com/keylesspalace/tusky/db/AppDatabase.java @@ -7,7 +7,8 @@ import android.arch.persistence.room.RoomDatabase; * DB version & declare DAO */ -@Database(entities = {TootEntity.class}, version = 2, exportSchema = false) -abstract public class AppDatabase extends RoomDatabase { +@Database(entities = {TootEntity.class}, version = 3, exportSchema = false) +public abstract class AppDatabase extends RoomDatabase { + public abstract TootDao tootDao(); } diff --git a/app/src/main/java/com/keylesspalace/tusky/db/TootEntity.java b/app/src/main/java/com/keylesspalace/tusky/db/TootEntity.java index 4323c957..760379e8 100644 --- a/app/src/main/java/com/keylesspalace/tusky/db/TootEntity.java +++ b/app/src/main/java/com/keylesspalace/tusky/db/TootEntity.java @@ -16,6 +16,9 @@ public class TootEntity { @ColumnInfo(name = "text") private String text; + @ColumnInfo(name = "urls") + private String urls; + // getter setter public String getText() { return text; @@ -32,4 +35,12 @@ public class TootEntity { public void setUid(int uid) { this.uid = uid; } + + public String getUrls() { + return urls; + } + + public void setUrls(String urls) { + this.urls = urls; + } } diff --git a/app/src/main/res/layout/item_saved_toot.xml b/app/src/main/res/layout/item_saved_toot.xml index 089a993f..cf76d824 100644 --- a/app/src/main/res/layout/item_saved_toot.xml +++ b/app/src/main/res/layout/item_saved_toot.xml @@ -18,7 +18,9 @@ android:layout_width="24dp" android:layout_height="24dp" android:layout_gravity="center_vertical" + android:padding="16dp" android:layout_margin="16dp" + android:contentDescription="@string/action_unmute" app:srcCompat="@drawable/ic_clear_24dp" /> \ No newline at end of file