diff --git a/app/src/main/java/com/example/bondoman/ui/settings/SettingsFragment.kt b/app/src/main/java/com/example/bondoman/ui/settings/SettingsFragment.kt index 86d783020f0a2b1d5938e5247cb54c81545717f7..71df1434c4e4967ae12336453b55f266e5bdb1f0 100644 --- a/app/src/main/java/com/example/bondoman/ui/settings/SettingsFragment.kt +++ b/app/src/main/java/com/example/bondoman/ui/settings/SettingsFragment.kt @@ -1,5 +1,6 @@ package com.example.bondoman.ui.settings +import android.content.Context import android.content.Intent import android.os.Bundle import androidx.fragment.app.Fragment @@ -8,21 +9,14 @@ import android.view.View import android.view.ViewGroup import android.widget.Button import android.widget.Toast -import com.example.bondoman.MainActivity +import com.example.bondoman.LoginActivity import com.example.bondoman.R -import com.example.bondoman.databinding.FragmentChartBinding import com.example.bondoman.databinding.FragmentSettingsBinding +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext -// TODO: Rename parameter arguments, choose names that match -// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER -private const val ARG_PARAM1 = "param1" -private const val ARG_PARAM2 = "param2" - -/** - * A simple [Fragment] subclass. - * Use the [SettingsFragment.newInstance] factory method to - * create an instance of this fragment. - */ class SettingsFragment : Fragment() { private lateinit var binding: FragmentSettingsBinding private lateinit var saveButton: Button @@ -55,32 +49,62 @@ class SettingsFragment : Fragment() { } sendButton.setOnClickListener { - // Create an Intent with ACTION_SEND to send an email - val emailIntent = Intent(Intent.ACTION_SEND) - - // Set the type to 'message/rfc822' MIME type, which is the standard MIME type for email messages - emailIntent.type = "message/rfc822" + sendMail() + } - // Set recipients (to whom you want to send the email) - emailIntent.putExtra(Intent.EXTRA_EMAIL, arrayOf("13521106@std.stei.itb.ac.id")) + logoutButton.setOnClickListener { + CoroutineScope(Dispatchers.IO).launch { + withContext(Dispatchers.Main){ + val sharedPreferences = requireActivity().getSharedPreferences( + "BondoMan", + Context.MODE_PRIVATE + ) + val editor = sharedPreferences.edit() + editor.remove("token") + editor.remove("email") + editor.apply() + startActivity(Intent(activity, LoginActivity::class.java)) + activity?.finish() + } + } + Toast.makeText(requireContext(), "Logged Out",Toast.LENGTH_SHORT).show() + } - // Set subject of the email - emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Transaction Data") + } - // Set body of the email - emailIntent.putExtra(Intent.EXTRA_TEXT, "This is your data transaction") + fun sendMail(){ + val sharedPreferences = requireActivity().getSharedPreferences( + "BondoMan", + Context.MODE_PRIVATE + ) - // Start the activity with the emailIntent - startActivity(Intent.createChooser(emailIntent, "Send Email")) + val email = sharedPreferences.getString("email","") - // Display a toast indicating the action - Toast.makeText(requireContext(), "Sending email...", Toast.LENGTH_SHORT).show() + if(email == ""){ + Toast.makeText(requireContext(), "Email not found",Toast.LENGTH_SHORT).show() + return } - logoutButton.setOnClickListener { - Toast.makeText(requireContext(), "logout",Toast.LENGTH_SHORT).show() - } + // Create an Intent with ACTION_SEND to send an email + val emailIntent = Intent(Intent.ACTION_SEND) + + // Set the type to 'message/rfc822' MIME type, which is the standard MIME type for email messages + emailIntent.type = "message/rfc822" + + // Set recipients (to whom you want to send the email) + emailIntent.putExtra(Intent.EXTRA_EMAIL, arrayOf(email)) + + // Set subject of the email + emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Transaction Data") + + // Set body of the email + emailIntent.putExtra(Intent.EXTRA_TEXT, "This is your data transaction") + + // Start the activity with the emailIntent + startActivity(Intent.createChooser(emailIntent, "Send Email")) + // Display a toast indicating the action + Toast.makeText(requireContext(), "Sending email...", Toast.LENGTH_SHORT).show() } } \ No newline at end of file