package com.utbm.lo53.whereami;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class Preference extends AppCompatActivity
{
    EditText ip_address;
    EditText port_number;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_preference);

        //get EditTexts
        ip_address = (EditText) findViewById(R.id.ip_address);
        port_number = (EditText) findViewById(R.id.port_number);

        //check if there is a saved data in preferences
        SharedPreferences sharedPref = getSharedPreferences("preferences", MODE_PRIVATE);
        if (sharedPref.getString("ip_address", "") != "")
        {
            ip_address.setText(sharedPref.getString("ip_address", ""));
        }
        if (sharedPref.getString("port_number", "") != "")
        {
            port_number.setText(sharedPref.getString("port_number", ""));
        }
    }
    public void onClickSavePreferences(View view)
    {
        SharedPreferences sharedPref = getSharedPreferences("preferences", MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putString("ip_address",ip_address.getText().toString() );
        editor.putString("port_number",port_number.getText().toString() );
        editor.commit();
        Toast.makeText(getApplicationContext(),"Saved successfully", Toast.LENGTH_LONG).show();
    }
}
