Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.96 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity {
  2.  
  3. private RecyclerView mRecyclerView;
  4. private ExampleAdapter mAdapter;
  5. private RecyclerView.LayoutManager mLayoutManager;
  6.  
  7. Date temp_curr_date = Calendar.getInstance().getTime();
  8. SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
  9. String sel_date = df.format(temp_curr_date);
  10. String curr_date = df.format(temp_curr_date);
  11.  
  12. double daily_total;
  13. int progress = 0;
  14. double daily_goal = 7.5;
  15.  
  16. TextView textView1;
  17. TextView textView2;
  18. TextView textViewFlights;
  19. ProgressBar pb;
  20.  
  21. List<ExampleItem> mExampleList;
  22. List<ExampleItem> filteredList;
  23.  
  24.  
  25. @Override
  26. protected void onCreate(Bundle savedInstanceState) {
  27. super.onCreate(savedInstanceState);
  28. setContentView(R.layout.activity_main);
  29.  
  30. // ----- LOAD SAVED ARRAY LIST -----
  31. loadData();
  32.  
  33. // ----- SET VARIABLES -----
  34. daily_total = totalOutput(mExampleList, sel_date);
  35. textView1 = findViewById(R.id.total);
  36. textView1.setText(String.valueOf(daily_total));
  37. textViewFlights = findViewById(R.id.flights);
  38.  
  39. pb = findViewById(R.id.progress_bar);
  40. pb.setProgress(getProgress(mExampleList, sel_date), true);
  41.  
  42. // ----- BUILD RECYCLERVIEW -----
  43. buildRecyclerView();
  44. filter(sel_date);
  45.  
  46. // ----- ADD STEPS DIALOGUE -----
  47. setAddStepButton();
  48.  
  49. // ----- CALENDAR DIALOGUE -----
  50. setDateChangeButton();
  51. }
  52.  
  53. public double totalOutput(List<ExampleItem> steps, String date) {
  54. try{
  55. int temp_total = 0;
  56. double flight_total;
  57. for (int a = 0; a < steps.size(); a++) {
  58. if (date.equals(steps.get(a).getText1()))
  59. temp_total += steps.get(a).getText2();
  60. }
  61. flight_total = round(temp_total / 16.0, 2);
  62. return flight_total;
  63. } catch (Exception e){
  64. return 0.0;
  65. }
  66. }
  67.  
  68. public static double round(double value, int places) {
  69. if (places < 0) throw new IllegalArgumentException();
  70.  
  71. BigDecimal bd = new BigDecimal(value);
  72. bd = bd.setScale(places, RoundingMode.HALF_UP);
  73. return bd.doubleValue();
  74. }
  75.  
  76. public static int toInt(double value) {
  77. BigDecimal bd = new BigDecimal(value);
  78. bd = bd.setScale(0, RoundingMode.HALF_UP);
  79. return bd.intValue();
  80. }
  81.  
  82. public static Date getDate(int year, int month, int day) {
  83. Calendar cal = Calendar.getInstance();
  84. cal.set(Calendar.YEAR, year);
  85. cal.set(Calendar.MONTH, month);
  86. cal.set(Calendar.DAY_OF_MONTH, day);
  87. cal.set(Calendar.HOUR_OF_DAY, 0);
  88. cal.set(Calendar.MINUTE, 0);
  89. cal.set(Calendar.SECOND, 0);
  90. cal.set(Calendar.MILLISECOND, 0);
  91. return cal.getTime();
  92. }
  93.  
  94. private void saveData(){
  95. SharedPreferences sharedPreferences = getSharedPreferences("shared preferences", MODE_PRIVATE);
  96. SharedPreferences.Editor editor = sharedPreferences.edit();
  97. Gson gson = new Gson();
  98. String json = gson.toJson(mExampleList);
  99. editor.putString("task list", json);
  100. editor.apply();
  101. }
  102.  
  103. private void loadData(){
  104. SharedPreferences sharedPreferences = getSharedPreferences("shared preferences", MODE_PRIVATE);
  105. Gson gson = new Gson();
  106. String json = sharedPreferences.getString("task list", null);
  107. Type type = new TypeToken<ArrayList<ExampleItem>>() {}.getType();
  108. mExampleList = gson.fromJson(json, type);
  109.  
  110. if (mExampleList == null){
  111. mExampleList = new ArrayList<>();
  112. }
  113. }
  114.  
  115. private int getProgress(List<ExampleItem> steps, String date){
  116. int daily_progress_int;
  117. try{
  118. int temp_progress = 0;
  119. double flight_total;
  120. for (int a = 0; a < steps.size(); a++) {
  121. if (date.compareTo(steps.get(a).getText1()) == 0)
  122. temp_progress += steps.get(a).getText2();
  123. }
  124. flight_total = round(temp_progress / 16.0, 2);
  125. daily_progress_int = toInt((flight_total/daily_goal)*100);
  126. return daily_progress_int;
  127. } catch (Exception e){
  128. return 0;
  129. }
  130. }
  131.  
  132. private void addProgress(double x, int prog){
  133. int daily_progress_int = toInt((x/daily_goal)*100);
  134.  
  135. if (progress <= 100-daily_progress_int){
  136. progress = progress + prog;
  137. pb = findViewById(R.id.progress_bar);
  138. pb.setProgress(daily_progress_int, true);
  139. } else if (progress + daily_progress_int > 100){
  140. pb = findViewById(R.id.progress_bar);
  141. pb.setProgress(100, true);
  142. }
  143.  
  144. }
  145.  
  146. private void removeProgress(double x, int prog){
  147. int daily_progress_int = toInt((x/daily_goal)*100);
  148. progress = progress - prog;
  149. if (progress <= 100) {
  150. pb = findViewById(R.id.progress_bar);
  151. pb.setProgress(daily_progress_int, true);
  152. } else {
  153. pb = findViewById(R.id.progress_bar);
  154. pb.setProgress(0, true);
  155.  
  156. }
  157. }
  158.  
  159. public void addItem(String date, int steps, Instant ts){
  160. mExampleList.add(new ExampleItem(date, steps, ts));
  161. filter(sel_date);
  162. }
  163.  
  164. public void removeItem(final int position){
  165. final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
  166. View viewInflated = LayoutInflater.from(MainActivity.this).inflate(R.layout.confirm, (ViewGroup) findViewById(android.R.id.content), false);
  167. builder.setCancelable(true);
  168. builder.setView(viewInflated);
  169. builder.setPositiveButton("Yup",
  170. new DialogInterface.OnClickListener() {
  171. @Override
  172. public void onClick(DialogInterface dialog, int which) {
  173. mExampleList.remove(position);
  174. mAdapter.notifyItemRemoved(position);
  175. filter(sel_date);
  176.  
  177. daily_total = totalOutput(mExampleList, sel_date);
  178. textView1 = findViewById(R.id.total);
  179. textView1.setText(String.valueOf(daily_total));
  180.  
  181. removeProgress(daily_total,progress);
  182.  
  183. if (daily_total == 1.0){
  184. textViewFlights.setText("flight");
  185. } else {
  186. textViewFlights.setText("flights");
  187. }
  188.  
  189. saveData();
  190. }
  191. });
  192. builder.setNegativeButton("Nope", new DialogInterface.OnClickListener() {
  193. @Override
  194. public void onClick(DialogInterface dialog, int which) {
  195. }
  196. });
  197.  
  198. AlertDialog dialog = builder.create();
  199. dialog.show();
  200. }
  201.  
  202. public void buildRecyclerView(){
  203. mRecyclerView = findViewById(R.id.recyclerView);
  204. mRecyclerView.setHasFixedSize(true);
  205. mLayoutManager = new LinearLayoutManager(this);
  206.  
  207. mAdapter = new ExampleAdapter(mExampleList);
  208.  
  209. mRecyclerView.setLayoutManager(mLayoutManager);
  210. mRecyclerView.setAdapter(mAdapter);
  211.  
  212. mAdapter.setOnItemClickListener(new ExampleAdapter.OnItemClickListener() {
  213. @Override
  214. public void onItemClick(int position) {
  215. removeItem(position);
  216. }
  217. });
  218. }
  219.  
  220. public void filter(String text){
  221. filteredList = new ArrayList<>();
  222.  
  223. for (ExampleItem item : mExampleList){
  224. if (item.getText1().toLowerCase().contains(text.toLowerCase())){
  225. filteredList.add(item);
  226. }
  227. }
  228.  
  229. mAdapter.filterList(filteredList);
  230. }
  231.  
  232. public void setAddStepButton(){
  233. FloatingActionButton fab = findViewById(R.id.addSteps);
  234. fab.setOnClickListener(new View.OnClickListener() {
  235. @Override
  236. public void onClick(View view) {
  237. AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
  238. View viewInflated = LayoutInflater.from(MainActivity.this).inflate(R.layout.add_steps, (ViewGroup) findViewById(android.R.id.content), false);
  239.  
  240. // Step input
  241. final EditText input = viewInflated.findViewById(R.id.input);
  242. builder.setView(viewInflated);
  243.  
  244. // OK Button
  245. builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
  246. @Override
  247. public void onClick(DialogInterface dialog, int which) {
  248. if (input.getText().length() != 0) {
  249. try {
  250. int in = Integer.parseInt(String.valueOf(input.getText()));
  251. if (in > 0) {
  252. Instant timeStamp = Instant.now();
  253. addItem(sel_date, in, timeStamp);
  254. dialog.dismiss();
  255. } else {
  256. dialog.cancel();
  257. }
  258. } catch (Exception e) {
  259. dialog.cancel();
  260. }
  261.  
  262. daily_total = totalOutput(mExampleList, sel_date);
  263. textView1 = findViewById(R.id.total);
  264. textView1.setText(String.valueOf(daily_total));
  265. addProgress(daily_total, progress);
  266. mAdapter.notifyDataSetChanged();
  267. filter(sel_date);
  268.  
  269. if (daily_total == 1.0){
  270. textViewFlights.setText("flight");
  271. } else {
  272. textViewFlights.setText("flights");
  273. }
  274.  
  275. saveData();
  276. } else{
  277. dialog.cancel();
  278. }
  279.  
  280. }
  281. });
  282. // Cancel Button
  283. builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
  284. @Override
  285. public void onClick(DialogInterface dialog, int which) {
  286. dialog.cancel();
  287. }
  288. });
  289. builder.show();
  290. }
  291. });
  292. }
  293.  
  294. public void setDateChangeButton(){
  295. FloatingActionButton fabcal = findViewById(R.id.calendarButton);
  296. fabcal.setOnClickListener(new View.OnClickListener(){
  297. @Override
  298. public void onClick(View view) {
  299. LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService
  300. (Context.LAYOUT_INFLATER_SERVICE);
  301. LinearLayout ll= (LinearLayout)inflater.inflate(R.layout.calendar, null, false);
  302. CalendarView cv = (CalendarView) ll.getChildAt(0);
  303.  
  304. long milliseconds = 0;
  305. try {
  306. Date d = df.parse(sel_date);
  307. milliseconds = d.getTime();
  308. } catch (ParseException e) {
  309. e.printStackTrace();
  310. }
  311.  
  312. cv.setDate(milliseconds);
  313. cv.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
  314.  
  315. @Override
  316. public void onSelectedDayChange(
  317. @NonNull CalendarView view,
  318. int year,
  319. int month,
  320. int dayOfMonth)
  321. {
  322. Date temp_sel_date = getDate(year, month, dayOfMonth);
  323. sel_date = df.format(temp_sel_date);
  324.  
  325. textView2 = findViewById(R.id.daily_total);
  326.  
  327. if (sel_date.equals(curr_date)){
  328. textView2.setText("Today");
  329. } else {
  330. String dt_day = (String) DateFormat.format("dd", temp_sel_date);
  331. String dt_month = (String) DateFormat.format("MMM", temp_sel_date);
  332. textView2.setText(dt_month + " " + dt_day);
  333. }
  334.  
  335. daily_total = totalOutput(mExampleList, sel_date);
  336.  
  337. textView1 = findViewById(R.id.total);
  338. textView1.setText(String.valueOf(daily_total));
  339.  
  340. pb = findViewById(R.id.progress_bar);
  341. pb.setProgress(getProgress(mExampleList, sel_date), true);
  342. mAdapter.notifyDataSetChanged();
  343. filter(sel_date);
  344. }
  345. });
  346.  
  347. new AlertDialog.Builder(MainActivity.this)
  348. .setView(ll)
  349. .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
  350. public void onClick(DialogInterface dialog, int whichButton) {
  351. dialog.dismiss();
  352. }
  353. }
  354. ).show();
  355. }
  356. });
  357. }
  358. }
  359.  
  360. public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder> {
  361. private static List<ExampleItem> mExampleList;
  362. private static List<ExampleItem> exampleListFull;
  363. private OnItemClickListener mListener;
  364.  
  365. public interface OnItemClickListener{
  366. void onItemClick(int position);
  367. }
  368.  
  369. public void setOnItemClickListener(OnItemClickListener listener){
  370. mListener = listener;
  371. }
  372.  
  373. public static class ExampleViewHolder extends RecyclerView.ViewHolder {
  374. public TextView mTextView1;
  375. public ImageView mDeleteImage;
  376.  
  377. public ExampleViewHolder(View itemView, final OnItemClickListener listener) {
  378. super(itemView);
  379. mTextView1 = itemView.findViewById(R.id.textView);
  380. mDeleteImage = itemView.findViewById(R.id.image_delete);
  381.  
  382.  
  383.  
  384. mDeleteImage.setOnClickListener(new View.OnClickListener() {
  385. @Override
  386. public void onClick(View view) {
  387.  
  388. if (listener != null){
  389. int position = getAdapterPosition();
  390. if (position != RecyclerView.NO_POSITION){
  391. Instant test = mExampleList.get(position).getTimeStamp();
  392. for (ExampleItem item : exampleListFull){
  393. int compare = test.compareTo(item.getTimeStamp());
  394. if (compare == 0){
  395. int delIndex = exampleListFull.indexOf(item);
  396. position = delIndex;
  397. }
  398. }
  399. listener.onItemClick(position);
  400. }
  401.  
  402. }
  403. }
  404. });
  405. }
  406. }
  407.  
  408. public ExampleAdapter(List<ExampleItem> exampleList){
  409. this.mExampleList = exampleList;
  410. exampleListFull = new ArrayList<>(exampleList);
  411. }
  412.  
  413. @NonNull
  414. @Override
  415. public ExampleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
  416. View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item, parent, false);
  417. ExampleViewHolder evh = new ExampleViewHolder(v, mListener);
  418. return evh;
  419. }
  420.  
  421. @Override
  422. public void onBindViewHolder(@NonNull ExampleViewHolder holder, int position) {
  423. ExampleItem currentItem = mExampleList.get(position);
  424.  
  425. if (currentItem.getText2() == 1.0){
  426. holder.mTextView1.setText(currentItem.getText2() + " step");
  427. } else {
  428. holder.mTextView1.setText(currentItem.getText2() + " steps");
  429. }
  430. }
  431.  
  432. @Override
  433. public int getItemCount() {
  434. return mExampleList.size();
  435. }
  436.  
  437. public void filterList(List<ExampleItem> filteredList){
  438. mExampleList = filteredList;
  439. notifyDataSetChanged();
  440. }
  441. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement