PageRenderTime 63ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/EY-Android/app/src/main/java/rs/ey/taxserbia/activities/app/pages/types/contact/ContactFormTemplate.java

https://bitbucket.org/darkokostic/eyapp
Java | 368 lines | 318 code | 50 blank | 0 comment | 19 complexity | b26a37e935a89a0e7989d0babacc3872 MD5 | raw file
  1. package rs.ey.taxserbia.activities.app.pages.types.contact;
  2. import android.content.Context;
  3. import android.content.Intent;
  4. import android.graphics.PorterDuff;
  5. import android.os.Build;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.os.Bundle;
  8. import android.support.v7.widget.LinearLayoutManager;
  9. import android.support.v7.widget.RecyclerView;
  10. import android.support.v7.widget.Toolbar;
  11. import android.text.Editable;
  12. import android.text.TextWatcher;
  13. import android.util.Log;
  14. import android.view.Menu;
  15. import android.view.MenuItem;
  16. import android.view.View;
  17. import android.widget.EditText;
  18. import android.widget.FrameLayout;
  19. import android.widget.ImageView;
  20. import android.widget.LinearLayout;
  21. import android.widget.ProgressBar;
  22. import android.widget.Toast;
  23. import com.jaeger.library.StatusBarUtil;
  24. import org.json.JSONArray;
  25. import org.json.JSONException;
  26. import org.json.JSONObject;
  27. import java.util.ArrayList;
  28. import java.util.regex.Pattern;
  29. import rs.ey.taxserbia.EyApplication;
  30. import rs.ey.taxserbia.R;
  31. import rs.ey.taxserbia.activities.app.HomeActivity;
  32. import rs.ey.taxserbia.activities.app.home.pages.SearchMoreActivity;
  33. import rs.ey.taxserbia.activities.app.home.pages.SettingsActivity;
  34. import rs.ey.taxserbia.activities.app.pages.types.ExpandableListTemplate;
  35. import rs.ey.taxserbia.activities.app.pages.types.AboutPageTemplate;
  36. import rs.ey.taxserbia.adapters.HomeSearchResultListAdapter;
  37. import rs.ey.taxserbia.entities.HomeSearchResult;
  38. import rs.ey.taxserbia.helpers.CheckInternetConnection;
  39. import rs.ey.taxserbia.helpers.LanguageHelper;
  40. import rs.ey.taxserbia.services.http.Responsable;
  41. import rs.ey.taxserbia.services.http.VolleyService;
  42. public class ContactFormTemplate extends AppCompatActivity implements Responsable {
  43. private LinearLayout layoutSearch;
  44. private LinearLayout layoutSettings;
  45. private LinearLayout btnSettingsActive;
  46. private LinearLayout btnSearchClose;
  47. private LinearLayout layoutSettingsItemsBg;
  48. private ArrayList<HomeSearchResult> searchResultsList;
  49. private RecyclerView searchResultsListRecyclerView;
  50. private ImageView btnSearchMore;
  51. private EditText etSearch;
  52. private FrameLayout btnFaqSettings;
  53. private FrameLayout btnSettingsMenu;
  54. private FrameLayout btnAboutMenu;
  55. private ImageView btnBack;
  56. private ImageView imgLogo;
  57. private LinearLayout loadingLayout;
  58. private ProgressBar progressBar;
  59. private EditText etName;
  60. private EditText etLastName;
  61. private EditText etEmail;
  62. private EditText etMsg;
  63. private LinearLayout btnSend;
  64. private VolleyService httpService;
  65. private final static String HTTP_URL = "https://ey.nbgcreator.com/api/contacts";
  66. private final static String HTTP_SEARCH_URL = "https://ey.nbgcreator.com/api/news/search";
  67. private final static int REQUEST_TYPE_SEARCH = 2;
  68. private final static int REQUEST_TYPE_BASIC = 1;
  69. private static final Pattern VALID_EMAIL_ADDRESS_REGEX = Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE);
  70. @Override
  71. protected void onCreate(Bundle savedInstanceState) {
  72. super.onCreate(savedInstanceState);
  73. setContentView(R.layout.template_contact_form);
  74. StatusBarUtil.setColor(ContactFormTemplate.this, getResources().getColor(R.color.status_bar_intro));
  75. initComponents();
  76. initOnClickListeners();
  77. }
  78. @Override
  79. protected void attachBaseContext(Context newBase) {
  80. if(Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1) {
  81. super.attachBaseContext(LanguageHelper.wrap(newBase, EyApplication.langShort));
  82. } else {
  83. super.attachBaseContext(newBase);
  84. }
  85. }
  86. private void initComponents() {
  87. Toolbar toolbar = findViewById(R.id.home_toolbar);
  88. setSupportActionBar(toolbar);
  89. setTitle("");
  90. btnBack = findViewById(R.id.btn_back);
  91. layoutSearch = findViewById(R.id.layout_search);
  92. layoutSettings = findViewById(R.id.layout_settings);
  93. btnSettingsActive = findViewById(R.id.btn_settings_active);
  94. btnSearchClose = findViewById(R.id.btn_close_search);
  95. layoutSettingsItemsBg = findViewById(R.id.layout_settings_items_bg);
  96. searchResultsListRecyclerView = findViewById(R.id.home_search_result_recyclerview);
  97. btnSearchMore = findViewById(R.id.btn_search_more);
  98. etSearch = findViewById(R.id.et_search);
  99. btnFaqSettings = findViewById(R.id.btn_faq_settings);
  100. btnSettingsMenu = findViewById(R.id.btn_settings_menu);
  101. btnAboutMenu = findViewById(R.id.btn_about_menu);
  102. imgLogo = findViewById(R.id.img_logo);
  103. loadingLayout = findViewById(R.id.loading_layout);
  104. progressBar = findViewById(R.id.progress_bar);
  105. progressBar.getIndeterminateDrawable().setColorFilter(getResources().getColor(R.color.yellow_progress_bar), PorterDuff.Mode.MULTIPLY);
  106. loadingLayout.setVisibility(View.GONE);
  107. etName = findViewById(R.id.et_name);
  108. etLastName = findViewById(R.id.et_lastname);
  109. etEmail = findViewById(R.id.et_email);
  110. etMsg = findViewById(R.id.et_msg);
  111. btnSend = findViewById(R.id.btn_send);
  112. httpService = new VolleyService(this, this, REQUEST_TYPE_BASIC);
  113. }
  114. private void initOnClickListeners() {
  115. imgLogo.setOnClickListener(new View.OnClickListener() {
  116. @Override
  117. public void onClick(View v) {
  118. startActivity(new Intent(ContactFormTemplate.this, HomeActivity.class));
  119. finish();
  120. }
  121. });
  122. btnBack.setOnClickListener(new View.OnClickListener() {
  123. @Override
  124. public void onClick(View v) {
  125. finish();
  126. }
  127. });
  128. btnSettingsActive.setOnClickListener(new View.OnClickListener() {
  129. @Override
  130. public void onClick(View v) {
  131. layoutSettings.setVisibility(View.GONE);
  132. }
  133. });
  134. btnSearchClose.setOnClickListener(new View.OnClickListener() {
  135. @Override
  136. public void onClick(View v) {
  137. layoutSearch.setVisibility(View.GONE);
  138. }
  139. });
  140. layoutSettingsItemsBg.setOnClickListener(new View.OnClickListener() {
  141. @Override
  142. public void onClick(View v) {
  143. layoutSettings.setVisibility(View.GONE);
  144. }
  145. });
  146. btnSearchMore.setOnClickListener(new View.OnClickListener() {
  147. @Override
  148. public void onClick(View v) {
  149. if (!etSearch.getText().toString().equals("")) {
  150. layoutSearch.setVisibility(View.GONE);
  151. Intent intent = new Intent(ContactFormTemplate.this, SearchMoreActivity.class);
  152. intent.putExtra("query", etSearch.getText().toString());
  153. startActivity(intent);
  154. }
  155. }
  156. });
  157. etSearch.addTextChangedListener(new TextWatcher() {
  158. @Override
  159. public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
  160. }
  161. @Override
  162. public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
  163. }
  164. @Override
  165. public void afterTextChanged(Editable edit) {
  166. if (edit.length() != 0) {
  167. String query = edit.toString().toLowerCase();
  168. if (CheckInternetConnection.isOnline(getApplicationContext())) {
  169. requestSearch(query);
  170. } else {
  171. Toast.makeText(getApplicationContext(), getResources().getString(R.string.no_internet_msg), Toast.LENGTH_SHORT).show();
  172. }
  173. } else {
  174. searchResultsList = new ArrayList<>();
  175. updateSearchListResultRecyclerView();
  176. }
  177. }
  178. });
  179. btnFaqSettings.setOnClickListener(new View.OnClickListener() {
  180. @Override
  181. public void onClick(View v) {
  182. startActivity(new Intent(ContactFormTemplate.this, ExpandableListTemplate.class));
  183. layoutSettings.setVisibility(View.GONE);
  184. }
  185. });
  186. btnSettingsMenu.setOnClickListener(new View.OnClickListener() {
  187. @Override
  188. public void onClick(View v) {
  189. startActivity(new Intent(ContactFormTemplate.this, SettingsActivity.class));
  190. layoutSettings.setVisibility(View.GONE);
  191. }
  192. });
  193. btnAboutMenu.setOnClickListener(new View.OnClickListener() {
  194. @Override
  195. public void onClick(View v) {
  196. startActivity(new Intent(ContactFormTemplate.this, AboutPageTemplate.class));
  197. layoutSettings.setVisibility(View.GONE);
  198. }
  199. });
  200. btnSend.setOnClickListener(new View.OnClickListener() {
  201. @Override
  202. public void onClick(View v) {
  203. if (CheckInternetConnection.isOnline(ContactFormTemplate.this)) {
  204. loadingLayout.setVisibility(View.VISIBLE);
  205. if (!etName.getText().toString().equals("") ||
  206. !etLastName.getText().toString().equals("") ||
  207. !etEmail.getText().toString().equals("") ||
  208. !etMsg.getText().toString().equals("")) {
  209. if (isEmailValid(etEmail.getText().toString())) {
  210. sendMessageToServer();
  211. } else {
  212. loadingLayout.setVisibility(View.GONE);
  213. Toast.makeText(ContactFormTemplate.this, getResources().getString(R.string.contact_send_invalid_email_msg), Toast.LENGTH_SHORT).show();
  214. }
  215. } else {
  216. loadingLayout.setVisibility(View.GONE);
  217. Toast.makeText(ContactFormTemplate.this, getResources().getString(R.string.contact_send_empty_msg), Toast.LENGTH_SHORT).show();
  218. }
  219. } else {
  220. loadingLayout.setVisibility(View.GONE);
  221. Toast.makeText(ContactFormTemplate.this, getResources().getString(R.string.no_internet_msg), Toast.LENGTH_SHORT).show();
  222. }
  223. }
  224. });
  225. }
  226. @Override
  227. public boolean onCreateOptionsMenu(Menu menu) {
  228. getMenuInflater().inflate(R.menu.home_options_menu, menu);
  229. menu.getItem(1).setIcon(getResources().getDrawable(R.drawable.home_header_contact_active));
  230. return true;
  231. }
  232. @Override
  233. public boolean onOptionsItemSelected(MenuItem item) {
  234. switch (item.getItemId()) {
  235. case R.id.home_action_search:
  236. layoutSearch.setVisibility(View.VISIBLE);
  237. return true;
  238. case R.id.home_action_contact:
  239. finish();
  240. return true;
  241. case R.id.home_action_settings:
  242. layoutSettings.setVisibility(View.VISIBLE);
  243. return true;
  244. default:
  245. return super.onOptionsItemSelected(item);
  246. }
  247. }
  248. private void updateSearchListResultRecyclerView() {
  249. searchResultsListRecyclerView.setHasFixedSize(true);
  250. final HomeSearchResultListAdapter searchResultAdapter = new HomeSearchResultListAdapter(getApplicationContext(), searchResultsList, HomeSearchResultListAdapter.HOME_SEARCH_LIST_LAYOUT);
  251. searchResultsListRecyclerView.setAdapter(searchResultAdapter);
  252. RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
  253. searchResultsListRecyclerView.setLayoutManager(layoutManager);
  254. }
  255. private void requestSearch(String query) {
  256. httpService = new VolleyService(this, this, REQUEST_TYPE_SEARCH);
  257. searchResultsList = new ArrayList<>();
  258. String params = "?lang=" + EyApplication.langShort
  259. + "&phrase=" + query;
  260. httpService.get(HTTP_SEARCH_URL + params);
  261. }
  262. @SuppressWarnings("SameParameterValue")
  263. private void createSearchResult(String id, String title, String desc) {
  264. HomeSearchResult searchResult = new HomeSearchResult(id, title, desc);
  265. searchResultsList.add(searchResult);
  266. }
  267. private void sendMessageToServer() {
  268. httpService = new VolleyService(this, this, REQUEST_TYPE_BASIC);
  269. JSONObject params = new JSONObject();
  270. try {
  271. params.put("first_name", etName.getText().toString());
  272. params.put("last_name", etLastName.getText().toString());
  273. params.put("email", etEmail.getText().toString());
  274. params.put("lang", EyApplication.langShort);
  275. params.put("message", etMsg.getText().toString());
  276. httpService.post(HTTP_URL, params);
  277. } catch (JSONException e) {
  278. e.printStackTrace();
  279. }
  280. }
  281. private void emptyInputs() {
  282. etName.setText("");
  283. etLastName.setText("");
  284. etEmail.setText("");
  285. etMsg.setText("");
  286. }
  287. private boolean isEmailValid(String email) {
  288. return VALID_EMAIL_ADDRESS_REGEX.matcher(email).find();
  289. }
  290. @Override
  291. public void onSuccessResponse(JSONObject res, int requestType) {
  292. try {
  293. if (requestType == REQUEST_TYPE_BASIC) {
  294. loadingLayout.setVisibility(View.GONE);
  295. Toast.makeText(ContactFormTemplate.this, getResources().getString(R.string.contact_send_success_msg), Toast.LENGTH_SHORT).show();
  296. emptyInputs();
  297. } else {
  298. JSONArray entity = res.getJSONArray("entity");
  299. for (int i = 0; i < entity.length(); i++) {
  300. JSONObject searchItem = entity.getJSONObject(i);
  301. createSearchResult(searchItem.getString("id"), searchItem.getString("cateory"), searchItem.getString("title"));
  302. }
  303. updateSearchListResultRecyclerView();
  304. }
  305. } catch (JSONException e) {
  306. e.printStackTrace();
  307. }
  308. }
  309. @Override
  310. public void onErrorResponse(JSONObject err) {
  311. loadingLayout.setVisibility(View.GONE);
  312. try {
  313. Toast.makeText(getApplicationContext(), err.getString("message"), Toast.LENGTH_LONG).show();
  314. } catch (JSONException e) {
  315. e.printStackTrace();
  316. }
  317. }
  318. }