Skip to content

Commit

Permalink
Closes #99
Browse files Browse the repository at this point in the history
Merge branch 'fix/export' into develop

* fix/export:
  Fixes issue with exporting large plant lists and updates markdown
  Fixes markdown and reverses action list
  • Loading branch information
7LPdWcaW authored and 7LPdWcaW committed Jul 9, 2019
2 parents bd762ab + e0c9bbf commit ccb99ac
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ else if (item.getItemId() == R.id.export_garden)
NotificationHelper.sendExportNotification(getActivity(), "Exporting garden grow log", "Exporting " + garden.getName());

ArrayList<Plant> export = new ArrayList<>(adapter.getPlants());
exportPlants(export);
ExportService.export(getActivity(), export, garden.getName().replaceAll("[^a-zA-Z0-9]+", "-"), garden.getName());
}
else if (item.getItemId() == R.id.delete_garden)
{
Expand Down Expand Up @@ -524,12 +524,6 @@ else if (item.getItemId() == R.id.delete_garden)
return super.onOptionsItemSelected(item);
}

private void exportPlants(final ArrayList<Plant> plants)
{
Toast.makeText(getActivity(), "Exporting grow log...", Toast.LENGTH_SHORT).show();
ExportService.export(getActivity(), plants, garden.getName().replaceAll("[^a-zA-Z0-9]+", "-"), garden.getName());
}

private void filter()
{
ArrayList<Plant> plantList = PlantManager.getInstance().getSortedPlantList(garden);
Expand Down
10 changes: 7 additions & 3 deletions app/src/main/java/me/anon/grow/service/ExportService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import me.anon.lib.ExportCallback
import me.anon.lib.helper.ExportHelper
import me.anon.lib.helper.GsonHelper
import me.anon.lib.helper.NotificationHelper
import me.anon.lib.manager.PlantManager
import me.anon.model.Plant
import java.io.File

Expand All @@ -25,9 +26,8 @@ class ExportService : Service()
@JvmStatic
public fun export(context: Context, plants: ArrayList<Plant>, title: String, name: String)
{
val plantStr = GsonHelper.getGson().toJson(plants, object : TypeToken<ArrayList<Plant>>(){}.type)
val intent = Intent(context, ExportService::class.java)
intent.putExtra("plants", plantStr)
intent.putStringArrayListExtra("plants", ArrayList(plants.map { it.id }))
intent.putExtra("title", title)
intent.putExtra("name", name)
context.startService(intent)
Expand All @@ -39,7 +39,9 @@ class ExportService : Service()
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int
{
intent?.let {
val plants = GsonHelper.parse<ArrayList<Plant>>(it.extras.getString("plants", "[]"), object : TypeToken<ArrayList<Plant>>(){}.type) as ArrayList<Plant>
val plantsIds = it.extras.getStringArrayList("plants") ?: arrayListOf()

val plants = ArrayList(PlantManager.getInstance().plants.filter { plantsIds.contains(it.id) })
val title = it.getStringExtra("title")
val name = it.getStringExtra("name")

Expand All @@ -61,6 +63,8 @@ class ExportService : Service()
{
context.sendBroadcast(Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.fromFile(file)))
}

stopSelf()
}
})
}
Expand Down
37 changes: 25 additions & 12 deletions app/src/main/java/me/anon/lib/helper/ExportHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public class ExportHelper
double days = (double)seconds * 0.0000115741d;

StringBuffer plantDetails = new StringBuffer(1000);
plantDetails.append("#").append(plant.getName()).append(" Grow Log");
plantDetails.append("# ").append(plant.getName()).append(" Grow Log");
plantDetails.append(NEW_LINE);
plantDetails.append("*Strain*: ").append(plant.getStrain());
plantDetails.append(NEW_LINE);
Expand All @@ -153,7 +153,7 @@ public class ExportHelper
plantDetails.append("*Medium*: ").append(plant.getMedium().getPrintString());
plantDetails.append(NEW_LINE);

plantDetails.append("##Stages");
plantDetails.append("## Stages");
plantDetails.append(NEW_LINE);

SortedMap<PlantStage, Long> stages = plant.calculateStageTime();
Expand All @@ -172,7 +172,7 @@ public class ExportHelper
plantDetails.append(NEW_LINE);
}

plantDetails.append("##General stats");
plantDetails.append("## General stats");
plantDetails.append(NEW_LINE);
plantDetails.append(" - *Total grow time*: ").append(String.format("%1$,.2f days", days));
plantDetails.append(NEW_LINE);
Expand Down Expand Up @@ -212,19 +212,21 @@ public class ExportHelper

if (!additiveNames.isEmpty())
{
plantDetails.append("##Additives used");
plantDetails.append("## Additives used");
plantDetails.append(NEW_LINE);
plantDetails.append(" - ");
plantDetails.append(TextUtils.join(NEW_LINE + " - ", additiveNames));
plantDetails.append(NEW_LINE);
}

plantDetails.append("##Timeline");
plantDetails.append("## Timeline");
plantDetails.append(NEW_LINE);

for (Action action : plant.getActions())
ArrayList<Action> actions = plant.getActions();
for (int i = actions.size() - 1; i >= 0; i--)
{
plantDetails.append("###").append(printableDate(context, action.getDate()));
Action action = actions.get(i);
plantDetails.append("### ").append(printableDate(context, action.getDate()));
plantDetails.append(NEW_LINE);

if (action.getClass() == Water.class)
Expand Down Expand Up @@ -301,7 +303,10 @@ else if (action instanceof StageChange)

for (Additive additive : ((Water)action).getAdditives())
{
if (additive == null || additive.getAmount() == null) continue;
if (additive == null || additive.getAmount() == null)
{
continue;
}

double converted = ML.to(measureUnit, additive.getAmount());
String amountStr = converted == Math.floor(converted) ? String.valueOf((int)converted) : String.valueOf(converted);
Expand Down Expand Up @@ -331,7 +336,7 @@ else if (action instanceof StageChange)
}
}

plantDetails.append("##Raw plant data");
plantDetails.append("## Raw plant data");
plantDetails.append(NEW_LINE);
plantDetails.append("```").append("\r\n").append(GsonHelper.parse(plant)).append("\r\n").append("```");
plantDetails.append(NEW_LINE);
Expand Down Expand Up @@ -515,16 +520,23 @@ protected static void copyImagesAndFinish(Context context, final ArrayList<Plant
.setContentIntent(PendingIntent.getActivity(appContext, 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT))
.setTicker("Exporting grow log for " + (plant.size() == 1 ? plant.get(0).getName() : "multiple plants"))
.setSmallIcon(R.drawable.ic_stat_name)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setPriority(NotificationCompat.PRIORITY_LOW)
.setSound(null);

notificationManager.notify(0, exportNotification.build());
}

@Override protected void onProgressUpdate(Integer... values)
{
exportNotification.setProgress(values[1], values[0], false);
notificationManager.notify(0, exportNotification.build());
if (values[1] == values[0])
{
notificationManager.cancel(0);
}
else
{
exportNotification.setProgress(values[1], values[0], false);
notificationManager.notify(0, exportNotification.build());
}
}

@Override protected File doInBackground(Plant... params)
Expand Down Expand Up @@ -571,6 +583,7 @@ protected static void copyImagesAndFinish(Context context, final ArrayList<Plant
}
}

notificationManager.cancel(0);
callback.onCallback(appContext, finalFile.getFile());
return null;
}
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/me/anon/lib/helper/NotificationHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ object NotificationHelper
if (Build.VERSION.SDK_INT >= 26)
{
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val channel = NotificationChannel("export", "Export status", NotificationManager.IMPORTANCE_HIGH)
val channel = NotificationChannel("export", "Export status", NotificationManager.IMPORTANCE_DEFAULT)
channel.setSound(null, null)
channel.enableLights(false)
channel.enableVibration(false)
Expand Down Expand Up @@ -67,7 +67,7 @@ object NotificationHelper
.setContentText(message)
.setTicker(title)
.setContentTitle("Export Complete")
.setContentIntent(PendingIntent.getActivity(context, 0, openIntent, PendingIntent.FLAG_UPDATE_CURRENT))
.setContentIntent(PendingIntent.getActivity(context, 0, openIntent, PendingIntent.FLAG_CANCEL_CURRENT))
.setStyle(NotificationCompat.BigTextStyle()
.bigText(message)
)
Expand Down

0 comments on commit ccb99ac

Please sign in to comment.