Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cz.sudoman281.kubirovaciKalkulacka.layout;
- import android.Manifest;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.content.pm.PackageManager;
- import android.net.Uri;
- import android.os.Bundle;
- import android.os.Environment;
- import android.support.annotation.NonNull;
- import android.support.v4.app.ActivityCompat;
- import android.support.v4.app.Fragment;
- import android.support.v7.app.AlertDialog;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.inputmethod.InputMethodManager;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ListView;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.google.gson.Gson;
- import java.io.File;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Map;
- import cz.sudoman281.kubirovaciKalkulacka.R;
- import cz.sudoman281.kubirovaciKalkulacka.Result;
- import cz.sudoman281.kubirovaciKalkulacka.ResultAdapter;
- import cz.sudoman281.kubirovaciKalkulacka.Updatable;
- import cz.sudoman281.kubirovaciKalkulacka.Utils;
- public class ListFragment extends Fragment implements Updatable {
- final int PERMISSION_ALL = 1;
- View thisView;
- Button resetInput;
- Button saveInput;
- Button loadInput;
- Button sendInput;
- ListView resultsListView;
- TextView info;
- TextView listName;
- Button closeInput;
- SharedPreferences sharedPreferences;
- String[] PERMISSIONS = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
- String currentDialog = "";
- ResultAdapter adapter;
- //TODO: List of lists into another activity
- //TODO: Growth not in every line bud as a title (do it also as name in save)
- //TODO: Growth is not individual for every record
- /*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á.
- 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.
- 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:
- 720A10a
- BOx 4-32 - oddenková kulatina
- BO 8-26 - další výřezy
- BO 10-20
- BOx 6-42 - oddenková kulatina
- BO 8-36 - další výřezy
- BO 12-22
- O-II - dvě špice nulky
- 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.
- 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ů.
- 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í.
- Taky bych ponechal možnost opravovat počet oddenků.
- 720A10a
- 1121 BO x 4 - 32 0,44 m3
- 1122 BO x 8 - 26 0,52 m3
- BO 0 2ks 0,10 m3
- 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
- Vím, je to zložité ale já bych za takovou appku stovku zaplatil :-)*/
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- Initialize(inflater, container);
- Utils.SetResultsArray(new ArrayList<Result>());
- adapter = new ResultAdapter(getActivity(), Utils.GetResultsArray(), this);
- resultsListView = (ListView) thisView.findViewById(R.id.resultsListView);
- resultsListView.setAdapter(adapter);
- SetListeners();
- return thisView;
- }
- public void OnUpdate() {
- final InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
- imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
- adapter.notifyDataSetChanged();
- info.setText(Utils.SumCountText());
- }
- private void Initialize(LayoutInflater inflater, ViewGroup container) {
- thisView = inflater.inflate(R.layout.fragment_list, container, false);
- resetInput = (Button) thisView.findViewById(R.id.resetInput);
- saveInput = (Button) thisView.findViewById(R.id.saveInput);
- loadInput = (Button) thisView.findViewById(R.id.loadInput);
- sendInput = (Button) thisView.findViewById(R.id.sendInput);
- info = (TextView) thisView.findViewById(R.id.info);
- listName = (TextView) thisView.findViewById(R.id.listName);
- closeInput = (Button) thisView.findViewById(R.id.closeInput);
- sharedPreferences = getContext().getSharedPreferences("cz.sudoman281.kubirovaciKalkulacka", Context.MODE_PRIVATE);
- }
- private String makeCSV() {
- String data;
- data = "Číslo porostu;Číslo kusu;Oddenek;Dřevina;Délka;Průměr;Objem";
- for (Result result : Utils.GetResultsArray()) {
- String stool = "";
- if (result.isStool()) {
- stool = "x";
- }
- data += "\n" + result.getGrowthNumber() + ";" + String.valueOf(result.getSerialNumber()) + ";" + stool + ";" + result.getWood() + ";" + String.valueOf(result.getLength()) + ";" +
- String.valueOf(result.getDiameter()) + ";" + String.valueOf(result.getVolume());
- }
- data += "\n\nDohromady kusů: " + String.valueOf(Utils.GetResultsArray().size()) + ";Celkový objem: " + Utils.Sum().replace(",", ".") + " m³";
- return data;
- }
- private void SetListeners() {
- saveInput.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- currentDialog = "save";
- if (!Utils.HasPermissions(getContext(), PERMISSIONS)) {
- requestPermissions(PERMISSIONS, PERMISSION_ALL);
- } else {
- CreateSaveDialog();
- }
- }
- });
- loadInput.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- currentDialog = "load";
- String[] PERMISSIONS = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
- if (!Utils.HasPermissions(getContext(), PERMISSIONS)) {
- requestPermissions(PERMISSIONS, PERMISSION_ALL);
- } else {
- LoadList();
- }
- }
- });
- resetInput.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- AlertDialog.Builder resetConfirmDialog = new AlertDialog.Builder(getContext());
- resetConfirmDialog.setTitle("Reset");
- resetConfirmDialog.setMessage("Resetováním přijdete o zadaná data, chcete pokračovat?");
- resetConfirmDialog.setPositiveButton("Ano", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- Utils.GetResultsArray().clear();
- adapter.notifyDataSetChanged();
- info.setText(Utils.SumCountText());
- Utils._dataSaved = false;
- }
- });
- resetConfirmDialog.setNegativeButton("Ne", null);
- resetConfirmDialog.show();
- }
- });
- sendInput.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- currentDialog = "send";
- String[] PERMISSIONS = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
- if (!Utils.HasPermissions(getContext(), PERMISSIONS)) {
- ActivityCompat.requestPermissions(getActivity(), PERMISSIONS, PERMISSION_ALL);
- } else {
- SendList();
- }
- }
- });
- closeInput.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- if (!Utils._dataSaved) {
- AlertDialog.Builder saveDialog = new AlertDialog.Builder(getContext());
- saveDialog.setMessage("Zavřít seznam bez uložení?");
- saveDialog.setTitle("Zavřít");
- saveDialog.setPositiveButton("Ano", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Utils._actualListName = "";
- listName.setText("");
- saveInput.setText("Uložit\njako");
- closeInput.setVisibility(View.INVISIBLE);
- Utils.GetResultsArray().clear();
- adapter.notifyDataSetChanged();
- info.setText(Utils.SumCountText());
- Utils._dataSaved = true;
- }
- });
- saveDialog.setNegativeButton("Ne", null);
- saveDialog.show();
- } else {
- Utils._actualListName = "";
- listName.setText("");
- saveInput.setText("Uložit\njako");
- closeInput.setVisibility(View.INVISIBLE);
- Utils.GetResultsArray().clear();
- adapter.notifyDataSetChanged();
- info.setText(Utils.SumCountText());
- Utils._dataSaved = true;
- }
- }
- });
- }
- public void OnDeleteItemClick(int position) {
- int changeToNumber = Utils.GetResultsArray().get(0).getSerialNumber();
- Utils.GetResultsArray().remove(position);
- for (Result result : Utils.GetResultsArray()) {
- result.setSerialNumber(changeToNumber);
- changeToNumber++;
- }
- adapter.notifyDataSetChanged();
- info.setText(Utils.SumCountText());
- Utils._dataSaved = false;
- }
- private void ShowToast(String text) {
- Toast.makeText(getContext(), text, Toast.LENGTH_SHORT).show();
- }
- private void LoadList() {
- Intent intent = new Intent(getContext(), ListsActivity.class);
- startActivityForResult(intent, 1);
- }
- private void CreateSaveDialog() {
- if (Utils._actualListName.equals("")) {
- AlertDialog.Builder saveDialog = new AlertDialog.Builder(getContext());
- final EditText nameInput = new EditText(getContext());
- saveDialog.setMessage("Pojmenujte soubor");
- saveDialog.setTitle("Ukládání");
- saveDialog.setView(nameInput);
- nameInput.requestFocus();
- InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
- imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
- saveDialog.setPositiveButton("Uložit", new DialogInterface.OnClickListener() {
- public void onClick(final DialogInterface dialog, int whichButton) {
- File directory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), "kubirovaciKalkulacka");
- String fileName = nameInput.getText().toString() + ".csv";
- String regex = ".*[^-_.A-Za-z0-9 áčďéěíňóřšťúůýžÁČĎÉĚÍŇÓŘŠŤÚŮÝŽ].*";
- if (!fileName.matches(regex)) {
- File file = new File(directory, fileName);
- if (file.exists()) {
- final AlertDialog.Builder builderInner = new AlertDialog.Builder(getContext());
- builderInner.setMessage("Nelze vytvořit více souborů se stejným jménem");
- builderInner.setTitle("Upozornění");
- builderInner.setNegativeButton("Zrušit", null);
- builderInner.show();
- } else {
- Gson gson = new Gson();
- String json = gson.toJson(Utils.GetResultsArray());
- File fileComplete = Utils.CreateFile("kubirovaciKalkulacka", fileName, json);
- if (fileComplete.exists()) {
- Utils._actualListName = fileName.replace(".csv", "");
- SetListName();
- closeInput.setVisibility(View.VISIBLE);
- saveInput.setText("Uložit");
- Utils._dataSaved = true;
- ShowToast("Uloženo");
- InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
- imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
- } else {
- ShowToast("Nastala chyba při vytváření souboru");
- InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
- imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
- }
- }
- } else {
- ShowToast("Bohužel nelze takto pojmenovat soubor");
- }
- }
- });
- saveDialog.setNegativeButton("Zrušit", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
- imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
- }
- });
- saveDialog.show();
- } else {
- String fileName = Utils._actualListName + ".csv";
- Gson gson = new Gson();
- String json = gson.toJson(Utils.GetResultsArray());
- File fileComplete = Utils.CreateFile("kubirovaciKalkulacka", fileName, json);
- if (fileComplete.exists()) {
- ShowToast("Uloženo");
- Utils._dataSaved = true;
- } else {
- ShowToast("Nastala chyba při vytváření souboru");
- }
- }
- }
- private void SendList() {
- String data = makeCSV();
- String fileName;
- if (Utils._actualListName.equals("")) {
- fileName = "tabulkaDrevin.csv";
- } else {
- fileName = Utils._actualListName + ".csv";
- }
- File file = Utils.CreateFile("kubirovaciKalkulackaEmail", fileName, data);
- if (file.exists()) {
- Uri path = Uri.fromFile(file);
- Intent i = new Intent(Intent.ACTION_SEND);
- i.setType("message/rfc822");
- String recipient = sharedPreferences.getString("email", "@");
- i.putExtra(Intent.EXTRA_EMAIL, new String[]{recipient});
- i.putExtra(Intent.EXTRA_SUBJECT, "Tabulka dřevin - Kubirovaci kalkulacka");
- i.putExtra(Intent.EXTRA_STREAM, path);
- try {
- startActivity(Intent.createChooser(i, "Odeslat..."));
- } catch (android.content.ActivityNotFoundException ex) {
- ShowToast("Musíte mít nainstalovaného emailového klienta");
- }
- } else {
- ShowToast("Nastala chyba při vytváření souboru");
- }
- }
- @Override
- public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
- switch (requestCode) {
- case PERMISSION_ALL:
- Map<String, Integer> perms = new HashMap<>();
- // Initial
- perms.put(Manifest.permission.READ_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
- perms.put(Manifest.permission.WRITE_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
- // Fill with results
- for (int i = 0; i < permissions.length; i++)
- perms.put(permissions[i], grantResults[i]);
- // Check for ACCESS_FINE_LOCATION
- if (perms.get(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
- && perms.get(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
- switch (currentDialog) {
- case "save":
- CreateSaveDialog();
- break;
- case "load":
- LoadList();
- break;
- case "send":
- SendList();
- }
- } else {
- // Permission Denied
- ShowToast("Permise zamítnuta, nebudete moci ukládat seznamy");
- }
- break;
- default:
- super.onRequestPermissionsResult(requestCode, permissions, grantResults);
- }
- }
- private void SetListName() {
- adapter.notifyDataSetChanged();
- info.setText(Utils.SumCountText());
- if(!Utils._actualListName.equals("")) {
- closeInput.setVisibility(View.VISIBLE);
- saveInput.setText("Uložit");
- } else {
- saveInput.setText("Uložit\njako");
- closeInput.setVisibility(View.INVISIBLE);
- }
- if (Utils._actualListName.length() > 15) {
- String shortenListName = Utils._actualListName.substring(0, 15) + "...";
- listName.setText(shortenListName);
- } else {
- listName.setText(Utils._actualListName);
- }
- }
- @Override
- public void onActivityResult(int requestCode, int resultCode, Intent data) {
- if (requestCode == 1) {
- SetListName();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement