1 |
efrain |
1 |
package com.cesams.twogetskills.fragment;
|
|
|
2 |
|
|
|
3 |
import android.os.Bundle;
|
|
|
4 |
import android.text.TextUtils;
|
|
|
5 |
import android.util.Log;
|
|
|
6 |
import android.view.LayoutInflater;
|
|
|
7 |
import android.view.Menu;
|
|
|
8 |
import android.view.MenuInflater;
|
|
|
9 |
import android.view.View;
|
|
|
10 |
import android.view.ViewGroup;
|
|
|
11 |
import android.widget.ImageView;
|
|
|
12 |
import android.widget.TextView;
|
|
|
13 |
|
|
|
14 |
import androidx.annotation.NonNull;
|
|
|
15 |
import androidx.annotation.Nullable;
|
|
|
16 |
import androidx.fragment.app.Fragment;
|
|
|
17 |
import androidx.lifecycle.LifecycleOwner;
|
|
|
18 |
import androidx.lifecycle.Observer;
|
|
|
19 |
import androidx.lifecycle.ViewModelProvider;
|
|
|
20 |
import androidx.recyclerview.widget.LinearLayoutManager;
|
|
|
21 |
import androidx.recyclerview.widget.RecyclerView;
|
|
|
22 |
|
|
|
23 |
import com.bumptech.glide.Glide;
|
|
|
24 |
import com.bumptech.glide.load.engine.DiskCacheStrategy;
|
|
|
25 |
import com.bumptech.glide.load.model.GlideUrl;
|
|
|
26 |
import com.bumptech.glide.load.model.LazyHeaders;
|
|
|
27 |
import com.bumptech.glide.request.RequestOptions;
|
|
|
28 |
import com.cesams.twogetskills.Constants;
|
|
|
29 |
import com.cesams.twogetskills.R;
|
|
|
30 |
import com.cesams.twogetskills.adapter.UserProfileListViewAdapter;
|
|
|
31 |
import com.cesams.twogetskills.dao.CompanyDao;
|
|
|
32 |
import com.cesams.twogetskills.dao.UserExtendedDao;
|
|
|
33 |
import com.cesams.twogetskills.entity.Company;
|
|
|
34 |
import com.cesams.twogetskills.entity.UserExtended;
|
|
|
35 |
import com.cesams.twogetskills.viewdata.UserProfileViewData;
|
|
|
36 |
import com.cesams.twogetskills.library.MD5;
|
|
|
37 |
import com.cesams.twogetskills.skeleton.ITwoGetSkills;
|
|
|
38 |
import com.cesams.twogetskills.viewmodel.UserProfileViewModel;
|
|
|
39 |
|
|
|
40 |
|
|
|
41 |
import java.util.ArrayList;
|
|
|
42 |
import java.util.Calendar;
|
|
|
43 |
import java.util.List;
|
|
|
44 |
import java.util.Random;
|
|
|
45 |
import java.util.TimeZone;
|
|
|
46 |
|
|
|
47 |
|
|
|
48 |
public class UserProfileFragment extends Fragment implements LifecycleOwner {
|
|
|
49 |
private final String TAG = "C2GS - UserInfoFragment";
|
|
|
50 |
private RecyclerView listView;
|
|
|
51 |
private UserProfileListViewAdapter adapter;
|
|
|
52 |
private ITwoGetSkills iTwoGetSkills;
|
|
|
53 |
private ImageView
|
|
|
54 |
headerUserImage;
|
|
|
55 |
private TextView headerUserName;
|
|
|
56 |
private TextView headerUserEmail;
|
|
|
57 |
private UserProfileViewModel mUserProfileViewModel;
|
|
|
58 |
|
|
|
59 |
|
|
|
60 |
@Override
|
|
|
61 |
public void onCreate(@Nullable Bundle savedInstanceState) {
|
|
|
62 |
super.onCreate(savedInstanceState);
|
|
|
63 |
setHasOptionsMenu(true);
|
|
|
64 |
|
|
|
65 |
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
@Override
|
|
|
69 |
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
|
|
70 |
super.onCreateOptionsMenu(menu, inflater);
|
|
|
71 |
menu.clear();
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
@Override
|
|
|
75 |
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
|
|
76 |
|
|
|
77 |
return inflater.inflate(R.layout.fragment_userprofile, container, false);
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
@Override
|
|
|
81 |
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
|
|
82 |
super.onViewCreated(view, savedInstanceState);
|
|
|
83 |
iTwoGetSkills = (ITwoGetSkills) getActivity();
|
|
|
84 |
iTwoGetSkills.setTitleActionBar(getActivity().getString(R.string.menu_user_profile));
|
|
|
85 |
|
|
|
86 |
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
|
|
|
87 |
layoutManager.scrollToPosition(0);
|
|
|
88 |
|
|
|
89 |
mUserProfileViewModel = new ViewModelProvider(requireActivity()).get(UserProfileViewModel.class);
|
|
|
90 |
adapter = new UserProfileListViewAdapter(getActivity(), mUserProfileViewModel.getUserProfileViewDataArrayList());
|
|
|
91 |
|
|
|
92 |
Observer<ArrayList<UserProfileViewData>> UserProfileArrayListUpdateObserver = new Observer<ArrayList<UserProfileViewData>>() {
|
|
|
93 |
@Override
|
|
|
94 |
public void onChanged(ArrayList<UserProfileViewData> UserProfileViewDataArrayList) {
|
|
|
95 |
adapter.notifyDataSetChanged();
|
|
|
96 |
}
|
|
|
97 |
};
|
|
|
98 |
|
|
|
99 |
|
|
|
100 |
mUserProfileViewModel.getUserProfileViewDataMutableLiveData().observe(requireActivity(),UserProfileArrayListUpdateObserver);
|
|
|
101 |
|
|
|
102 |
|
|
|
103 |
listView = (RecyclerView) getView().findViewById(R.id.fragment_userprofile_listview);
|
|
|
104 |
listView.setAdapter(adapter);
|
|
|
105 |
listView.setLayoutManager(layoutManager);
|
|
|
106 |
|
|
|
107 |
|
|
|
108 |
headerUserImage = (ImageView) getView().findViewById(R.id.fragment_userprofile_header_user_image);
|
|
|
109 |
TimeZone timeZone = TimeZone.getTimeZone("UTC");
|
|
|
110 |
Calendar calendar = Calendar.getInstance(timeZone);
|
|
|
111 |
TimeZone tz = calendar.getTimeZone();
|
|
|
112 |
int created = (int) (calendar.getTimeInMillis() / 1000);
|
|
|
113 |
|
|
|
114 |
Random random = new Random(created);
|
|
|
115 |
int rand = 1000 + random.nextInt(8999);
|
|
|
116 |
|
|
|
117 |
|
|
|
118 |
String deviceUuid = iTwoGetSkills.getPreference().getDeviceUuid();
|
|
|
119 |
String password = iTwoGetSkills.getPreference().getPassword();
|
|
|
120 |
|
|
|
121 |
Log.d(TAG, "token = " + deviceUuid);
|
|
|
122 |
Log.d(TAG, "created = " + created);
|
|
|
123 |
Log.d(TAG, "rand = " + rand);
|
|
|
124 |
Log.d(TAG, "calc = " + password + ':' + created + ':' + rand);
|
|
|
125 |
|
|
|
126 |
String secret = MD5.generar(password + ':' + created + ':' + rand);
|
|
|
127 |
|
|
|
128 |
GlideUrl url = new GlideUrl(iTwoGetSkills.getPreference().getImage(), new LazyHeaders.Builder()
|
|
|
129 |
.addHeader(Constants.HTTP_HEADER_ACCEPT, Constants.HTTP_HEADER_ACCEPT_VALUE)
|
|
|
130 |
.addHeader(Constants.HTTP_HEADER_SECURITY_TOKEN, deviceUuid)
|
|
|
131 |
.addHeader(Constants.HTTP_HEADER_SECURITY_SECRET, secret)
|
|
|
132 |
.addHeader(Constants.HTTP_HEADER_SECURITY_CREATED, String.valueOf(created))
|
|
|
133 |
.addHeader(Constants.HTTP_HEADER_SECURITY_RAND, String.valueOf(rand))
|
|
|
134 |
.build());
|
|
|
135 |
|
|
|
136 |
RequestOptions options = new RequestOptions()
|
|
|
137 |
.diskCacheStrategy(DiskCacheStrategy.ALL);
|
|
|
138 |
|
|
|
139 |
/*
|
|
|
140 |
Glide.with(requireActivity()).load(url)
|
|
|
141 |
.thumbnail()
|
|
|
142 |
.apply(options)
|
|
|
143 |
.into(headerUserImage);
|
|
|
144 |
|
|
|
145 |
*/
|
|
|
146 |
|
|
|
147 |
Glide.with(requireActivity()).load(url)
|
|
|
148 |
.apply(options)
|
|
|
149 |
.into(headerUserImage);
|
|
|
150 |
|
|
|
151 |
headerUserName = (TextView) getView().findViewById(R.id.fragment_userprofile_header_user_name);
|
|
|
152 |
headerUserName.setText((iTwoGetSkills.getPreference().getFirstName() + " " + iTwoGetSkills.getPreference().getLastName()).trim());
|
|
|
153 |
headerUserEmail = (TextView) getView().findViewById(R.id.fragment_userprofile_header_header_user_email);
|
|
|
154 |
headerUserEmail.setText(iTwoGetSkills.getPreference().getEmail());
|
|
|
155 |
|
|
|
156 |
|
|
|
157 |
}
|
|
|
158 |
@Override
|
|
|
159 |
public void onResume() {
|
|
|
160 |
super.onResume();
|
|
|
161 |
|
|
|
162 |
Log.d(TAG, "onResume");
|
|
|
163 |
loadData();
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
|
|
|
167 |
|
|
|
168 |
|
|
|
169 |
@Override
|
|
|
170 |
public void onHiddenChanged(boolean hidden) {
|
|
|
171 |
super.onHiddenChanged(hidden);
|
|
|
172 |
|
|
|
173 |
Log.d(TAG, "onHiddenChanged : " + (hidden ? "true" : "false"));
|
|
|
174 |
|
|
|
175 |
if(!hidden) {
|
|
|
176 |
loadData();
|
|
|
177 |
}
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
private void loadData()
|
|
|
181 |
{
|
|
|
182 |
CompanyDao companyDao = iTwoGetSkills.getDatabase().getCompanyDao();
|
|
|
183 |
List<Company> companies = companyDao.selectAll();
|
|
|
184 |
|
|
|
185 |
mUserProfileViewModel.getUserProfileViewDataArrayList().clear();
|
|
|
186 |
UserProfileViewData userProfileViewData;
|
|
|
187 |
|
|
|
188 |
|
|
|
189 |
UserExtendedDao userExtendedDao = iTwoGetSkills.getDatabase().getUserExtendedDao();
|
|
|
190 |
List<UserExtended> dbUserExtendedData = userExtendedDao.selectAll();
|
|
|
191 |
|
|
|
192 |
|
|
|
193 |
int i = 0;
|
|
|
194 |
for(Company company : companies)
|
|
|
195 |
{
|
|
|
196 |
userProfileViewData = new UserProfileViewData();
|
|
|
197 |
userProfileViewData.setType(UserProfileViewData.TYPE_COMPANY);
|
|
|
198 |
userProfileViewData.setLabelOrCompanyName(company.getName());
|
|
|
199 |
userProfileViewData.setValueOrImage(company.getImage());
|
|
|
200 |
mUserProfileViewModel.getUserProfileViewDataArrayList().add(userProfileViewData);
|
|
|
201 |
|
|
|
202 |
for(UserExtended userExtended : dbUserExtendedData)
|
|
|
203 |
{
|
|
|
204 |
if(company.getUuid().equals(userExtended.getCompanyUuid()))
|
|
|
205 |
{
|
|
|
206 |
|
|
|
207 |
userProfileViewData = new UserProfileViewData();
|
|
|
208 |
userProfileViewData.setType(UserProfileViewData.TYPE_ITEM);
|
|
|
209 |
userProfileViewData.setLabelOrCompanyName(userExtended.getLabel());
|
|
|
210 |
userProfileViewData.setValueOrImage(userExtended.getValue());
|
|
|
211 |
mUserProfileViewModel.getUserProfileViewDataArrayList().add(userProfileViewData);
|
|
|
212 |
}
|
|
|
213 |
}
|
|
|
214 |
|
|
|
215 |
}
|
|
|
216 |
|
|
|
217 |
mUserProfileViewModel.getUserProfileViewDataMutableLiveData().setValue(mUserProfileViewModel.getUserProfileViewDataArrayList());
|
|
|
218 |
}
|
|
|
219 |
|
|
|
220 |
|
|
|
221 |
}
|