package com.icaynia.soundki.Data;

import android.content.Context;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

import com.icaynia.soundki.Model.PlayList;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

/**
 * Created by icaynia on 01/03/2017.
 */

public class FileManager
{
    public Context context;
    public String TAG = "FileManager";

    String dirPath;

    public FileManager(Context context)
    {
        init();
    }

    public void init()
    {
        dirPath = context.getFilesDir().getPath();
        File file = new File(dirPath);
        if( !file.exists() ) {
            file.mkdirs();
        }
        file = new File(dirPath+"/playlist");
        if( !file.exists() ) {
            file.mkdirs();
        }
    }

    public void savePlaylist(PlayList playlist)
    {
        String filename = playlist.getName();
        if (isFileAvailable(filename))
        {
            Log.e(TAG, "File is already created");
            return;
        }
        if (playlist.getName().equals(""))
        {
            Log.e(TAG, "Playlist name isn't set.");
        }

        try
        {
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("dg.ser"));
            oos.writeObject(playlist);
            oos.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public boolean isFileAvailable(String file)
    {
        File tmpFile = new File(file);
        if( tmpFile.exists() ) {
            return true;
        }
        return false;
    }
}



@@ -60,16 +60,56 @@
 ontext;%0A
+import android.media.audiofx.BassBoost;%0A
 import a
@@ -251,16 +251,48 @@
 o.File;%0A
+import java.io.FileInputStream;%0A
 import j
@@ -344,16 +344,78 @@
 eption;%0A
+import java.io.ObjectInput;%0Aimport java.io.ObjectInputStream;%0A
 import j
@@ -1047,26 +1047,183 @@
     
-String filename = 
+if (playlist.getName().equals(%22%22))%0A        %7B%0A            Log.e(TAG, %22Playlist name isn't set.%22);%0A            return;%0A        %7D%0A%0A        String filename = dirPath+%22/playlist/%22+
 play
@@ -1236,16 +1236,27 @@
 etName()
++%22.soundki%22
 ;%0A      
@@ -1345,17 +1345,31 @@
  created
-%22
+ : %22 + filename
 );%0A     
@@ -1397,123 +1397,608 @@
   %7D%0A
-        if (playlist.getName().equals(%22%22))%0A        %7B%0A            Log.e(TAG, %22Playlist name isn't set.%22);%0A        %7D%0A
+%0A        try%0A        %7B%0A            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename));%0A            oos.writeObject(playlist);%0A            oos.close();%0A        %7D%0A        catch (Exception e)%0A        %7B%0A            Log.e(TAG, %22Something was wrong!%22);%0A            e.printStackTrace();%0A        %7D%0A    %7D%0A%0A    public PlayList loadPlayList(String playlistName)%0A    %7B%0A        if (playlistName.equals(%22%22))%0A        %7B%0A            Log.e(TAG, %22Playlist name isn't set.%22);%0A        %7D%0A        PlayList playlist = null;%0A        String filename = dirPath+%22/playlist/%22+playlistName+%22.soundki%22;
 %0A   
@@ -2026,35 +2026,34 @@
           Object
-Out
+In
 putStream oos = 
@@ -2039,33 +2039,33 @@
 ectInputStream o
-o
+i
 s = new ObjectOu
@@ -2054,35 +2054,34 @@
 ois = new Object
-Out
+In
 putStream(new Fi
@@ -2074,35 +2074,34 @@
 tStream(new File
-Out
+In
 putStream(%22dg.se
@@ -2098,16 +2098,16 @@
 eam(
-%22dg.ser%22
+filename
 ));%0A
@@ -2122,32 +2122,45 @@
     
-oos.writeObject(playlist
+playlist = (PlayList) ois.readObject(
 );%0A 
@@ -2163,33 +2163,33 @@
 );%0A            o
-o
+i
 s.close();%0A     
@@ -2223,32 +2223,80 @@
 on e)%0A        %7B%0A
+            Log.e(TAG, %22Something was wrong!%22);%0A
             e.pr
@@ -2313,32 +2313,58 @@
 ace();%0A        %7D
+%0A%0A        return playlist;
 %0A    %7D%0A%0A    publ

Add loadPlaylist method