Advertisement
Guest User

Untitled

a guest
Feb 24th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 19.33 KB | None | 0 0
  1. package cz.sudoman281.kubirovaciKalkulacka.layout;
  2.  
  3. import android.Manifest;
  4. import android.content.Context;
  5. import android.content.DialogInterface;
  6. import android.content.Intent;
  7. import android.content.SharedPreferences;
  8. import android.content.pm.PackageManager;
  9. import android.net.Uri;
  10. import android.os.Bundle;
  11. import android.os.Environment;
  12. import android.support.annotation.NonNull;
  13. import android.support.v4.app.ActivityCompat;
  14. import android.support.v4.app.Fragment;
  15. import android.support.v7.app.AlertDialog;
  16. import android.view.LayoutInflater;
  17. import android.view.View;
  18. import android.view.ViewGroup;
  19. import android.view.inputmethod.InputMethodManager;
  20. import android.widget.Button;
  21. import android.widget.EditText;
  22. import android.widget.ListView;
  23. import android.widget.TextView;
  24. import android.widget.Toast;
  25.  
  26. import com.google.gson.Gson;
  27.  
  28. import java.io.File;
  29. import java.util.ArrayList;
  30. import java.util.HashMap;
  31. import java.util.Map;
  32.  
  33. import cz.sudoman281.kubirovaciKalkulacka.R;
  34. import cz.sudoman281.kubirovaciKalkulacka.Result;
  35. import cz.sudoman281.kubirovaciKalkulacka.ResultAdapter;
  36. import cz.sudoman281.kubirovaciKalkulacka.Updatable;
  37. import cz.sudoman281.kubirovaciKalkulacka.Utils;
  38.  
  39.  
  40. public class ListFragment extends Fragment implements Updatable {
  41.  
  42.     final int PERMISSION_ALL = 1;
  43.     View thisView;
  44.     Button resetInput;
  45.     Button saveInput;
  46.     Button loadInput;
  47.     Button sendInput;
  48.     ListView resultsListView;
  49.     TextView info;
  50.     TextView listName;
  51.     Button closeInput;
  52.     SharedPreferences sharedPreferences;
  53.     String[] PERMISSIONS = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
  54.     String currentDialog = "";
  55.     ResultAdapter adapter;
  56.  
  57.     //TODO: List of lists into another activity
  58.     //TODO: Growth not in every line bud as a title (do it also as name in save)
  59.     //TODO: Growth is not individual for every record
  60.     /*Dobrý den, chci Vám pomoci s Vaší appkou. Podle mně, je vhodné dát JPRL úplně nahoru jako nadpis a taky pod názvem JPRL ukládat, s možností přepsání jména JPRL. Normálně funguje příjem dříví tak, že se těží v porostě (JPRL) několik dnů a příjem se postupně přidává.
  61. Já bych zmenšil i font písma u JPRL a pole zkrátil na 7 znaků JPRL má nejdelší možný tvar např 720A10a, u dřevin bych použil zkratky SM,JD,BO,VJ,MD,DB,DBC,BK,HB,JV,KL,JS,LP,OL,TP,VR,OS, dále bych zkrátil pole délka na 2 znaky, delší strom, než 99 metrů dělat nebudeme :-) Taky pole průměr na 3 znaky. Můžete použít zkratky m,Ø což ne EN klávesnice a levý Alt 0216.
  62. Taky bych zrušil ponechání zaškrtnutého oddenku po uložení položky, oddenků je podstatně méně než výřezů a zapomínám rušit to zaškrtnutí. Taky by bylo dobré zapisovat teplickpu metodou přijaté dříví k výřezu 0=0,05plm, 1=0,10 plm, 2=0,2 plm atd, stačí do 4ky t.j do 0,40 plm. K tomu jen počet kusů. Příjem z lesa vypadá asi takto:
  63. 720A10a
  64. BOx 4-32 - oddenková kulatina
  65. BO 8-26 - další výřezy
  66. BO 10-20
  67. BOx 6-42  - oddenková kulatina
  68. BO  8-36  - další výřezy
  69. BO 12-22
  70. O-II - dvě špice nulky
  71. z toho vyplývá, že se jedná o dva stromy BO a měli dvě nulky jako špice, což je 2x0,05 kubíku.(plm) Zde vidíte, že máme 8 kusů ale jen dva oddenky.
  72. Taky nevím, zdy máte ošetřený kubírování dle Pravidel pro měření dříví, u BO se počítá do oddenku výřez pod 8 metrů.
  73. V menu Seznamy bych v řádku nezobrazoval JPRL pořád dookola, ale jako napis pod kterým je to uloženo. Každý si to uloží pod JPRL nebo si název změní v nabídce Uložit jako. Raději bych dal do řádku číslo kusu, dřevina, oddenek,  delka, průměr, množství.
  74. Taky bych ponechal možnost opravovat počet oddenků.
  75. 720A10a
  76. 1121  BO x  4 - 32  0,44 m3
  77. 1122  BO x  8 - 26  0,52 m3
  78.          BO  0    2ks  0,10 m3
  79. Jako výstup by bylo super extra vypočítat sumář dle dřevin a průmernou hmotnatost dle dřevin, ta určuje cenu za práci. To znamená za dřevinu celkem m3 / oddenky na dvě desetiny např hm 0,32
  80.  
  81. Vím, je to zložité ale já bych za takovou appku stovku zaplatil :-)*/
  82.  
  83.     @Override
  84.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  85.         Initialize(inflater, container);
  86.         Utils.SetResultsArray(new ArrayList<Result>());
  87.         adapter = new ResultAdapter(getActivity(), Utils.GetResultsArray(), this);
  88.         resultsListView = (ListView) thisView.findViewById(R.id.resultsListView);
  89.         resultsListView.setAdapter(adapter);
  90.         SetListeners();
  91.         return thisView;
  92.     }
  93.  
  94.     public void OnUpdate() {
  95.         final InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
  96.         imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
  97.         adapter.notifyDataSetChanged();
  98.         info.setText(Utils.SumCountText());
  99.     }
  100.  
  101.     private void Initialize(LayoutInflater inflater, ViewGroup container) {
  102.         thisView = inflater.inflate(R.layout.fragment_list, container, false);
  103.         resetInput = (Button) thisView.findViewById(R.id.resetInput);
  104.         saveInput = (Button) thisView.findViewById(R.id.saveInput);
  105.         loadInput = (Button) thisView.findViewById(R.id.loadInput);
  106.         sendInput = (Button) thisView.findViewById(R.id.sendInput);
  107.         info = (TextView) thisView.findViewById(R.id.info);
  108.         listName = (TextView) thisView.findViewById(R.id.listName);
  109.         closeInput = (Button) thisView.findViewById(R.id.closeInput);
  110.         sharedPreferences = getContext().getSharedPreferences("cz.sudoman281.kubirovaciKalkulacka", Context.MODE_PRIVATE);
  111.     }
  112.  
  113.     private String makeCSV() {
  114.         String data;
  115.         data = "Číslo porostu;Číslo kusu;Oddenek;Dřevina;Délka;Průměr;Objem";
  116.         for (Result result : Utils.GetResultsArray()) {
  117.             String stool = "";
  118.             if (result.isStool()) {
  119.                 stool = "x";
  120.             }
  121.             data += "\n" + result.getGrowthNumber() + ";" + String.valueOf(result.getSerialNumber()) + ";" + stool + ";" + result.getWood() + ";" + String.valueOf(result.getLength()) + ";" +
  122.                     String.valueOf(result.getDiameter()) + ";" + String.valueOf(result.getVolume());
  123.         }
  124.  
  125.         data += "\n\nDohromady kusů: " + String.valueOf(Utils.GetResultsArray().size()) + ";Celkový objem: " + Utils.Sum().replace(",", ".") + " m³";
  126.         return data;
  127.     }
  128.  
  129.     private void SetListeners() {
  130.         saveInput.setOnClickListener(new View.OnClickListener() {
  131.             @Override
  132.             public void onClick(View view) {
  133.                 currentDialog = "save";
  134.                 if (!Utils.HasPermissions(getContext(), PERMISSIONS)) {
  135.                     requestPermissions(PERMISSIONS, PERMISSION_ALL);
  136.                 } else {
  137.                     CreateSaveDialog();
  138.                 }
  139.             }
  140.         });
  141.  
  142.         loadInput.setOnClickListener(new View.OnClickListener() {
  143.             @Override
  144.             public void onClick(View view) {
  145.                 currentDialog = "load";
  146.                 String[] PERMISSIONS = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
  147.                 if (!Utils.HasPermissions(getContext(), PERMISSIONS)) {
  148.                     requestPermissions(PERMISSIONS, PERMISSION_ALL);
  149.                 } else {
  150.                     LoadList();
  151.                 }
  152.             }
  153.         });
  154.  
  155.         resetInput.setOnClickListener(new View.OnClickListener() {
  156.             @Override
  157.             public void onClick(View view) {
  158.                 AlertDialog.Builder resetConfirmDialog = new AlertDialog.Builder(getContext());
  159.                 resetConfirmDialog.setTitle("Reset");
  160.                 resetConfirmDialog.setMessage("Resetováním přijdete o zadaná data, chcete pokračovat?");
  161.                 resetConfirmDialog.setPositiveButton("Ano", new DialogInterface.OnClickListener() {
  162.                     @Override
  163.                     public void onClick(DialogInterface dialogInterface, int i) {
  164.                         Utils.GetResultsArray().clear();
  165.                         adapter.notifyDataSetChanged();
  166.                         info.setText(Utils.SumCountText());
  167.                         Utils._dataSaved = false;
  168.                     }
  169.                 });
  170.                 resetConfirmDialog.setNegativeButton("Ne", null);
  171.                 resetConfirmDialog.show();
  172.             }
  173.         });
  174.  
  175.         sendInput.setOnClickListener(new View.OnClickListener() {
  176.             @Override
  177.             public void onClick(View view) {
  178.                 currentDialog = "send";
  179.                 String[] PERMISSIONS = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
  180.                 if (!Utils.HasPermissions(getContext(), PERMISSIONS)) {
  181.                     ActivityCompat.requestPermissions(getActivity(), PERMISSIONS, PERMISSION_ALL);
  182.                 } else {
  183.                     SendList();
  184.                 }
  185.             }
  186.         });
  187.  
  188.         closeInput.setOnClickListener(new View.OnClickListener() {
  189.             @Override
  190.             public void onClick(View view) {
  191.                 if (!Utils._dataSaved) {
  192.                     AlertDialog.Builder saveDialog = new AlertDialog.Builder(getContext());
  193.                     saveDialog.setMessage("Zavřít seznam bez uložení?");
  194.                     saveDialog.setTitle("Zavřít");
  195.                     saveDialog.setPositiveButton("Ano", new DialogInterface.OnClickListener() {
  196.                         @Override
  197.                         public void onClick(DialogInterface dialog, int which) {
  198.                             Utils._actualListName = "";
  199.                             listName.setText("");
  200.                             saveInput.setText("Uložit\njako");
  201.                             closeInput.setVisibility(View.INVISIBLE);
  202.                             Utils.GetResultsArray().clear();
  203.                             adapter.notifyDataSetChanged();
  204.                             info.setText(Utils.SumCountText());
  205.                             Utils._dataSaved = true;
  206.                         }
  207.                     });
  208.                     saveDialog.setNegativeButton("Ne", null);
  209.  
  210.                     saveDialog.show();
  211.                 } else {
  212.                     Utils._actualListName = "";
  213.                     listName.setText("");
  214.                     saveInput.setText("Uložit\njako");
  215.                     closeInput.setVisibility(View.INVISIBLE);
  216.                     Utils.GetResultsArray().clear();
  217.                     adapter.notifyDataSetChanged();
  218.                     info.setText(Utils.SumCountText());
  219.                     Utils._dataSaved = true;
  220.                 }
  221.             }
  222.         });
  223.     }
  224.  
  225.     public void OnDeleteItemClick(int position) {
  226.         int changeToNumber = Utils.GetResultsArray().get(0).getSerialNumber();
  227.         Utils.GetResultsArray().remove(position);
  228.         for (Result result : Utils.GetResultsArray()) {
  229.             result.setSerialNumber(changeToNumber);
  230.             changeToNumber++;
  231.         }
  232.         adapter.notifyDataSetChanged();
  233.         info.setText(Utils.SumCountText());
  234.         Utils._dataSaved = false;
  235.     }
  236.  
  237.     private void ShowToast(String text) {
  238.         Toast.makeText(getContext(), text, Toast.LENGTH_SHORT).show();
  239.     }
  240.  
  241.     private void LoadList() {
  242.         Intent intent = new Intent(getContext(), ListsActivity.class);
  243.         startActivityForResult(intent, 1);
  244.     }
  245.  
  246.     private void CreateSaveDialog() {
  247.         if (Utils._actualListName.equals("")) {
  248.             AlertDialog.Builder saveDialog = new AlertDialog.Builder(getContext());
  249.  
  250.             final EditText nameInput = new EditText(getContext());
  251.             saveDialog.setMessage("Pojmenujte soubor");
  252.             saveDialog.setTitle("Ukládání");
  253.             saveDialog.setView(nameInput);
  254.  
  255.             nameInput.requestFocus();
  256.             InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
  257.             imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
  258.  
  259.             saveDialog.setPositiveButton("Uložit", new DialogInterface.OnClickListener() {
  260.                 public void onClick(final DialogInterface dialog, int whichButton) {
  261.                     File directory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), "kubirovaciKalkulacka");
  262.                     String fileName = nameInput.getText().toString() + ".csv";
  263.                     String regex = ".*[^-_.A-Za-z0-9 áčďéěíňóřšťúůýžÁČĎÉĚÍŇÓŘŠŤÚŮÝŽ].*";
  264.                     if (!fileName.matches(regex)) {
  265.                         File file = new File(directory, fileName);
  266.                         if (file.exists()) {
  267.                             final AlertDialog.Builder builderInner = new AlertDialog.Builder(getContext());
  268.                             builderInner.setMessage("Nelze vytvořit více souborů se stejným jménem");
  269.                             builderInner.setTitle("Upozornění");
  270.                             builderInner.setNegativeButton("Zrušit", null);
  271.                             builderInner.show();
  272.                         } else {
  273.                             Gson gson = new Gson();
  274.                             String json = gson.toJson(Utils.GetResultsArray());
  275.                             File fileComplete = Utils.CreateFile("kubirovaciKalkulacka", fileName, json);
  276.                             if (fileComplete.exists()) {
  277.                                 Utils._actualListName = fileName.replace(".csv", "");
  278.                                 SetListName();
  279.                                 closeInput.setVisibility(View.VISIBLE);
  280.                                 saveInput.setText("Uložit");
  281.                                 Utils._dataSaved = true;
  282.                                 ShowToast("Uloženo");
  283.                                 InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
  284.                                 imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
  285.                             } else {
  286.                                 ShowToast("Nastala chyba při vytváření souboru");
  287.                                 InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
  288.                                 imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
  289.                             }
  290.                         }
  291.                     } else {
  292.                         ShowToast("Bohužel nelze takto pojmenovat soubor");
  293.                     }
  294.                 }
  295.             });
  296.  
  297.             saveDialog.setNegativeButton("Zrušit", new DialogInterface.OnClickListener() {
  298.                 @Override
  299.                 public void onClick(DialogInterface dialogInterface, int i) {
  300.                     InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
  301.                     imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
  302.                 }
  303.             });
  304.  
  305.             saveDialog.show();
  306.         } else {
  307.             String fileName = Utils._actualListName + ".csv";
  308.             Gson gson = new Gson();
  309.             String json = gson.toJson(Utils.GetResultsArray());
  310.             File fileComplete = Utils.CreateFile("kubirovaciKalkulacka", fileName, json);
  311.             if (fileComplete.exists()) {
  312.                 ShowToast("Uloženo");
  313.                 Utils._dataSaved = true;
  314.             } else {
  315.                 ShowToast("Nastala chyba při vytváření souboru");
  316.             }
  317.         }
  318.     }
  319.  
  320.     private void SendList() {
  321.         String data = makeCSV();
  322.         String fileName;
  323.         if (Utils._actualListName.equals("")) {
  324.             fileName = "tabulkaDrevin.csv";
  325.         } else {
  326.             fileName = Utils._actualListName + ".csv";
  327.         }
  328.         File file = Utils.CreateFile("kubirovaciKalkulackaEmail", fileName, data);
  329.  
  330.         if (file.exists()) {
  331.             Uri path = Uri.fromFile(file);
  332.             Intent i = new Intent(Intent.ACTION_SEND);
  333.             i.setType("message/rfc822");
  334.             String recipient = sharedPreferences.getString("email", "@");
  335.             i.putExtra(Intent.EXTRA_EMAIL, new String[]{recipient});
  336.             i.putExtra(Intent.EXTRA_SUBJECT, "Tabulka dřevin - Kubirovaci kalkulacka");
  337.             i.putExtra(Intent.EXTRA_STREAM, path);
  338.             try {
  339.                 startActivity(Intent.createChooser(i, "Odeslat..."));
  340.             } catch (android.content.ActivityNotFoundException ex) {
  341.                 ShowToast("Musíte mít nainstalovaného emailového klienta");
  342.             }
  343.         } else {
  344.             ShowToast("Nastala chyba při vytváření souboru");
  345.         }
  346.     }
  347.  
  348.     @Override
  349.     public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  350.         switch (requestCode) {
  351.             case PERMISSION_ALL:
  352.                 Map<String, Integer> perms = new HashMap<>();
  353.                 // Initial
  354.                 perms.put(Manifest.permission.READ_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
  355.                 perms.put(Manifest.permission.WRITE_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
  356.                 // Fill with results
  357.                 for (int i = 0; i < permissions.length; i++)
  358.                     perms.put(permissions[i], grantResults[i]);
  359.                 // Check for ACCESS_FINE_LOCATION
  360.                 if (perms.get(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
  361.                         && perms.get(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
  362.                     switch (currentDialog) {
  363.                         case "save":
  364.                             CreateSaveDialog();
  365.                             break;
  366.                         case "load":
  367.                             LoadList();
  368.                             break;
  369.                         case "send":
  370.                             SendList();
  371.                     }
  372.                 } else {
  373.                     // Permission Denied
  374.                     ShowToast("Permise zamítnuta, nebudete moci ukládat seznamy");
  375.                 }
  376.                 break;
  377.             default:
  378.                 super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  379.         }
  380.     }
  381.  
  382.     private void SetListName() {
  383.         adapter.notifyDataSetChanged();
  384.         info.setText(Utils.SumCountText());
  385.         if(!Utils._actualListName.equals("")) {
  386.             closeInput.setVisibility(View.VISIBLE);
  387.             saveInput.setText("Uložit");
  388.         } else {
  389.             saveInput.setText("Uložit\njako");
  390.             closeInput.setVisibility(View.INVISIBLE);
  391.         }
  392.         if (Utils._actualListName.length() > 15) {
  393.             String shortenListName = Utils._actualListName.substring(0, 15) + "...";
  394.             listName.setText(shortenListName);
  395.         } else {
  396.             listName.setText(Utils._actualListName);
  397.         }
  398.     }
  399.  
  400.     @Override
  401.     public void onActivityResult(int requestCode, int resultCode, Intent data) {
  402.         if (requestCode == 1) {
  403.             SetListName();
  404.         }
  405.     }
  406. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement