From 07e9e918d66a420504022586e2057b5757a4ca54 Mon Sep 17 00:00:00 2001 From: "sammymutahi12@gmail.com" Date: Sat, 16 Nov 2019 10:53:13 +0300 Subject: [PATCH] fully implemented data binding to ToDoListAdapter --- .idea/caches/build_file_checksums.ser | Bin 539 -> 534 bytes .idea/codeStyles/Project.xml | 134 ++++++++++++++---- .idea/gradle.xml | 1 + .idea/misc.xml | 18 ++- .idea/modules.xml | 3 +- app/build.gradle | 51 +++++-- .../activities/TodoListActivity.java | 5 +- .../adapters/TodoListAdapter.java | 111 ++++++++------- .../practicetodoapp/utils/TodoDateUtils.java | 3 +- .../res/layout/activity_add_or_edit_task.xml | 2 +- app/src/main/res/layout/item_todo_list.xml | 60 ++++---- build.gradle | 2 +- versions.gradle | 22 +++ 13 files changed, 283 insertions(+), 129 deletions(-) create mode 100644 versions.gradle diff --git a/.idea/caches/build_file_checksums.ser b/.idea/caches/build_file_checksums.ser index 1e49432a68040faf9545abd93ec5be136ec7b079..13f011390acc576d1b8385facc3e3769d4fb6a39 100644 GIT binary patch delta 231 zcmbQuGL2=zbk@9L-RSIzbNmzdi&INVGV{`l_0o$HQ*u&Eix`-qoz{F7|G~x)@_>nf zp`w6+fk9yTKdHkLGq*m~h>8nN(^r))VcLT#Q*>R delta 218 zcmbQnGMi<>bk}7SUYzYH@acW6PW?p(ROk*KK9RnLk?Yx@_`)uyB z=jkTsNrgKdURc7QZdnZ_3>d^H}Qazto!XOCLA}#p2mwDBmgr$C)Y}1UN JofSx`001@(QDOi9 diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml index 30aa626..ae78c11 100644 --- a/.idea/codeStyles/Project.xml +++ b/.idea/codeStyles/Project.xml @@ -1,29 +1,113 @@ - - - - - - - - - - + + + +
+ + + + xmlns:android + + ^$ + + + +
+
+ + + + xmlns:.* + + ^$ + + + BY_NAME + +
+
+ + + + .*:id + + http://schemas.android.com/apk/res/android + + + +
+
+ + + + .*:name + + http://schemas.android.com/apk/res/android + + + +
+
+ + + + name + + ^$ + + + +
+
+ + + + style + + ^$ + + + +
+
+ + + + .* + + ^$ + + + BY_NAME + +
+
+ + + + .* + + http://schemas.android.com/apk/res/android + + + ANDROID_ATTRIBUTE_ORDER + +
+
+ + + + .* + + .* + + + BY_NAME + +
+
+
+
\ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml index 7ac24c7..169fd0d 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -12,6 +12,7 @@ diff --git a/.idea/misc.xml b/.idea/misc.xml index 4db45f7..860cc99 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -74,22 +74,36 @@ diff --git a/.idea/modules.xml b/.idea/modules.xml index 7a956e4..f3f3574 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,7 +2,8 @@ - + + diff --git a/app/build.gradle b/app/build.gradle index 5276932..b13d38a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,11 +1,11 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 27 + compileSdkVersion versions.compile_sdk defaultConfig { applicationId "com.tachyonlabs.practicetodoapp" - minSdkVersion 16 - targetSdkVersion 27 + minSdkVersion versions.min_sdk + targetSdkVersion versions.target_sdk versionCode 1 versionName "1.0" vectorDrawables.useSupportLibrary = true @@ -17,18 +17,45 @@ android { proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } - dataBinding.enabled = true + // The Data Binding Library is a support library that allows you to bind UI components in your + // layouts to data sources in your app using a declarative format rather than programmatically. + dataBinding { + enabled = true + } + // Configure only for each module that uses Java 8 language features + // (either in its source code or through dependencies). + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } + + flavorDimensions "version" + productFlavors { + demo { + applicationIdSuffix ".demo" + // This new string resource item will be generated at build time. + resValue "string", "app_id_suffix", ".demo" + buildConfigField 'boolean', 'IS_DEMO', 'true' + } + full { + resValue "string", "app_id_suffix", "" + buildConfigField 'boolean', 'IS_DEMO', 'false' + } + } + + applicationVariants.all { variant -> + variant.resValue "string", "versionName", variant.versionName } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) - implementation 'com.android.support:appcompat-v7:27.1.1' - implementation 'com.android.support.constraint:constraint-layout:1.1.2' - implementation 'com.android.support:design:27.1.1' - implementation 'com.android.support:recyclerview-v7:27.1.1' - implementation 'com.android.support:preference-v7:27.1.1' + implementation "com.android.support:appcompat-v7:$versions.support" + implementation "com.android.support.constraint:constraint-layout:$versions.constraintLayout" + implementation "com.android.support:design:$versions.support" + implementation "com.android.support:recyclerview-v7:$versions.support" + implementation "com.android.support:preference-v7:$versions.support" - testImplementation 'junit:junit:4.12' - androidTestImplementation 'com.android.support.test:runner:1.0.2' - androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' + testImplementation "junit:junit:$versions.jUnit" + androidTestImplementation "com.android.support.test:runner:$versions.runner" + androidTestImplementation "com.android.support.test.espresso:espresso-core:$versions.espresso" } diff --git a/app/src/main/java/com/tachyonlabs/practicetodoapp/activities/TodoListActivity.java b/app/src/main/java/com/tachyonlabs/practicetodoapp/activities/TodoListActivity.java index c8a1f04..932428f 100644 --- a/app/src/main/java/com/tachyonlabs/practicetodoapp/activities/TodoListActivity.java +++ b/app/src/main/java/com/tachyonlabs/practicetodoapp/activities/TodoListActivity.java @@ -79,8 +79,8 @@ public void onClick(View view) { getSupportLoaderManager().initLoader(ID_TODOLIST_LOADER, null, this); - //scheduleDailyDueCheckerAlarm(); - //cancelAlarm(); + scheduleDailyDueCheckerAlarm(); + cancelAlarm(); } @Override @@ -161,6 +161,7 @@ private void updateWidget() { sendBroadcast(intent); } + @NonNull @Override public Loader onCreateLoader(int loaderId, Bundle bundle) { if (loaderId == ID_TODOLIST_LOADER) { diff --git a/app/src/main/java/com/tachyonlabs/practicetodoapp/adapters/TodoListAdapter.java b/app/src/main/java/com/tachyonlabs/practicetodoapp/adapters/TodoListAdapter.java index 716ef62..7dea090 100644 --- a/app/src/main/java/com/tachyonlabs/practicetodoapp/adapters/TodoListAdapter.java +++ b/app/src/main/java/com/tachyonlabs/practicetodoapp/adapters/TodoListAdapter.java @@ -3,6 +3,7 @@ import com.tachyonlabs.practicetodoapp.R; import com.tachyonlabs.practicetodoapp.custom_views.PriorityStarImageView; import com.tachyonlabs.practicetodoapp.data.TodoListContract; +import com.tachyonlabs.practicetodoapp.databinding.ItemTodoListBinding; import com.tachyonlabs.practicetodoapp.models.TodoTask; import com.tachyonlabs.practicetodoapp.utils.TodoDateUtils; @@ -11,6 +12,7 @@ import android.content.res.ColorStateList; import android.content.res.Resources; import android.database.Cursor; +import android.databinding.DataBindingUtil; import android.graphics.Color; import android.support.annotation.NonNull; import android.support.constraint.ConstraintLayout; @@ -65,12 +67,12 @@ public TodoListAdapter(Context context, TodoListAdapterOnClickHandler todoListAd @Override public TodoListAdapterViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { Context context = viewGroup.getContext(); - int layoutIdForListItem = R.layout.item_todo_list; LayoutInflater inflater = LayoutInflater.from(context); boolean shouldAttachToParentImmediately = false; - View view = inflater.inflate(layoutIdForListItem, viewGroup, shouldAttachToParentImmediately); - TodoListAdapterViewHolder viewHolder = new TodoListAdapterViewHolder(view); + ItemTodoListBinding binding = ItemTodoListBinding.inflate(inflater,viewGroup,shouldAttachToParentImmediately); + + TodoListAdapterViewHolder viewHolder = new TodoListAdapterViewHolder(binding); return viewHolder; } @@ -79,47 +81,7 @@ public TodoListAdapterViewHolder onCreateViewHolder(ViewGroup viewGroup, int vie @Override public void onBindViewHolder(@NonNull TodoListAdapter.TodoListAdapterViewHolder holder, int position) { mCursor.moveToPosition(position); - - holder.cbTodoDescription.setText(mCursor.getString(mDescriptionIndex)); - holder.tvTodoDueDate.setTextColor(holder.tvTodoPriority.getCurrentTextColor()); - - String dueDateString; - long dueDate = mCursor.getLong(mDueDateIndex); - Log.d(TAG, mCursor.getString(mDescriptionIndex) + " " + dueDate); - if (dueDate == TodoTask.NO_DUE_DATE) { - dueDateString = mContext.getString(R.string.no_due_date); - } else { - dueDateString = TodoDateUtils.formatDueDate(mContext, dueDate); - } - - int priority = mCursor.getInt(mPriorityIndex); - holder.tvTodoDueDate.setText(dueDateString); - holder.tvTodoPriority.setText(mRes.getStringArray(R.array.priorities)[priority]); - int isCompleted = mCursor.getInt(mCompletedIndex); - holder.cbTodoDescription.setChecked(isCompleted == TodoTask.TASK_COMPLETED); - - if (isCompleted == TodoTask.TASK_COMPLETED) { - // if the task is completed, we want everything grey - holder.clTodoListItem.setBackground(mRes.getDrawable(R.drawable.list_item_completed_touch_selector)); - holder.cbTodoDescription.setTextColor(mRes.getColor(R.color.colorCompleted)); - holder.cbTodoDescription.setSupportButtonTintList(completedCheckboxColors); - holder.tvTodoPriority.setText(mRes.getString(R.string.completed)); - priority = PriorityStarImageView.COMPLETED; - } else { - holder.clTodoListItem.setBackground(mRes.getDrawable(R.drawable.list_item_touch_selector)); - holder.cbTodoDescription.setTextColor(mRes.getColor(R.color.colorPrimaryDark)); - holder.cbTodoDescription.setSupportButtonTintList(unCompletedCheckboxColors); - holder.tvTodoPriority.setText(mRes.getStringArray(R.array.priorities)[priority]); - if (dueDate < TodoDateUtils.getTodaysDateInMillis()) { - // display overdue tasks with the date in red - // yeah, I know red for both overdue and high priority may be not the best idea - holder.tvTodoDueDate.setTextColor(mRes.getColor(R.color.colorOverdue)); - } else { - holder.tvTodoDueDate.setTextColor(holder.tvTodoPriority.getCurrentTextColor()); - Log.d(TAG, "color is " + (holder.tvTodoPriority.getCurrentTextColor())); - } - } - holder.ivTodoPriorityStar.setPriority(priority); + holder.bind(mCursor); } @Override @@ -148,21 +110,17 @@ public interface TodoListAdapterOnClickHandler { } public class TodoListAdapterViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { - final AppCompatCheckBox cbTodoDescription; - final TextView tvTodoDueDate; - final TextView tvTodoPriority; - final PriorityStarImageView ivTodoPriorityStar; + final ConstraintLayout clTodoListItem; + ItemTodoListBinding binding; - public TodoListAdapterViewHolder(View itemView) { - super(itemView); - cbTodoDescription = itemView.findViewById(R.id.cb_todo_description); - tvTodoDueDate = itemView.findViewById(R.id.tv_todo_due_date); - tvTodoPriority = itemView.findViewById(R.id.tv_todo_priority); - ivTodoPriorityStar = itemView.findViewById(R.id.iv_todo_priority_star); + public TodoListAdapterViewHolder(ItemTodoListBinding binding) { + super(binding.getRoot()); + this.binding = binding; clTodoListItem = (ConstraintLayout) itemView; itemView.setOnClickListener(this); - cbTodoDescription.setOnClickListener(this); + binding.cbTodoDescription.setOnClickListener(this); + binding.executePendingBindings(); } @Override @@ -175,5 +133,48 @@ public void onClick(View view) { mCursor.getInt(mCompletedIndex)); mClickHandler.onClick(todoTask, view); } + @SuppressLint("RestrictedApi") + public void bind(Cursor cursor){ + binding.cbTodoDescription.setText(cursor.getString(mDescriptionIndex)); + binding.tvTodoDueDate.setTextColor(binding.tvTodoPriority.getCurrentTextColor()); + + String dueDateString; + long dueDate = cursor.getLong(mDueDateIndex); + Log.d(TAG, cursor.getString(mDescriptionIndex) + " " + dueDate); + if (dueDate == TodoTask.NO_DUE_DATE) { + dueDateString = mContext.getString(R.string.no_due_date); + } else { + dueDateString = TodoDateUtils.formatDueDate(mContext, dueDate); + } + + int priority = cursor.getInt(mPriorityIndex); + binding.tvTodoDueDate.setText(dueDateString); + binding.tvTodoPriority.setText(mRes.getStringArray(R.array.priorities)[priority]); + int isCompleted = cursor.getInt(mCompletedIndex); + binding.cbTodoDescription.setChecked(isCompleted == TodoTask.TASK_COMPLETED); + + if (isCompleted == TodoTask.TASK_COMPLETED) { + // if the task is completed, we want everything grey + clTodoListItem.setBackground(mRes.getDrawable(R.drawable.list_item_completed_touch_selector)); + binding.cbTodoDescription.setTextColor(mRes.getColor(R.color.colorCompleted)); + binding.cbTodoDescription.setSupportButtonTintList(completedCheckboxColors); + binding.tvTodoPriority.setText(mRes.getString(R.string.completed)); + priority = PriorityStarImageView.COMPLETED; + } else { + clTodoListItem.setBackground(mRes.getDrawable(R.drawable.list_item_touch_selector)); + binding.cbTodoDescription.setTextColor(mRes.getColor(R.color.colorPrimaryDark)); + binding.cbTodoDescription.setSupportButtonTintList(unCompletedCheckboxColors); + binding.tvTodoPriority.setText(mRes.getStringArray(R.array.priorities)[priority]); + if (dueDate < TodoDateUtils.getTodaysDateInMillis()) { + // display overdue tasks with the date in red + // yeah, I know red for both overdue and high priority may be not the best idea + binding.tvTodoDueDate.setTextColor(mRes.getColor(R.color.colorOverdue)); + } else { + binding.tvTodoDueDate.setTextColor(binding.tvTodoPriority.getCurrentTextColor()); + Log.d(TAG, "color is " + (binding.tvTodoPriority.getCurrentTextColor())); + } + } + binding.ivTodoPriorityStar.setPriority(priority); + } } } diff --git a/app/src/main/java/com/tachyonlabs/practicetodoapp/utils/TodoDateUtils.java b/app/src/main/java/com/tachyonlabs/practicetodoapp/utils/TodoDateUtils.java index d5a2e8b..715d029 100644 --- a/app/src/main/java/com/tachyonlabs/practicetodoapp/utils/TodoDateUtils.java +++ b/app/src/main/java/com/tachyonlabs/practicetodoapp/utils/TodoDateUtils.java @@ -23,12 +23,11 @@ public static long getTodaysDateInMillis() { } public static String formatDueDate(Context context, long dueDateInMillis) { - String formattedDueDateString = android.text.format.DateUtils.formatDateTime(context, dueDateInMillis, + return android.text.format.DateUtils.formatDateTime(context, dueDateInMillis, android.text.format.DateUtils.FORMAT_SHOW_DATE | android.text.format.DateUtils.FORMAT_ABBREV_MONTH | android.text.format.DateUtils.FORMAT_SHOW_YEAR | android.text.format.DateUtils.FORMAT_ABBREV_WEEKDAY | android.text.format.DateUtils.FORMAT_SHOW_WEEKDAY); - return formattedDueDateString; } } diff --git a/app/src/main/res/layout/activity_add_or_edit_task.xml b/app/src/main/res/layout/activity_add_or_edit_task.xml index 9986c38..4e12ed5 100644 --- a/app/src/main/res/layout/activity_add_or_edit_task.xml +++ b/app/src/main/res/layout/activity_add_or_edit_task.xml @@ -26,7 +26,7 @@ android:layout_marginTop="24dp" android:text="@string/priority" android:id="@+id/tv_priority_label" - app:layout_constraintTop_toBottomOf="@+id/et_task_description"/> + app:layout_constraintTop_toBottomOf="@+id/et_task_description" /> - + + + android:paddingBottom="8dp"> - + tools:text="A todoTask description" /> - + tools:text="@string/no_due_date" /> - + android:layout_marginRight="8dp" + app:layout_constraintEnd_toStartOf="@+id/iv_todo_priority_star" + app:layout_constraintTop_toTopOf="@+id/tv_todo_due_date" + tools:text="@string/high_priority" /> - + app:layout_constraintTop_toBottomOf="@+id/tv_todo_priority" /> - \ No newline at end of file + + \ No newline at end of file diff --git a/build.gradle b/build.gradle index 077cb2f..ff7116e 100644 --- a/build.gradle +++ b/build.gradle @@ -1,7 +1,7 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { - + apply from: 'versions.gradle' repositories { google() jcenter() diff --git a/versions.gradle b/versions.gradle new file mode 100644 index 0000000..4ea63e9 --- /dev/null +++ b/versions.gradle @@ -0,0 +1,22 @@ + +def versions = [:] + +// Sdk and tools +versions.compile_sdk = 27 +versions.min_sdk = 16 +versions.target_sdk = 27 + +//App dependencies +versions.support = '27.1.1' +versions.constraintLayout = '1.1.2' + +//jUnit test +versions.jUnit = '4.12' + +//runner test +versions.runner = '1.0.2' + +//espresso +versions.espresso = '3.0.2' + +ext.versions = versions \ No newline at end of file