UserInfoAdapter.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /**
  2. * Nextcloud Android client application
  3. *
  4. * @author Mario Danic
  5. * Copyright (C) 2017 Mario Danic
  6. * Copyright (C) 2017 Nextcloud GmbH.
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. package com.owncloud.android.ui.adapter;
  22. import android.content.Context;
  23. import android.support.v7.widget.RecyclerView;
  24. import android.text.TextUtils;
  25. import android.view.LayoutInflater;
  26. import android.view.View;
  27. import android.view.ViewGroup;
  28. import android.widget.TextView;
  29. import com.owncloud.android.R;
  30. import com.owncloud.android.datamodel.StringPair;
  31. import com.owncloud.android.lib.common.UserInfo;
  32. import java.util.ArrayList;
  33. import java.util.List;
  34. import butterknife.BindView;
  35. import butterknife.ButterKnife;
  36. /**
  37. * Created by mdjanic on 24/01/2017.
  38. */
  39. public class UserInfoAdapter extends RecyclerView.Adapter<UserInfoAdapter.ViewHolder> {
  40. private UserInfo userInfo;
  41. private Context context;
  42. private List<StringPair> stringPairs;
  43. public UserInfoAdapter(UserInfo userInfo, Context context) {
  44. this.userInfo = userInfo;
  45. this.context = context;
  46. stringPairs = new ArrayList<>();
  47. getRelevantInformation();
  48. }
  49. @Override
  50. public int getItemViewType(int position) {
  51. // Just as an example, return 0 or 2 depending on position
  52. // Note that unlike in ListView adapters, types don't have to be contiguous
  53. if (position % 2 == 0) {
  54. return 0;
  55. } else {
  56. return 1;
  57. }
  58. }
  59. @Override
  60. public UserInfoAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  61. LayoutInflater inflater = LayoutInflater.from(parent.getContext());
  62. View itemView = inflater.inflate(R.layout.user_info_list_item, parent, false);
  63. if (context != null) {
  64. if (viewType == 0) {
  65. itemView.setBackgroundColor(context.getResources().getColor(R.color.white));
  66. } else {
  67. itemView.setBackgroundColor(context.getResources().getColor(R.color.white2));
  68. }
  69. }
  70. return new ViewHolder(itemView);
  71. }
  72. @Override
  73. public void onBindViewHolder(UserInfoAdapter.ViewHolder holder, int position) {
  74. StringPair currentPair = stringPairs.get(position);
  75. holder.attributeHeadlineTextView.setText(currentPair.first);
  76. holder.attributeValueTextView.setText(currentPair.second);
  77. }
  78. @Override
  79. public int getItemCount() {
  80. return stringPairs.size();
  81. }
  82. public static class ViewHolder extends RecyclerView.ViewHolder {
  83. @BindView(R.id.attribute_headline_tv)
  84. TextView attributeHeadlineTextView;
  85. @BindView(R.id.attribute_value_tv)
  86. TextView attributeValueTextView;
  87. public ViewHolder(View itemView) {
  88. super(itemView);
  89. ButterKnife.bind(this, itemView);
  90. }
  91. }
  92. private void getRelevantInformation() {
  93. if (!TextUtils.isEmpty(userInfo.getDisplayName())) {
  94. if (context != null) {
  95. stringPairs.add(new StringPair(context.getResources().getString(R.string.user_info_full_name),
  96. userInfo.getDisplayName()));
  97. }
  98. }
  99. if (!TextUtils.isEmpty((userInfo.getEmail()))) {
  100. if (context != null) {
  101. stringPairs.add(new StringPair(context.getResources().getString(R.string.user_info_email),
  102. userInfo.getEmail()));
  103. }
  104. }
  105. if (!TextUtils.isEmpty(userInfo.getPhone())) {
  106. if (context != null) {
  107. stringPairs.add(new StringPair(context.getResources().getString(R.string.user_info_phone),
  108. userInfo.getPhone()));
  109. }
  110. }
  111. if (!TextUtils.isEmpty(userInfo.getAddress())) {
  112. if (context != null) {
  113. stringPairs.add(new StringPair(context.getResources().getString(R.string.user_info_address),
  114. userInfo.getAddress()));
  115. }
  116. }
  117. if (!TextUtils.isEmpty(userInfo.getWebpage())) {
  118. if (context != null) {
  119. stringPairs.add(new StringPair(context.getResources().getString(R.string.user_info_website),
  120. userInfo.getWebpage()));
  121. }
  122. }
  123. if (!TextUtils.isEmpty(userInfo.getTwitter())) {
  124. if (context != null) {
  125. stringPairs.add(new StringPair(context.getResources().getString(R.string.user_info_twitter),
  126. userInfo.getTwitter()));
  127. }
  128. }
  129. }
  130. }