diff --git a/app/src/main/java/org/mian/gitnex/activities/MainActivity.java b/app/src/main/java/org/mian/gitnex/activities/MainActivity.java index 4dcb7645..5187649c 100644 --- a/app/src/main/java/org/mian/gitnex/activities/MainActivity.java +++ b/app/src/main/java/org/mian/gitnex/activities/MainActivity.java @@ -18,15 +18,20 @@ import android.widget.TextView; import androidx.annotation.NonNull; import androidx.appcompat.app.ActionBarDrawerToggle; import androidx.appcompat.app.AlertDialog; +import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; import androidx.core.view.GravityCompat; import androidx.drawerlayout.widget.DrawerLayout; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; +import androidx.recyclerview.widget.RecyclerView; import com.google.android.material.navigation.NavigationView; import org.mian.gitnex.R; +import org.mian.gitnex.adapters.UserAccountsNavAdapter; import org.mian.gitnex.clients.PicassoService; import org.mian.gitnex.clients.RetrofitClient; +import org.mian.gitnex.database.api.UserAccountsApi; +import org.mian.gitnex.database.models.UserAccount; import org.mian.gitnex.fragments.AboutFragment; import org.mian.gitnex.fragments.AdministrationFragment; import org.mian.gitnex.fragments.BottomSheetDraftsFragment; @@ -51,6 +56,8 @@ import org.mian.gitnex.helpers.Toasty; import org.mian.gitnex.helpers.Version; import org.mian.gitnex.models.GiteaVersion; import org.mian.gitnex.models.UserInfo; +import java.util.ArrayList; +import java.util.List; import java.util.Objects; import eightbitlab.com.blurview.BlurView; import eightbitlab.com.blurview.RenderScriptBlur; @@ -218,6 +225,26 @@ public class MainActivity extends BaseActivity implements NavigationView.OnNavig userAvatarBackground = hView.findViewById(R.id.userAvatarBackground); navHeaderFrame = hView.findViewById(R.id.navHeaderFrame); + List userAccountsList; + userAccountsList = new ArrayList<>(); + UserAccountsApi userAccountsApi; + userAccountsApi = new UserAccountsApi(ctx); + + RecyclerView navRecyclerViewUserAccounts = hView.findViewById(R.id.navRecyclerViewUserAccounts); + UserAccountsNavAdapter adapterUserAccounts; + + adapterUserAccounts = new UserAccountsNavAdapter(ctx, userAccountsList, drawer, toolbarTitle); + + userAccountsApi.getAllAccounts().observe((AppCompatActivity) ctx, userAccounts -> { + + if(userAccounts.size() > 0) { + + userAccountsList.addAll(userAccounts); + navRecyclerViewUserAccounts.setAdapter(adapterUserAccounts); + } + + }); + userEmail.setTypeface(myTypeface); userFullName.setTypeface(myTypeface); @@ -285,14 +312,6 @@ public class MainActivity extends BaseActivity implements NavigationView.OnNavig }); - ImageView userAccounts = hView.findViewById(R.id.userAccounts); - userAccounts.setOnClickListener(v -> { - - toolbarTitle.setText(getResources().getString(R.string.pageTitleUserAccounts)); - getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new UserAccountsFragment()).commit(); - drawer.closeDrawers(); - }); - toggle.syncState(); toolbar.setNavigationIcon(R.drawable.ic_menu); diff --git a/app/src/main/java/org/mian/gitnex/adapters/UserAccountsListDialogAdapter.java b/app/src/main/java/org/mian/gitnex/adapters/UserAccountsListDialogAdapter.java new file mode 100644 index 00000000..eb73f697 --- /dev/null +++ b/app/src/main/java/org/mian/gitnex/adapters/UserAccountsListDialogAdapter.java @@ -0,0 +1,72 @@ +package org.mian.gitnex.adapters; + +import android.annotation.SuppressLint; +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.ImageView; +import android.widget.TextView; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import org.mian.gitnex.R; +import org.mian.gitnex.clients.PicassoService; +import org.mian.gitnex.database.models.UserAccount; +import org.mian.gitnex.helpers.RoundedTransformation; +import org.mian.gitnex.helpers.TinyDB; +import java.util.List; +import io.mikael.urlbuilder.UrlBuilder; + +/** + * Author M M Arif + */ + +public class UserAccountsListDialogAdapter extends ArrayAdapter { + + private final Context mCtx; + private final List userAccountsList; + + public UserAccountsListDialogAdapter(@NonNull Context mCtx, int resource, @NonNull List objects) { + + super(mCtx, resource, objects); + userAccountsList = objects; + this.mCtx = mCtx; + } + + @SuppressLint("ViewHolder") + @NonNull + @Override + public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { + + LayoutInflater inflater = (LayoutInflater) mCtx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + assert inflater != null; + View rowView = inflater.inflate(R.layout.custom_user_accounts_list, parent, false); + + TinyDB tinyDB = new TinyDB(mCtx); + + ImageView profileImage = rowView.findViewById(R.id.profileImage); + TextView userName = rowView.findViewById(R.id.userName); + TextView accountUrl = rowView.findViewById(R.id.accountUrl); + ImageView activeAccount = rowView.findViewById(R.id.activeAccount); + + UserAccount currentItem = userAccountsList.get(position); + + String url = UrlBuilder.fromString(currentItem.getInstanceUrl()) + .withPath("/") + .toString(); + + userName.setText(currentItem.getUserName()); + accountUrl.setText(url); + + if(tinyDB.getInt("currentActiveAccountId") == currentItem.getAccountId()) { + activeAccount.setVisibility(View.VISIBLE); + } + + PicassoService + .getInstance(mCtx).get().load(url + "img/favicon.png").placeholder(R.drawable.loader_animated).transform(new RoundedTransformation(8, 0)).resize(120, 120).centerCrop().into(profileImage); + + return rowView; + } + +} diff --git a/app/src/main/java/org/mian/gitnex/adapters/UserAccountsNavAdapter.java b/app/src/main/java/org/mian/gitnex/adapters/UserAccountsNavAdapter.java new file mode 100644 index 00000000..eafb25d5 --- /dev/null +++ b/app/src/main/java/org/mian/gitnex/adapters/UserAccountsNavAdapter.java @@ -0,0 +1,147 @@ +package org.mian.gitnex.adapters; + +import android.annotation.SuppressLint; +import android.app.Activity; +import android.app.Dialog; +import android.content.Context; +import android.graphics.Color; +import android.graphics.drawable.ColorDrawable; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.ListView; +import android.widget.TextView; +import androidx.annotation.NonNull; +import androidx.appcompat.app.AppCompatActivity; +import androidx.drawerlayout.widget.DrawerLayout; +import androidx.recyclerview.widget.RecyclerView; +import org.mian.gitnex.R; +import org.mian.gitnex.clients.PicassoService; +import org.mian.gitnex.database.api.UserAccountsApi; +import org.mian.gitnex.database.models.UserAccount; +import org.mian.gitnex.fragments.UserAccountsFragment; +import org.mian.gitnex.helpers.RoundedTransformation; +import org.mian.gitnex.helpers.TinyDB; +import org.mian.gitnex.helpers.Toasty; +import java.util.List; +import io.mikael.urlbuilder.UrlBuilder; + +/** + * Author M M Arif + */ + +public class UserAccountsNavAdapter extends RecyclerView.Adapter { + + private static DrawerLayout drawer; + private List userAccountsList; + private Context mCtx; + private TextView toolbarTitle; + + public UserAccountsNavAdapter(Context mCtx, List userAccountsListMain, DrawerLayout drawerLayout, TextView toolbarTitle) { + + this.mCtx = mCtx; + this.userAccountsList = userAccountsListMain; + drawer = drawerLayout; + this.toolbarTitle = toolbarTitle; + } + + class UserAccountsViewHolder extends RecyclerView.ViewHolder { + + private ImageView userAccountAvatar; + + private UserAccountsViewHolder(View itemView) { + + super(itemView); + + userAccountAvatar = itemView.findViewById(R.id.userAccountAvatar); + + itemView.setOnClickListener(item -> { + + customDialogUserAccountsList(userAccountsList); + drawer.closeDrawers(); + }); + + } + + } + + @NonNull + @Override + public UserAccountsNavAdapter.UserAccountsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + + View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.nav_user_accounts, parent, false); + return new UserAccountsViewHolder(v); + } + + @SuppressLint("DefaultLocale") + @Override + public void onBindViewHolder(@NonNull UserAccountsNavAdapter.UserAccountsViewHolder holder, int position) { + + UserAccount currentItem = userAccountsList.get(position); + + String url = UrlBuilder.fromString(currentItem.getInstanceUrl()) + .withPath("/") + .toString(); + + PicassoService + .getInstance(mCtx).get().load(url + "img/favicon.png").placeholder(R.drawable.loader_animated).transform(new RoundedTransformation(8, 0)).resize(120, 120).centerCrop().into(holder.userAccountAvatar); + } + + @Override + public int getItemCount() { + + return userAccountsList.size(); + } + + private void customDialogUserAccountsList(List allAccountsList) { + + TinyDB tinyDB = new TinyDB(mCtx); + Dialog dialog = new Dialog(mCtx); + dialog.setContentView(R.layout.custom_user_accounts_dialog); + + ListView listView = dialog.findViewById(R.id.accountsList); + TextView manageAccounts = dialog.findViewById(R.id.manageAccounts); + + if (dialog.getWindow() != null) { + dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); + } + + manageAccounts.setOnClickListener(item -> { + + toolbarTitle.setText(mCtx.getResources().getString(R.string.pageTitleUserAccounts)); + AppCompatActivity activity = (AppCompatActivity) mCtx; + activity.getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new UserAccountsFragment()).commit(); + dialog.dismiss(); + }); + + UserAccountsListDialogAdapter arrayAdapter = new UserAccountsListDialogAdapter(mCtx, R.layout.custom_user_accounts_list, allAccountsList); + listView.setAdapter(arrayAdapter); + + listView.setOnItemClickListener((adapterView, view, which, l) -> { + + String accountNameSwitch = allAccountsList.get(which).getAccountName(); + UserAccountsApi userAccountsApi = new UserAccountsApi(mCtx); + UserAccount userAccount = userAccountsApi.getAccountData(accountNameSwitch); + + if(tinyDB.getInt("currentActiveAccountId") != userAccount.getAccountId()) { + + String url = UrlBuilder.fromString(userAccount.getInstanceUrl()) + .withPath("/") + .toString(); + + tinyDB.putString("loginUid", userAccount.getUserName()); + tinyDB.putString("userLogin", userAccount.getUserName()); + tinyDB.putString(userAccount.getUserName() + "-token", userAccount.getToken()); + tinyDB.putString("instanceUrl", userAccount.getInstanceUrl()); + tinyDB.putInt("currentActiveAccountId", userAccount.getAccountId()); + + Toasty.success(mCtx, mCtx.getResources().getString(R.string.switchAccountSuccess, userAccount.getUserName(), url)); + ((Activity) mCtx).recreate(); + dialog.dismiss(); + } + }); + dialog.show(); + } + +} diff --git a/app/src/main/res/drawable/shape_custom_dialog.xml b/app/src/main/res/drawable/shape_custom_dialog.xml new file mode 100644 index 00000000..1e0f424e --- /dev/null +++ b/app/src/main/res/drawable/shape_custom_dialog.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/app/src/main/res/layout/custom_user_accounts_dialog.xml b/app/src/main/res/layout/custom_user_accounts_dialog.xml new file mode 100644 index 00000000..1a6a660e --- /dev/null +++ b/app/src/main/res/layout/custom_user_accounts_dialog.xml @@ -0,0 +1,40 @@ + + + + + + + + + + + +