commit
b322bcfe54
@ -0,0 +1,143 @@ |
||||
/* 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; |
||||
|
||||
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.view.MenuItem; |
||||
import android.view.View; |
||||
import android.widget.TextView; |
||||
|
||||
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 implements SavedTootAdapter.SavedTootAction { |
||||
|
||||
// dao
|
||||
private static TootDao tootDao = TuskyApplication.getDB().tootDao(); |
||||
|
||||
// ui
|
||||
private SavedTootAdapter adapter; |
||||
private TextView noContent; |
||||
|
||||
@Override |
||||
protected void onCreate(Bundle savedInstanceState) { |
||||
super.onCreate(savedInstanceState); |
||||
setContentView(R.layout.activity_saved_toot); |
||||
|
||||
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); |
||||
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(this); |
||||
recyclerView.setAdapter(adapter); |
||||
|
||||
} |
||||
|
||||
@Override |
||||
protected void onResume() { |
||||
super.onResume(); |
||||
|
||||
// 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<Void, Void, List<TootEntity>>() { |
||||
@Override |
||||
protected List<TootEntity> doInBackground(Void... params) { |
||||
return tootDao.loadAll(); |
||||
} |
||||
|
||||
@Override |
||||
protected void onPostExecute(List<TootEntity> tootEntities) { |
||||
super.onPostExecute(tootEntities); |
||||
// set ui
|
||||
setNoContent(tootEntities.size()); |
||||
if (adapter != null) { |
||||
adapter.setItems(tootEntities); |
||||
adapter.notifyDataSetChanged(); |
||||
} |
||||
} |
||||
}.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_uid", item.getUid()); |
||||
intent.putExtra("saved_toot_text", item.getText()); |
||||
intent.putExtra("saved_json_urls", item.getUrls()); |
||||
startActivity(intent); |
||||
} |
||||
} |
@ -0,0 +1,139 @@ |
||||
/* 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.adapter; |
||||
|
||||
import android.content.Context; |
||||
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<TootEntity> list; |
||||
private SavedTootAction handler; |
||||
|
||||
public SavedTootAdapter(Context context) { |
||||
super(); |
||||
list = new ArrayList<>(); |
||||
handler = (SavedTootAction) context; |
||||
} |
||||
|
||||
@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(position, getItem(position)); |
||||
} |
||||
|
||||
@Override |
||||
public int getItemCount() { |
||||
return list.size(); |
||||
} |
||||
|
||||
public void setItems(List<TootEntity> newToot) { |
||||
list = new ArrayList<>(); |
||||
list.addAll(newToot); |
||||
} |
||||
|
||||
public void addItems(List<TootEntity> 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 TootEntity getItem(int position) { |
||||
if (position >= 0 && position < list.size()) { |
||||
return list.get(position); |
||||
} |
||||
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; |
||||
|
||||
public ViewHolder(TextView v) { |
||||
super(v); |
||||
mTextView = v; |
||||
} |
||||
} |
||||
|
||||
private class TootViewHolder extends RecyclerView.ViewHolder { |
||||
View view; |
||||
TextView content; |
||||
ImageButton suppr; |
||||
|
||||
TootViewHolder(View view) { |
||||
super(view); |
||||
this.view = view; |
||||
this.content = (TextView) view.findViewById(R.id.content); |
||||
this.suppr = (ImageButton) view.findViewById(R.id.suppr); |
||||
} |
||||
|
||||
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); |
||||
} |
||||
}); |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,14 @@ |
||||
package com.keylesspalace.tusky.db; |
||||
|
||||
import android.arch.persistence.room.Database; |
||||
import android.arch.persistence.room.RoomDatabase; |
||||
|
||||
/** |
||||
* DB version & declare DAO |
||||
*/ |
||||
|
||||
@Database(entities = {TootEntity.class}, version = 1, exportSchema = false) |
||||
public abstract class AppDatabase extends RoomDatabase { |
||||
|
||||
public abstract TootDao tootDao(); |
||||
} |
@ -0,0 +1,47 @@ |
||||
package com.keylesspalace.tusky.db; |
||||
|
||||
import android.arch.persistence.room.ColumnInfo; |
||||
import android.arch.persistence.room.Entity; |
||||
import android.arch.persistence.room.ForeignKey; |
||||
import android.arch.persistence.room.PrimaryKey; |
||||
|
||||
/** |
||||
* Media model |
||||
*/ |
||||
|
||||
@Entity(foreignKeys = @ForeignKey(entity = TootEntity.class, |
||||
parentColumns = "uid", |
||||
childColumns = "toot_id")) |
||||
public class MediaEntity { |
||||
@ColumnInfo(name = "toot_id") |
||||
private int toot_id; |
||||
@PrimaryKey(autoGenerate = true) |
||||
private int uid; |
||||
@ColumnInfo(name = "url") |
||||
private String url; |
||||
|
||||
// getter setter
|
||||
public int getToot_id() { |
||||
return toot_id; |
||||
} |
||||
|
||||
public void setToot_id(int toot_id) { |
||||
this.toot_id = toot_id; |
||||
} |
||||
|
||||
public int getUid() { |
||||
return uid; |
||||
} |
||||
|
||||
public void setUid(int uid) { |
||||
this.uid = uid; |
||||
} |
||||
|
||||
public String getUrl() { |
||||
return url; |
||||
} |
||||
|
||||
public void setUrl(String url) { |
||||
this.url = url; |
||||
} |
||||
} |
@ -0,0 +1,34 @@ |
||||
package com.keylesspalace.tusky.db; |
||||
|
||||
import android.arch.persistence.room.Dao; |
||||
import android.arch.persistence.room.Delete; |
||||
import android.arch.persistence.room.Insert; |
||||
import android.arch.persistence.room.OnConflictStrategy; |
||||
import android.arch.persistence.room.Query; |
||||
import android.arch.persistence.room.Update; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* Created by cto3543 on 28/06/2017. |
||||
* crud interface on this Toot DB |
||||
*/ |
||||
|
||||
@Dao |
||||
public interface TootDao { |
||||
// c
|
||||
@Insert |
||||
long insert(TootEntity users); |
||||
|
||||
// r
|
||||
@Query("SELECT * FROM TootEntity") |
||||
List<TootEntity> loadAll(); |
||||
|
||||
// u
|
||||
@Update |
||||
void updateToot(TootEntity toot); |
||||
|
||||
// d
|
||||
@Delete |
||||
int delete(TootEntity user); |
||||
} |
@ -0,0 +1,46 @@ |
||||
package com.keylesspalace.tusky.db; |
||||
|
||||
import android.arch.persistence.room.ColumnInfo; |
||||
import android.arch.persistence.room.Entity; |
||||
import android.arch.persistence.room.PrimaryKey; |
||||
|
||||
/** |
||||
* toot model |
||||
*/ |
||||
|
||||
@Entity |
||||
public class TootEntity { |
||||
@PrimaryKey(autoGenerate = true) |
||||
private int uid; |
||||
|
||||
@ColumnInfo(name = "text") |
||||
private String text; |
||||
|
||||
@ColumnInfo(name = "urls") |
||||
private String urls; |
||||
|
||||
// getter setter
|
||||
public String getText() { |
||||
return text; |
||||
} |
||||
|
||||
public void setText(String text) { |
||||
this.text = text; |
||||
} |
||||
|
||||
public int getUid() { |
||||
return uid; |
||||
} |
||||
|
||||
public void setUid(int uid) { |
||||
this.uid = uid; |
||||
} |
||||
|
||||
public String getUrls() { |
||||
return urls; |
||||
} |
||||
|
||||
public void setUrls(String urls) { |
||||
this.urls = urls; |
||||
} |
||||
} |
@ -0,0 +1,38 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
xmlns:tools="http://schemas.android.com/tools" |
||||
android:id="@+id/activity_view_thread" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" |
||||
tools:context="com.keylesspalace.tusky.AccountListActivity"> |
||||
|
||||
<RelativeLayout |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" |
||||
android:orientation="vertical"> |
||||
|
||||
<android.support.v7.widget.Toolbar |
||||
android:id="@+id/toolbar" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="?attr/actionBarSize" |
||||
android:background="?attr/toolbar_background_color" |
||||
android:elevation="4dp" /> |
||||
|
||||
<TextView |
||||
android:id="@+id/no_content" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:layout_centerInParent="true" |
||||
android:text="@string/no_content" |
||||
android:visibility="invisible" /> |
||||
|
||||
<android.support.v7.widget.RecyclerView |
||||
android:id="@+id/recycler_view" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" |
||||
android:layout_below="@+id/toolbar" /> |
||||
|
||||
|
||||
</RelativeLayout> |
||||
|
||||
</android.support.design.widget.CoordinatorLayout> |
@ -0,0 +1,26 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
xmlns:app="http://schemas.android.com/apk/res-auto" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:orientation="horizontal"> |
||||
|
||||
<TextView |
||||
android:id="@+id/content" |
||||
android:layout_width="0dp" |
||||
android:layout_height="wrap_content" |
||||
android:layout_weight="0.91" |
||||
android:padding="8dp" /> |
||||
|
||||
<ImageButton |
||||
android:id="@+id/suppr" |
||||
style="?attr/image_button_style" |
||||
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" /> |
||||
</LinearLayout> |
Loading…
Reference in new issue