Vous êtes sur la page 1sur 6

package net.simplifiedcoding.retrofitexample.

fragments;

import android.Manifest;
import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import net.simplifiedcoding.retrofitexample.R;
import net.simplifiedcoding.retrofitexample.api.APIUrl;
import net.simplifiedcoding.retrofitexample.api.AbsenAPI;
import net.simplifiedcoding.retrofitexample.helper.SharedPrefManager;
import net.simplifiedcoding.retrofitexample.models.LemburModel;
import net.simplifiedcoding.retrofitexample.models.Result;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static android.content.Context.LOCATION_SERVICE;

/**
* A simple {@link Fragment} subclass.
*/
public class LemburFragment extends Fragment {

MapView mMapView;
private GoogleMap googleMap;
LocationManager locationManager;
private static final int REQUEST_LOCATION = 1;
TextView tanggal,jam;
EditText keterangan;
Button Lembur;
SimpleDateFormat simpledateformat1,simpledateformat;
Calendar calander;
private RadioGroup radioLembur;
private RadioButton radioLemburButton;
public static final String KEY_NIP = "nip.KEY";
public static final String KEY_TANGGAL_LEMBUR = "tanggallembur.KEY";
public static final String KEY_JAM_MULAI_LEMBUR = "jammulailembur.KEY";
public static final String KEY_KETERANGAN_LEMBUR = "keteranganlembur.KEY";

public LemburFragment() {
// Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View rootView = inflater.inflate(R.layout.fragment_lembur,
container, false);
ActivityCompat.requestPermissions(getActivity(), new String[]
{android.Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);

tanggal = (TextView) rootView.findViewById(R.id.Tanggal);


jam = (TextView) rootView.findViewById(R.id.editTime);
keterangan = (EditText)
rootView.findViewById(R.id.editTextKeterangan);
Lembur = (Button) rootView.findViewById(R.id.ButtonLembur);
radioLembur = (RadioGroup) rootView.findViewById(R.id.radioLembur)
;

Lembur.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int selectedId = radioLembur.getCheckedRadioButtonId();

radioLemburButton = (RadioButton)
rootView.findViewById(selectedId);
if(radioLemburButton.getText().equals("Mulai Lembur")){
// Lembur();
}else if(radioLemburButton.getText().equals("Selesai Lembur")){
// Le
}

}
});

calander = Calendar.getInstance();
simpledateformat = new SimpleDateFormat("HH:mm:ss");
simpledateformat1 = new SimpleDateFormat("dd-MMMM-YYYY");
String tanggal1 = simpledateformat1.format(calander.getTime());
String jam1 = simpledateformat.format(calander.getTime());

tanggal.setText(tanggal1);
jam.setText(jam1);

mMapView = (MapView) rootView.findViewById(R.id.mapView);


mMapView.onCreate(savedInstanceState);
mMapView.onResume(); // needed to get the map to display immediately

locationManager = (LocationManager)
getActivity().getSystemService(LOCATION_SERVICE);

try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}

mMapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap mMap) {
googleMap = mMap;

if (ActivityCompat.checkSelfPermission(getActivity(),
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(getActivity(),
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

return;
}
googleMap.setMyLocationEnabled(true);
// Getting LocationManager object from System Service
LOCATION_SERVICE
LocationManager locationManager = (LocationManager)
getActivity().getSystemService(LOCATION_SERVICE);

// Creating a criteria object to retrieve provider


Criteria criteria = new Criteria();

// Getting the name of the best provider


String provider = locationManager.getBestProvider(criteria, true);

// Getting Current Location


Location location = locationManager.getLastKnownLocation(provider);

if (location != null) {
// Getting latitude of the current location
double latitude = location.getLatitude();

// Getting longitude of the current location


double longitude = location.getLongitude();

// Creating a LatLng object for the current location


LatLng latLng = new LatLng(latitude, longitude);

LatLng myPosition = new LatLng(latitude, longitude);

// LatLng latLng = new


LatLng(location.getLatitude(),location.getLongitude());

Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(getActivity(), Locale.getDefault());

try {
addresses = geocoder.getFromLocation(latitude, longitude,
1); // Here 1 represent max location result to returned, by documents it
recommended 1 to 5
String address = addresses.get(0).getAddressLine(0); // If
any additional address line present than only, check with max available address
lines by getMaxAddressLineIndex()
String city = addresses.get(0).getLocality();
String state = addresses.get(0).getAdminArea();
String country = addresses.get(0).getCountryName();
String postalCode = addresses.get(0).getPostalCode();
String knownName = addresses.get(0).getFeatureName();

googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(myPosition, 15));
googleMap.addMarker(new
MarkerOptions().position(myPosition).title(address));
} catch (IOException e) {
e.printStackTrace();
}

}
}
});
return rootView;
}

// private void Lembur() {


// //defining a progress dialog to show while signing up
// final ProgressDialog progressDialog = new ProgressDialog(getActivity());
// progressDialog.setMessage("Prosessingg...");
// progressDialog.show();
//
// final String date = new SimpleDateFormat("yyyy-MM-dd",
Locale.getDefault()).format(new Date());
// String time = new SimpleDateFormat("HH:mm:ss",
Locale.getDefault()).format(new Date());
// String Keterangan = keterangan.getText().toString().trim();
//
// //building retrofit object
// Retrofit retrofit = new Retrofit.Builder()
// .baseUrl(APIUrl.BASE_URL_1)
// .addConverterFactory(GsonConverterFactory.create())
// .build();
// //Defining retrofit api service
// AbsenAPI service = retrofit.create(AbsenAPI.class);
//
// //Defining the user object as we need to pass it with the call
// LemburModel Lembur = new
LemburModel(SharedPrefManager.getInstance(getActivity()).getUser().getNip(),date,
// time, null,Keterangan);
//
//// SharedPreferences sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(getActivity());
//// SharedPreferences.Editor edit = sharedPreferences.edit();
//// edit.clear();
//// edit.putString(KEY_NIP,
SharedPrefManager.getInstance(getContext()).getUser().getNip());
//// edit.putString(KEY_TANGGAL_LEMBUR, date);
//// edit.putString(KEY_JAM_MULAI_LEMBUR, time);
//// edit.putString(KEY_KETERANGAN_LEMBUR, Keterangan);
//// edit.commit();
//
// //defining the call
// Call<Result> call = service.sendLembur(
// Lembur.getNip(),
// Lembur.getTanggal_lembur(),
// Lembur.getJam_mulai(),
// Lembur.getJam_selesai(),
// Lembur.getKeterangan()
//
// );
//
// //calling the api
// call.enqueue(new Callback<Result>() {
// @Override
// public void onResponse(Call<Result> call, Response<Result> response)
{
// //hiding progress dialog
// progressDialog.dismiss();
// Log.d("RETRO", "response : " + response.body().getKode());
// int kode = response.body().getKode();
// if(kode==1)
// {
// Toast.makeText(getContext(), response.body().getMessage(),
Toast.LENGTH_LONG).show();
// }else
// {
// Toast.makeText(getContext(),response.body().getMessage(),
Toast.LENGTH_LONG).show();
// }
// }
// @Override
// public void onFailure(Call<Result> call, Throwable t) {
// progressDialog.dismiss();
// Log.d("RETRO", "Falure : " + "Gagal Mengirim Request");
// }
// });
//
// }
}

Vous aimerez peut-être aussi