Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Main
- public class MainActivity extends Activity {
- private EditText name;
- private EditText password;
- SharedPreferences prefs;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- /////////////////////////////////////////////////////////////////////////
- prefs=getApplicationContext().getSharedPreferences("userPrefs",MODE_PRIVATE);
- name =(EditText)findViewById(R.id.userName);
- password =(EditText)findViewById(R.id.pass);
- if(prefs.getString("logd","logd").equals("logd")) //If the user is already logdin
- {
- Intent HomePage = new Intent(this, HomePage.class); //Pass diractly to the main menu
- HomePage.putExtra("Name", prefs.getString("userName", "exist").toString()); //Show "Hello "+ name of the user in the main menu
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////
- public void signUp(View v)
- {
- Intent signup = new Intent(this,Register.class);
- this.startActivity(signup);
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////
- public void logIn(View v)
- {
- if (prefs.contains(name.getText().toString())|| prefs.contains(password.getText().toString()))
- {
- Intent IhomPage = new Intent(this, HomePage.class);
- IhomPage.putExtra("Name",name.getText().toString());
- SharedPreferences.Editor editor=prefs.edit();
- editor.putString("logd", "logd");
- startActivity(IhomPage);
- }
- else
- {
- Toast.makeText(this, "email or password are incorrect", Toast.LENGTH_LONG).show();
- }
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////
- //home page
- public class HomePage extends AppCompatActivity {
- TextView hom ;
- LinearLayout myMain;
- LinearLayout top;
- LinearLayout listLayout;
- Context context;
- ListView lv1;
- String userName;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_home_page);
- hom = (TextView)findViewById(R.id.helow);
- hom.setText("Hello " + getIntent().getStringExtra("Name"));
- userName= getIntent().getStringExtra("Name");
- //--------------------------------------------------------------------------------------------------
- myMain = (LinearLayout) findViewById(R.id.hom);
- listLayout = (LinearLayout) findViewById(R.id.listLayout);
- top = (LinearLayout) findViewById(R.id.top);
- lv1=(ListView)findViewById(R.id.lv1);
- context = this;
- //created new objects
- Button myButton = new Button(this);
- TaskAdapter adapter = new TaskAdapter(this, userName); //******
- //link adapter to ListView
- lv1.setAdapter(adapter);
- //set properties for new Button
- myButton.setText("+ Add new task");
- myButton.setWidth(listLayout.getWidth());
- myButton.setTextSize(30);
- myButton.setBackgroundColor(Color.GRAY);
- myButton.setGravity(Gravity.CENTER_HORIZONTAL);
- myButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v)
- {
- Intent IaddTask = new Intent(context,AddTaskActivity.class);
- IaddTask.putExtra("userName",getIntent().getStringExtra("Name"));
- startActivity(IaddTask);
- }
- });
- //add new elements to our LinearLayout
- myMain.addView(myButton);
- }
- //__________________________________________________________________________________________________
- public void logUot(View v)
- {
- SharedPreferences prefs=getApplicationContext().getSharedPreferences("myPref",MODE_PRIVATE);
- SharedPreferences.Editor editor=prefs.edit();
- editor.remove("logd");
- editor.commit();
- finish();
- }
- }
- //TaskAdapter
- public class TaskAdapter extends BaseAdapter
- {
- //SET CONTEXT
- Context context;
- String userName;
- List<utlShared> userTasks;
- public TaskAdapter(Context context,String userName)
- {
- this.context=context;
- this.userName=userName;
- //CREATE utlShared instance
- utlShared spTasks = new utlShared(context,userName);
- //get all task into instance
- userTasks = spTasks.getAllTasks();
- }
- @Override
- public int getCount() {
- //RETURN SIZE OF ALL TASKS
- return userTasks.size();
- }
- @Override
- public Object getItem(int position) {
- return null;
- }
- @Override
- public long getItemId(int position) {
- return 0;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- //CREATE NEW TEXT VIEW
- TextView taskName = new TextView(context);
- //PUT TASK NAME INTO TEXT VIEW
- taskName.setText(userTasks.get(position).taskName);
- //SET COLOR OF TASK
- taskName.setTextColor(userTasks.get(position).taskDone.equals("1")?Color.GREEN: Color.RED);
- //RETURN TEXTVIEW
- return taskName;
- }
- }
- //utlShared
- public class utlShared
- {
- private Context context;
- private SharedPreferences userPref;
- private SharedPreferences.Editor editor;
- public String taskName;
- public Boolean taskDone;
- private String userName;
- private List<utlShared> myTasks;
- //contructor for adding name to list
- public utlShared(String taskName, Boolean taskDone)
- {
- this.taskName=taskName;
- this.taskDone=taskDone;
- }
- //contructor for new utlShared instance
- public utlShared(Context context, String userName)
- {
- //get context
- this.context = context;
- //get username for shared preferances file
- this.userName=userName;
- userPref=context.getSharedPreferences(userName,Context.MODE_PRIVATE);
- editor = userPref.edit();
- //create new list
- List<utlShared> myTasks = new ArrayList<>();
- //get all tasks to list
- getTasks();
- }
- public void newTask(String taskName)
- {
- //PUT NEW TASK INTO SHARED PREFERANCES
- editor.putString(taskName,"0");
- editor.commit();
- //PUT TASK INTO LIST
- myTasks.add(new utlShared(taskName, false));
- }
- public void removeTask(String taskName)
- {
- //REMOVE TASK FROM SHARED PREFERANCES
- editor.remove(taskName);
- editor.commit();
- //REMOVE TASK FROM LIST
- myTasks.remove(taskName);
- }
- public void doneTask(String taskName, boolean taskDone)
- {
- //CHANGE TASK IN SHARED PREFERANCES
- editor.putString(taskName, taskDone ? "1" : "0");
- editor.commit();
- //REMOVE TASK FROM LIST
- myTasks.remove(taskName);
- //ADD TASK WITH NEW TASK DONE INTO LIST
- myTasks.add(new utlShared(taskName, taskDone));
- }
- public void getTasks()
- {
- //GET ALL TASK FROM SHARED PREFERANCES
- Map<String,?> tasks= userPref.getAll();
- for(Map.Entry<String,?> singleTask:tasks.entrySet())
- {
- //INSERT TASK INTO LIST
- myTasks.add(new utlShared(singleTask.getKey(),singleTask.getValue().equals("1")));
- }
- }
- public List<utlShared> getAllTasks()
- {
- //GETTER RETURN TASK LIST
- return myTasks;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement