package net.somethingdreadful.MAL.tasks;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import com.crashlytics.android.Crashlytics;

import net.somethingdreadful.MAL.ContentManager;
import net.somethingdreadful.MAL.account.AccountService;
import net.somethingdreadful.MAL.api.APIHelper;
import net.somethingdreadful.MAL.api.BaseModels.AnimeManga.Anime;
import net.somethingdreadful.MAL.api.BaseModels.AnimeManga.GenericRecord;
import net.somethingdreadful.MAL.api.BaseModels.AnimeManga.Manga;
import net.somethingdreadful.MAL.api.MALApi.ListType;
import net.somethingdreadful.MAL.broadcasts.RecordStatusUpdatedReceiver;

public class WriteDetailTask extends AsyncTask<GenericRecord, Void, Boolean> {
    private ListType type = ListType.ANIME;
    private final Activity activity;

    public WriteDetailTask(ListType type, Activity activity) {
        this.type = type;
        this.activity = activity;
    }

    @Override
    protected Boolean doInBackground(GenericRecord... gr) {
        boolean error = false;
        ContentManager manager = new ContentManager(activity);

        if (!AccountService.isMAL() && APIHelper.isNetworkAvailable(activity))
            manager.verifyAuthentication();

        try {
            if (APIHelper.isNetworkAvailable(activity)) {
                if (type.equals(ListType.ANIME)) {
                    Anime anime = (Anime) gr[0];
                    if (manager.writeAnimeDetails(anime))
                        anime.clearDirty();
                    manager.saveAnimeToDatabase(anime);
                } else {
                    Manga manga = (Manga) gr[0];
                    if (manager.writeMangaDetails(manga))
                        manga.clearDirty();
                    manager.saveMangaToDatabase(manga);
                }
            }
        } catch (Exception e) {
            Crashlytics.log(Log.ERROR, "Atarashii", "WriteDetailTask.doInBackground(5, " + type + "): unknown API error (?): " + e.getMessage());
            Crashlytics.logException(e);
            error = true;
        }

        // only update if everything went well!
        if (!error) {
            if (gr[0].getDeleteFlag()) {
                if (ListType.ANIME.equals(type)) {
                    manager.deleteAnime((Anime) gr[0]);
                } else {
                    manager.deleteManga((Manga) gr[0]);
                }
            } else {
                if (type.equals(ListType.ANIME)) {
                    manager.saveAnimeToDatabase((Anime) gr[0]);
                } else {
                    manager.saveMangaToDatabase((Manga) gr[0]);
                }
            }
        }

        return null;
    }

    @Override
    protected void onPostExecute(Boolean aBoolean) {
        // send broadcast for list updates
        Intent i = new Intent();
        i.setAction(RecordStatusUpdatedReceiver.RECV_IDENT);
        i.putExtra("type", type);
        LocalBroadcastManager.getInstance(activity).sendBroadcast(i);
    }
}
@@ -1136,16 +1136,93 @@
  false;%0A
+        boolean isNetworkAvailable = APIHelper.isNetworkAvailable(activity);%0A
         
@@ -1316,26 +1316,16 @@
 AL() && 
-APIHelper.
 isNetwor
@@ -1334,26 +1334,16 @@
 vailable
-(activity)
 )%0A      
@@ -1411,22 +1411,71 @@
     
-if (APIHelper.
+// Sync details if there is network connection%0A            if (
 isNe
@@ -1488,26 +1488,16 @@
 vailable
-(activity)
 ) %7B%0A    
@@ -1567,293 +1567,113 @@
     
-Anime anime = (Anime) gr%5B0%5D;%0A                    if (manager.writeAnimeDetails(anime))%0A                        anime.clearDirty();%0A                    manager.saveAnimeToDatabase(anime);%0A                %7D else %7B%0A                    Manga manga = (Manga) gr%5B0%5D;%0A                    if (
+error = !manager.writeAnimeDetails((Anime) gr%5B0%5D);%0A                %7D else %7B%0A                    error = !
 mana
@@ -1698,113 +1698,21 @@
 ils(
-m
+(M
 anga)
-)%0A                        manga.clearDirty();%0A                    manager.saveMangaToDatabase(manga
+ gr%5B0%5D
 );%0A 
@@ -2013,71 +2013,203 @@
  // 
-only update if everything went well!%0A        if (!error) %7B%0A    
+Records updated successfully and will be marked as done if it hasn't been removed%0A        if (isNetworkAvailable && !error && !gr%5B0%5D.getDeleteFlag()) %7B%0A            gr%5B0%5D.clearDirty();%0A        %7D%0A%0A
     
@@ -2237,32 +2237,57 @@
 teFlag()) %7B%0A    
+        // Delete record%0A
             if (
@@ -2313,28 +2313,24 @@
 ls(type)) %7B%0A
-    
             
@@ -2373,36 +2373,32 @@
 %5D);%0A            
-    
 %7D else %7B%0A       
@@ -2398,36 +2398,32 @@
                 
- 
-   
 manager.deleteMa
@@ -2458,39 +2458,59 @@
     
-    %7D%0A            %7D else %7B%0A    
+%7D%0A        %7D else %7B%0A            // Save the records%0A
     
@@ -2560,36 +2560,32 @@
                 
-    
 manager.saveAnim
@@ -2616,36 +2616,32 @@
 %5D);%0A            
-   
- 
 %7D else %7B%0A       
@@ -2625,36 +2625,32 @@
        %7D else %7B%0A
-    
                 
@@ -2693,34 +2693,16 @@
 gr%5B0%5D);%0A
-                %7D%0A
         
@@ -2709,33 +2709,32 @@
     %7D%0A        %7D%0A
-%0A
         return n

Fix removed records are saved in the DB & speed improvement