From 946f58439c3a27b7540f36461e5fe5953389e3e9 Mon Sep 17 00:00:00 2001 From: torrentcome Date: Thu, 29 Jun 2017 17:55:39 +0200 Subject: [PATCH] (saved_toot) add part of saved toot activity --- app/src/main/AndroidManifest.xml | 4 + .../tusky/SavedTootActivity.java | 84 +++++++++++ .../tusky/adapter/SavedTootAdapter.java | 137 ++++++++++++++++++ .../main/res/layout/activity_saved_toot.xml | 28 ++++ app/src/main/res/layout/item_saved_toot.xml | 24 +++ app/src/main/res/values/strings.xml | 4 + 6 files changed, 281 insertions(+) create mode 100644 app/src/main/java/com/keylesspalace/tusky/SavedTootActivity.java create mode 100644 app/src/main/java/com/keylesspalace/tusky/adapter/SavedTootAdapter.java create mode 100644 app/src/main/res/layout/activity_saved_toot.xml create mode 100644 app/src/main/res/layout/item_saved_toot.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 1bb3c925d..bafc4abd6 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -26,6 +26,10 @@ + + diff --git a/app/src/main/java/com/keylesspalace/tusky/SavedTootActivity.java b/app/src/main/java/com/keylesspalace/tusky/SavedTootActivity.java new file mode 100644 index 000000000..f33ef5b37 --- /dev/null +++ b/app/src/main/java/com/keylesspalace/tusky/SavedTootActivity.java @@ -0,0 +1,84 @@ +/* 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 . */ + +package com.keylesspalace.tusky; + +import android.graphics.drawable.Drawable; +import android.os.AsyncTask; +import android.os.Bundle; +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 com.keylesspalace.tusky.adapter.SavedTootAdapter; +import com.keylesspalace.tusky.db.TootDao; +import com.keylesspalace.tusky.db.TootEntity; +import com.keylesspalace.tusky.util.ThemeUtils; + +import java.util.List; + +public class SavedTootActivity extends BaseActivity { + + // dao + private static TootDao tootDao = TuskyApplication.getDB().tootDao(); + + // ui + private SavedTootAdapter adapter; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_saved_toot); + + Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); + setSupportActionBar(toolbar); + + RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view); + 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); + adapter = new SavedTootAdapter(); + recyclerView.setAdapter(adapter); + + getAllToot(); + } + + public void getAllToot() { + new AsyncTask>() { + @Override + protected List doInBackground(Void... params) { + return tootDao.loadAll(); + } + + @Override + protected void onPostExecute(List tootEntities) { + super.onPostExecute(tootEntities); + for (TootEntity t : tootEntities) { + Log.e("toot", "id=" + t.getUid() + "text=" + t.getText()); + } + adapter.addItems(tootEntities); + } + }.execute(); + } + +} diff --git a/app/src/main/java/com/keylesspalace/tusky/adapter/SavedTootAdapter.java b/app/src/main/java/com/keylesspalace/tusky/adapter/SavedTootAdapter.java new file mode 100644 index 000000000..c8005347b --- /dev/null +++ b/app/src/main/java/com/keylesspalace/tusky/adapter/SavedTootAdapter.java @@ -0,0 +1,137 @@ +/* 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 . */ + +package com.keylesspalace.tusky.adapter; + +import android.support.annotation.Nullable; +import android.support.v7.widget.RecyclerView; +import android.text.TextUtils; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageButton; +import android.widget.TextView; + +import com.keylesspalace.tusky.R; +import com.keylesspalace.tusky.db.TootEntity; + +import java.util.ArrayList; +import java.util.List; + +public class SavedTootAdapter extends RecyclerView.Adapter { + private List list; + + public SavedTootAdapter() { + super(); + list = new ArrayList<>(); + } + + @Override + public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + View view = LayoutInflater.from(parent.getContext()) + .inflate(R.layout.item_saved_toot, parent, false); + return new TootViewHolder(view); + } + + @Override + public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) { + TootViewHolder holder = (TootViewHolder) viewHolder; + holder.bind(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(); + } + + public void addItems(List newToot) { + int end = list.size(); + list.addAll(newToot); + notifyItemRangeInserted(end, newToot.size()); + } + + @Nullable + public TootEntity removeItem(int position) { + if (position < 0 || position >= list.size()) { + return null; + } + TootEntity toot = list.remove(position); + notifyItemRemoved(position); + 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); + } + return null; + } + + public static class ViewHolder extends RecyclerView.ViewHolder { + TextView mTextView; + + public ViewHolder(TextView v) { + super(v); + mTextView = v; + } + } + + private static class TootViewHolder extends RecyclerView.ViewHolder { + public TextView content; + public ImageButton suppr; + + TootViewHolder(View view) { + super(view); + content = (TextView) view.findViewById(R.id.content); + 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(""); + } + } +} diff --git a/app/src/main/res/layout/activity_saved_toot.xml b/app/src/main/res/layout/activity_saved_toot.xml new file mode 100644 index 000000000..a4575d175 --- /dev/null +++ b/app/src/main/res/layout/activity_saved_toot.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/item_saved_toot.xml b/app/src/main/res/layout/item_saved_toot.xml new file mode 100644 index 000000000..089a993f4 --- /dev/null +++ b/app/src/main/res/layout/item_saved_toot.xml @@ -0,0 +1,24 @@ + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d77e6152b..b1bc44b74 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -101,6 +101,7 @@ Accept Reject Search + Saved Toot Share toot URL to… Share toot to… @@ -194,4 +195,7 @@ Video Follow requested + + %1$s %2$s +