chinwag-android/app/src/main/java/com/keylesspalace/tusky/util/SaveTootHelper.java
Konrad Pozniak 940d6d395a
Drafts v2 (#2032)
* cleanup warnings, reorganize some code

* move ComposeAutoCompleteAdapter to compose package

* composeOptions doesn't need to be a class member

* add DraftsActivity and DraftsViewModel

* drafts

* remove unnecessary Unit in ComposeViewModel

* add schema/25.json

* fix db migration

* drafts

* cleanup code

* fix compose activity rotation bug

* fix media descriptions getting lost when restoring a draft

* improve deleting drafts

* fix ComposeActivityTest

* improve draft layout for almost empty drafts

* reformat code

* show toast when opening reply to deleted toot

* improve item_draft layout
2021-01-21 18:57:09 +01:00

57 lines
1.6 KiB
Java

package com.keylesspalace.tusky.util;
import android.content.Context;
import android.net.Uri;
import android.util.Log;
import androidx.annotation.NonNull;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.keylesspalace.tusky.db.AppDatabase;
import com.keylesspalace.tusky.db.TootDao;
import com.keylesspalace.tusky.db.TootEntity;
import java.util.ArrayList;
import javax.inject.Inject;
public final class SaveTootHelper {
private static final String TAG = "SaveTootHelper";
private TootDao tootDao;
private Context context;
private Gson gson = new Gson();
@Inject
public SaveTootHelper(@NonNull AppDatabase appDatabase, @NonNull Context context) {
this.tootDao = appDatabase.tootDao();
this.context = context;
}
public void deleteDraft(int tootId) {
TootEntity item = tootDao.find(tootId);
if (item != null) {
deleteDraft(item);
}
}
public void deleteDraft(@NonNull TootEntity item) {
// Delete any media files associated with the status.
ArrayList<String> uris = gson.fromJson(item.getUrls(),
new TypeToken<ArrayList<String>>() {
}.getType());
if (uris != null) {
for (String uriString : uris) {
Uri uri = Uri.parse(uriString);
if (context.getContentResolver().delete(uri, null, null) == 0) {
Log.e(TAG, String.format("Did not delete file %s.", uriString));
}
}
}
// update DB
tootDao.delete(item.getUid());
}
}