From 94927efe04b85013960caa7357ad2ce593174081 Mon Sep 17 00:00:00 2001 From: Gideon Okuro Date: Fri, 15 Mar 2024 20:45:14 +0300 Subject: [PATCH] actions: less warnings is more (fixes #3251) (#3254) Co-authored-by: dogi --- app/build.gradle | 4 +- .../ole/planet/myplanet/base/BaseActivity.kt | 4 +- .../planet/myplanet/base/BaseNewsFragment.kt | 2 +- .../myplanet/base/BaseResourceFragment.kt | 4 +- .../callback/OnHomeItemClickListener.kt | 2 +- .../planet/myplanet/datamanager/Service.kt | 4 +- .../myplanet/ui/chat/ChatDetailFragment.kt | 2 +- .../myplanet/ui/course/AdapterCourses.kt | 2 - .../myplanet/ui/course/TakeCourseFragment.kt | 12 +-- .../enterprises/EnterpriseCalendarFragment.kt | 16 ++-- .../myplanet/ui/feedback/FeedbackFragment.kt | 3 +- .../ui/myPersonals/MyPersonalsFragment.kt | 4 +- .../myplanet/ui/news/NewsDetailActivity.kt | 4 +- .../myplanet/ui/survey/SendSurveyFragment.kt | 2 +- .../ui/sync/DashboardElementActivity.kt | 4 - .../planet/myplanet/ui/sync/SyncActivity.kt | 20 ++--- .../ui/userprofile/AchievementFragment.kt | 3 +- .../ui/userprofile/BecomeMemberActivity.kt | 86 +++++++------------ .../ui/userprofile/EditAchievementFragment.kt | 5 +- .../myplanet/utilities/DownloadUtils.kt | 5 +- .../planet/myplanet/utilities/FileUtils.kt | 6 +- 21 files changed, 81 insertions(+), 113 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 3c96801a7a..43d7a0e047 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -9,8 +9,8 @@ android { applicationId "org.ole.planet.myplanet" minSdkVersion 21 targetSdkVersion 34 - versionCode 1418 - versionName "0.14.18" + versionCode 1419 + versionName "0.14.19" ndkVersion '21.3.6528147' testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" vectorDrawables.useSupportLibrary = true diff --git a/app/src/main/java/org/ole/planet/myplanet/base/BaseActivity.kt b/app/src/main/java/org/ole/planet/myplanet/base/BaseActivity.kt index 6f45761155..9c8e22520c 100644 --- a/app/src/main/java/org/ole/planet/myplanet/base/BaseActivity.kt +++ b/app/src/main/java/org/ole/planet/myplanet/base/BaseActivity.kt @@ -6,8 +6,8 @@ import android.view.MenuItem import androidx.appcompat.app.AppCompatActivity abstract class BaseActivity : AppCompatActivity() { - public override fun onCreate(savedInstanceStat: Bundle?) { - super.onCreate(savedInstanceStat) + public override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) } fun initActionBar() { diff --git a/app/src/main/java/org/ole/planet/myplanet/base/BaseNewsFragment.kt b/app/src/main/java/org/ole/planet/myplanet/base/BaseNewsFragment.kt index a4e471ce18..0be6c7883e 100644 --- a/app/src/main/java/org/ole/planet/myplanet/base/BaseNewsFragment.kt +++ b/app/src/main/java/org/ole/planet/myplanet/base/BaseNewsFragment.kt @@ -87,7 +87,7 @@ abstract class BaseNewsFragment : BaseContainerFragment(), OnNewsItemClickListen override fun onDestroy() { super.onDestroy() - if (profileDbHandler != null) profileDbHandler.onDestory() + profileDbHandler.onDestory() } override fun showReply(news: RealmNews?, fromLogin: Boolean) { diff --git a/app/src/main/java/org/ole/planet/myplanet/base/BaseResourceFragment.kt b/app/src/main/java/org/ole/planet/myplanet/base/BaseResourceFragment.kt index 0fa387c609..c53e86df77 100644 --- a/app/src/main/java/org/ole/planet/myplanet/base/BaseResourceFragment.kt +++ b/app/src/main/java/org/ole/planet/myplanet/base/BaseResourceFragment.kt @@ -102,10 +102,10 @@ abstract class BaseResourceFragment : Fragment() { .setTitle(R.string.download_suggestion) alertDialogBuilder.setPositiveButton(R.string.download_selected) { _: DialogInterface?, _: Int -> lv?.selectedItemsList?.let { - downloadFiles(db_myLibrary, it, settings) + downloadFiles(db_myLibrary, it) }?.let { startDownload(it) } }.setNeutralButton(R.string.download_all) { _: DialogInterface?, _: Int -> - startDownload(downloadAllFiles(db_myLibrary, settings)) + startDownload(downloadAllFiles(db_myLibrary)) }.setNegativeButton(R.string.txt_cancel, null) val alertDialog = alertDialogBuilder.create() createListView(db_myLibrary, alertDialog) diff --git a/app/src/main/java/org/ole/planet/myplanet/callback/OnHomeItemClickListener.kt b/app/src/main/java/org/ole/planet/myplanet/callback/OnHomeItemClickListener.kt index b8aef76b84..1dd652917e 100644 --- a/app/src/main/java/org/ole/planet/myplanet/callback/OnHomeItemClickListener.kt +++ b/app/src/main/java/org/ole/planet/myplanet/callback/OnHomeItemClickListener.kt @@ -7,7 +7,7 @@ import org.ole.planet.myplanet.model.RealmStepExam interface OnHomeItemClickListener { fun openCallFragment(f: Fragment) fun openLibraryDetailFragment(library: RealmMyLibrary?) - fun showRatingDialog(resource: String?, resource_id: String?, title: String?, listener: OnRatingChangeListener?) + fun showRatingDialog(type: String?, resource_id: String?, title: String?, listener: OnRatingChangeListener?) fun sendSurvey(current: RealmStepExam?) } diff --git a/app/src/main/java/org/ole/planet/myplanet/datamanager/Service.kt b/app/src/main/java/org/ole/planet/myplanet/datamanager/Service.kt index 61a1844ddd..38aa20ad56 100644 --- a/app/src/main/java/org/ole/planet/myplanet/datamanager/Service.kt +++ b/app/src/main/java/org/ole/planet/myplanet/datamanager/Service.kt @@ -94,11 +94,11 @@ class Service(private val context: Context) { .enqueue(object : Callback { override fun onResponse(call: Call, response: Response) { preferences.edit() - .putInt("LastWifiID", NetworkUtils.getCurrentNetworkId(context)).commit() + .putInt("LastWifiID", NetworkUtils.getCurrentNetworkId(context)).apply() if (response.body() != null) { val p = response.body() preferences.edit() - .putString("versionDetail", Gson().toJson(response.body())).commit() + .putString("versionDetail", Gson().toJson(response.body())).apply() retrofitInterface.getApkVersion(Utilities.getApkVersionUrl(settings)) .enqueue(object : Callback { override fun onResponse( diff --git a/app/src/main/java/org/ole/planet/myplanet/ui/chat/ChatDetailFragment.kt b/app/src/main/java/org/ole/planet/myplanet/ui/chat/ChatDetailFragment.kt index 71c599d91a..99af1bd567 100644 --- a/app/src/main/java/org/ole/planet/myplanet/ui/chat/ChatDetailFragment.kt +++ b/app/src/main/java/org/ole/planet/myplanet/ui/chat/ChatDetailFragment.kt @@ -299,7 +299,7 @@ class ChatDetailFragment : Fragment() { mRealm = Realm.getDefaultInstance() addConversationToChatHistory(mRealm, _id, query, chatResponse) } finally { - if (mRealm != null && !mRealm.isClosed) { + if (::mRealm.isInitialized && !mRealm.isClosed) { mRealm.close() } } diff --git a/app/src/main/java/org/ole/planet/myplanet/ui/course/AdapterCourses.kt b/app/src/main/java/org/ole/planet/myplanet/ui/course/AdapterCourses.kt index 4a5203c1aa..51f61d5215 100644 --- a/app/src/main/java/org/ole/planet/myplanet/ui/course/AdapterCourses.kt +++ b/app/src/main/java/org/ole/planet/myplanet/ui/course/AdapterCourses.kt @@ -292,8 +292,6 @@ class AdapterCourses(private val context: Context, private var courseList: List< if (`object`.has("ratingByUser")) if (ratingBar != null) { ratingBar.rating = `object`["ratingByUser"].asInt.toFloat() - } else if (ratingBar != null) { - ratingBar.rating = 0f } } } diff --git a/app/src/main/java/org/ole/planet/myplanet/ui/course/TakeCourseFragment.kt b/app/src/main/java/org/ole/planet/myplanet/ui/course/TakeCourseFragment.kt index 334bc21b32..fe13429584 100644 --- a/app/src/main/java/org/ole/planet/myplanet/ui/course/TakeCourseFragment.kt +++ b/app/src/main/java/org/ole/planet/myplanet/ui/course/TakeCourseFragment.kt @@ -35,7 +35,7 @@ class TakeCourseFragment : Fragment(), ViewPager.OnPageChangeListener, View.OnCl lateinit var dbService: DatabaseService lateinit var mRealm: Realm var courseId: String? = null - lateinit var currentCourse: RealmMyCourse + private lateinit var currentCourse: RealmMyCourse lateinit var steps: List lateinit var userModel: RealmUserModel var position = 0 @@ -62,7 +62,7 @@ class TakeCourseFragment : Fragment(), ViewPager.OnPageChangeListener, View.OnCl super.onViewCreated(view, savedInstanceState) fragmentTakeCourseBinding.tvCourseTitle.text = currentCourse.courseTitle steps = getSteps(mRealm, courseId) - if (steps == null || steps.isEmpty()) { + if (steps.isEmpty()) { fragmentTakeCourseBinding.nextStep.visibility = View.GONE fragmentTakeCourseBinding.previousStep.visibility = View.GONE } @@ -108,8 +108,8 @@ class TakeCourseFragment : Fragment(), ViewPager.OnPageChangeListener, View.OnCl fragmentTakeCourseBinding.btnRemove.visibility = View.GONE } createActivity(mRealm, userModel, currentCourse) - fragmentTakeCourseBinding.tvStep.text = "${getString(R.string.step)} 0/${steps?.size ?: 0}" - if (steps != null) fragmentTakeCourseBinding.courseProgress.max = steps.size + fragmentTakeCourseBinding.tvStep.text = "${getString(R.string.step)} 0/${steps.size}" + fragmentTakeCourseBinding.courseProgress.max = steps.size val i = getCurrentProgress(steps, mRealm, userModel.id, courseId) if (i < steps.size) fragmentTakeCourseBinding.courseProgress.secondaryProgress = i + 1 fragmentTakeCourseBinding.courseProgress.progress = i @@ -172,7 +172,7 @@ class TakeCourseFragment : Fragment(), ViewPager.OnPageChangeListener, View.OnCl when (view.id) { R.id.next_step -> { if (isValidClickRight) { - fragmentTakeCourseBinding.viewPagerCourse.currentItem = fragmentTakeCourseBinding.viewPagerCourse.currentItem + 1 + fragmentTakeCourseBinding.viewPagerCourse.currentItem += 1 fragmentTakeCourseBinding.previousStep.visibility = View.VISIBLE } onClickNext() @@ -181,7 +181,7 @@ class TakeCourseFragment : Fragment(), ViewPager.OnPageChangeListener, View.OnCl R.id.previous_step -> { onClickPrevious() if (isValidClickLeft) { - fragmentTakeCourseBinding.viewPagerCourse.currentItem = fragmentTakeCourseBinding.viewPagerCourse.currentItem - 1 + fragmentTakeCourseBinding.viewPagerCourse.currentItem -= 1 } } diff --git a/app/src/main/java/org/ole/planet/myplanet/ui/enterprises/EnterpriseCalendarFragment.kt b/app/src/main/java/org/ole/planet/myplanet/ui/enterprises/EnterpriseCalendarFragment.kt index fc3428c37f..1e7d94a303 100644 --- a/app/src/main/java/org/ole/planet/myplanet/ui/enterprises/EnterpriseCalendarFragment.kt +++ b/app/src/main/java/org/ole/planet/myplanet/ui/enterprises/EnterpriseCalendarFragment.kt @@ -43,7 +43,7 @@ class EnterpriseCalendarFragment : BaseTeamFragment() { override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? - ): View? { + ): View { fragmentEnterpriseCalendarBinding = FragmentEnterpriseCalendarBinding.inflate(inflater, container, false) start = Calendar.getInstance() end = Calendar.getInstance() @@ -79,8 +79,6 @@ class EnterpriseCalendarFragment : BaseTeamFragment() { Utilities.toast(activity, getString(R.string.title_is_required)) } else if (desc.isEmpty()) { Utilities.toast(activity, getString(R.string.description_is_required)) - } else if (start == null) { - Utilities.toast(activity, getString(R.string.start_time_is_required)) } else { if (!mRealm.isInTransaction) mRealm.beginTransaction() val meetup = @@ -90,7 +88,7 @@ class EnterpriseCalendarFragment : BaseTeamFragment() { meetup.meetupLocation = loc meetup.creator = user?.id meetup.startDate = start.timeInMillis - if (end != null) meetup.endDate = end.timeInMillis + meetup.endDate = end.timeInMillis meetup.endTime = addMeetupBinding.tvEndTime.text.toString() meetup.startTime = addMeetupBinding.tvStartTime.text.toString() val rb = addMeetupBinding.rgRecuring.findViewById(addMeetupBinding.rgRecuring.checkedRadioButtonId) @@ -117,7 +115,7 @@ class EnterpriseCalendarFragment : BaseTeamFragment() { date!!.set(Calendar.YEAR, year) date.set(Calendar.MONTH, monthOfYear) date.set(Calendar.DAY_OF_MONTH, dayOfMonth) - if (view != null) view.text = TimeUtils.formatDate(date.timeInMillis, "yyyy-MM-dd") + view.text = TimeUtils.formatDate(date.timeInMillis, "yyyy-MM-dd") }, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH)).show() } @@ -170,11 +168,11 @@ class EnterpriseCalendarFragment : BaseTeamFragment() { override fun create(view: View) = DayViewContainer(view) override fun bind(container: DayViewContainer, day: CalendarDay) { container.textView.text = day.date.dayOfMonth.toString() - var c = Calendar.getInstance() + val c = Calendar.getInstance() c.set(Calendar.YEAR, day.date.year) c.set(Calendar.MONTH, day.date.monthValue - 1) c.set(Calendar.DAY_OF_MONTH, day.date.dayOfMonth) - var event = getEvent(c.timeInMillis) + val event = getEvent(c.timeInMillis) if (day.owner == DayOwner.THIS_MONTH) { container.textView.setTextColor(Color.BLACK) } else { @@ -207,12 +205,12 @@ class EnterpriseCalendarFragment : BaseTeamFragment() { } private fun getTimeMills(time: Long, end: Boolean): Long { - var c = Calendar.getInstance() + val c = Calendar.getInstance() c.timeInMillis = time c.set(Calendar.MINUTE, if (end) 59 else 0) c.set(Calendar.HOUR, if (end) 23 else 0) c.set(Calendar.SECOND, if (end) 59 else 0) - return c.timeInMillis; + return c.timeInMillis } class DayViewContainer(view: View) : ViewContainer(view) { diff --git a/app/src/main/java/org/ole/planet/myplanet/ui/feedback/FeedbackFragment.kt b/app/src/main/java/org/ole/planet/myplanet/ui/feedback/FeedbackFragment.kt index 14e44d420d..88fe66dad0 100644 --- a/app/src/main/java/org/ole/planet/myplanet/ui/feedback/FeedbackFragment.kt +++ b/app/src/main/java/org/ole/planet/myplanet/ui/feedback/FeedbackFragment.kt @@ -46,8 +46,7 @@ class FeedbackFragment : DialogFragment(), View.OnClickListener { databaseService = DatabaseService(requireActivity()) mRealm = databaseService.realmInstance model = UserProfileDbHandler(requireContext()).userModel!! - user = if (model != null) { model.name } - else { "Anonymous" } + user = model.name fragmentFeedbackBinding.btnSubmit.setOnClickListener(this) fragmentFeedbackBinding.btnCancel.setOnClickListener(this) return fragmentFeedbackBinding.root diff --git a/app/src/main/java/org/ole/planet/myplanet/ui/myPersonals/MyPersonalsFragment.kt b/app/src/main/java/org/ole/planet/myplanet/ui/myPersonals/MyPersonalsFragment.kt index 6050899ef9..d5374857fd 100644 --- a/app/src/main/java/org/ole/planet/myplanet/ui/myPersonals/MyPersonalsFragment.kt +++ b/app/src/main/java/org/ole/planet/myplanet/ui/myPersonals/MyPersonalsFragment.kt @@ -80,7 +80,9 @@ class MyPersonalsFragment : Fragment(), OnSelectedMyPersonal { override fun onDestroy() { super.onDestroy() - if (mRealm != null && !mRealm.isClosed) mRealm.close() + if (::mRealm.isInitialized && !mRealm.isClosed) { + mRealm.close() + } } override fun onUpload(personal: RealmMyPersonal?) { diff --git a/app/src/main/java/org/ole/planet/myplanet/ui/news/NewsDetailActivity.kt b/app/src/main/java/org/ole/planet/myplanet/ui/news/NewsDetailActivity.kt index 1ca26260aa..c74df99848 100644 --- a/app/src/main/java/org/ole/planet/myplanet/ui/news/NewsDetailActivity.kt +++ b/app/src/main/java/org/ole/planet/myplanet/ui/news/NewsDetailActivity.kt @@ -23,7 +23,7 @@ import java.util.Date import java.util.UUID class NewsDetailActivity : BaseActivity() { - lateinit var activityNewsDetailBinding: ActivityNewsDetailBinding + private lateinit var activityNewsDetailBinding: ActivityNewsDetailBinding var news: RealmNews? = null lateinit var realm: Realm override fun onCreate(savedInstanceState: Bundle?) { @@ -47,7 +47,7 @@ class NewsDetailActivity : BaseActivity() { newsLog.androidId = NetworkUtils.getUniqueIdentifier() newsLog.type = "news" newsLog.time = Date().time - if (user != null) newsLog.userId = userId + newsLog.userId = userId } initViews() } diff --git a/app/src/main/java/org/ole/planet/myplanet/ui/survey/SendSurveyFragment.kt b/app/src/main/java/org/ole/planet/myplanet/ui/survey/SendSurveyFragment.kt index ccc591cc3d..dbf2ca5f87 100644 --- a/app/src/main/java/org/ole/planet/myplanet/ui/survey/SendSurveyFragment.kt +++ b/app/src/main/java/org/ole/planet/myplanet/ui/survey/SendSurveyFragment.kt @@ -47,7 +47,7 @@ class SendSurveyFragment : BaseDialogFragment() { .equalTo("parentId", if (!TextUtils.isEmpty(exam!!.courseId)) id + "@" + exam.courseId else id) .sort("lastUpdateTime", Sort.DESCENDING).equalTo("status", "pending").findFirst() sub = createSubmission(sub, mRealm) - sub!!.parentId = if (!TextUtils.isEmpty(exam.courseId)) id + "@" + exam.courseId else id + sub.parentId = if (!TextUtils.isEmpty(exam.courseId)) id + "@" + exam.courseId else id sub.userId = userId sub.type = "survey" sub.status = "pending" diff --git a/app/src/main/java/org/ole/planet/myplanet/ui/sync/DashboardElementActivity.kt b/app/src/main/java/org/ole/planet/myplanet/ui/sync/DashboardElementActivity.kt index 03c55cc625..dff7f18660 100644 --- a/app/src/main/java/org/ole/planet/myplanet/ui/sync/DashboardElementActivity.kt +++ b/app/src/main/java/org/ole/planet/myplanet/ui/sync/DashboardElementActivity.kt @@ -132,10 +132,6 @@ abstract class DashboardElementActivity : SyncActivity(), FragmentManager.OnBack Utilities.log("LAST SSID $id") val wifiManager = applicationContext.getSystemService(WIFI_SERVICE) as WifiManager val netId: Int - if (wifiManager == null) { - Utilities.toast(this, getString(R.string.unable_to_connect_to_planet_wifi)) - return - } for (tmp in wifiManager.configuredNetworks) { if (tmp.networkId > -1 && tmp.networkId == id) { netId = tmp.networkId diff --git a/app/src/main/java/org/ole/planet/myplanet/ui/sync/SyncActivity.kt b/app/src/main/java/org/ole/planet/myplanet/ui/sync/SyncActivity.kt index f1b825f627..1f052c877c 100644 --- a/app/src/main/java/org/ole/planet/myplanet/ui/sync/SyncActivity.kt +++ b/app/src/main/java/org/ole/planet/myplanet/ui/sync/SyncActivity.kt @@ -335,7 +335,7 @@ abstract class SyncActivity : ProcessUserDataActivity(), SyncListener, CheckVers progressDialog?.show() } - override fun onSyncFailed(s: String?) { + override fun onSyncFailed(msg: String?) { if (::syncIconDrawable.isInitialized) { syncIconDrawable = syncIcon.drawable as AnimationDrawable syncIconDrawable.stop() @@ -343,7 +343,7 @@ abstract class SyncActivity : ProcessUserDataActivity(), SyncListener, CheckVers syncIcon.invalidateDrawable(syncIconDrawable) } runOnUiThread { - showAlert(this@SyncActivity, getString(R.string.sync_failed), s) + showAlert(this@SyncActivity, getString(R.string.sync_failed), msg) showWifiSettingDialog(this@SyncActivity) } } @@ -745,7 +745,7 @@ abstract class SyncActivity : ProcessUserDataActivity(), SyncListener, CheckVers builder.setNegativeButton("cancel") { dialog: DialogInterface, _: Int -> dialog.dismiss() } builder.setPositiveButton("continue") { dialog: DialogInterface, _: Int -> dialog.dismiss() - val model = mRealm.copyFromRealm(createGuestUser(username, mRealm, settings)) + val model = createGuestUser(username, mRealm, settings)?.let { mRealm.copyFromRealm(it) } if (model == null) { Utilities.toast(this, getString(R.string.unable_to_login)) } else { @@ -988,9 +988,7 @@ abstract class SyncActivity : ProcessUserDataActivity(), SyncListener, CheckVers override fun onItemSelected(parentView: AdapterView<*>?, selectedItemView: View, position: Int, id: Long) { if (position > 0) { val selectedTeam = teams[position - 1] - if (selectedTeam != null) { - selectedTeamId = selectedTeam._id - } + selectedTeamId = selectedTeam._id } } @@ -1092,13 +1090,13 @@ abstract class SyncActivity : ProcessUserDataActivity(), SyncListener, CheckVers }) } - override fun onSuccess(s: String?) { + override fun onSuccess(success: String?) { Utilities.log("Sync completed ") - if (progressDialog?.isShowing == true && s?.contains("Crash") == true) { + if (progressDialog?.isShowing == true && success?.contains("Crash") == true) { progressDialog?.dismiss() } if (::btnSignIn.isInitialized) { - showSnack(btnSignIn, s) + showSnack(btnSignIn, success) } settings.edit().putLong("lastUsageUploaded", Date().time).apply() if (::lblLastSyncDate.isInitialized) { @@ -1139,13 +1137,13 @@ abstract class SyncActivity : ProcessUserDataActivity(), SyncListener, CheckVers bManager.registerReceiver(broadcastReceiver, intentFilter) } - override fun onError(msg: String, block: Boolean) { + override fun onError(msg: String, blockSync: Boolean) { Utilities.toast(this, msg) if (msg.startsWith("Config")) { settingDialog(this) } progressDialog?.dismiss() - if (!block) continueSyncProcess(forceSync = false, isSync = true) else { + if (!blockSync) continueSyncProcess(forceSync = false, isSync = true) else { syncIconDrawable.stop() syncIconDrawable.selectDrawable(0) } diff --git a/app/src/main/java/org/ole/planet/myplanet/ui/userprofile/AchievementFragment.kt b/app/src/main/java/org/ole/planet/myplanet/ui/userprofile/AchievementFragment.kt index 600e8ce52a..320127a6bc 100644 --- a/app/src/main/java/org/ole/planet/myplanet/ui/userprofile/AchievementFragment.kt +++ b/app/src/main/java/org/ole/planet/myplanet/ui/userprofile/AchievementFragment.kt @@ -102,8 +102,7 @@ class AchievementFragment : BaseContainerFragment() { fragmentAchievementBinding.rvOtherInfo.adapter = AdapterOtherInfo(MainApplication.context, achievement!!.getreferences()!!) } aRealm.addChangeListener { - if (fragmentAchievementBinding.llAchievement != null) - fragmentAchievementBinding.llAchievement.removeAllViews() + fragmentAchievementBinding.llAchievement.removeAllViews() createAchievementList() } } else { diff --git a/app/src/main/java/org/ole/planet/myplanet/ui/userprofile/BecomeMemberActivity.kt b/app/src/main/java/org/ole/planet/myplanet/ui/userprofile/BecomeMemberActivity.kt index f89f3d4248..6d2ee85cad 100644 --- a/app/src/main/java/org/ole/planet/myplanet/ui/userprofile/BecomeMemberActivity.kt +++ b/app/src/main/java/org/ole/planet/myplanet/ui/userprofile/BecomeMemberActivity.kt @@ -33,7 +33,7 @@ import java.util.regex.Pattern class BecomeMemberActivity : BaseActivity() { private lateinit var activityBecomeMemberBinding: ActivityBecomeMemberBinding - var dob: String = ""; + var dob: String = "" lateinit var settings: SharedPreferences var guest: Boolean = false private fun showDatePickerDialog() { @@ -54,8 +54,7 @@ class BecomeMemberActivity : BaseActivity() { setContentView(activityBecomeMemberBinding.root) supportActionBar?.setHomeButtonEnabled(true) supportActionBar?.setDisplayHomeAsUpEnabled(true) - var mRealm: Realm = DatabaseService(this).realmInstance; -// var user = UserProfileDbHandler(this).userModel; + val mRealm: Realm = DatabaseService(this).realmInstance val languages = resources.getStringArray(R.array.language) val adapter = ArrayAdapter(this, R.layout.become_a_member_spinner_layout, languages) activityBecomeMemberBinding.spnLang.adapter = adapter @@ -63,8 +62,8 @@ class BecomeMemberActivity : BaseActivity() { showDatePickerDialog() } - val username = intent.getStringExtra("username"); - guest = intent.getBooleanExtra("guest", false); + val username = intent.getStringExtra("username") + guest = intent.getBooleanExtra("guest", false) settings = getSharedPreferences(SyncActivity.PREFS_NAME, Context.MODE_PRIVATE) textChangedListener(mRealm) @@ -82,7 +81,7 @@ class BecomeMemberActivity : BaseActivity() { val firstChar = if (input.isNotEmpty()) input[0] else '\u0000' var hasInvalidCharacters = false - var hasSpecialCharacters: Boolean + val hasSpecialCharacters: Boolean var hasDiacriticCharacters = false val normalizedText = Normalizer.normalize(s, Normalizer.Form.NFD) @@ -128,21 +127,25 @@ class BecomeMemberActivity : BaseActivity() { } activityBecomeMemberBinding.btnSubmit.setOnClickListener { - var username: String? = activityBecomeMemberBinding.etUsername.text.toString() + val userName: String = activityBecomeMemberBinding.etUsername.text.toString() var password: String? = activityBecomeMemberBinding.etPassword.text.toString() - var repassword: String? = activityBecomeMemberBinding.etRePassword.text.toString() - var fname: String? = activityBecomeMemberBinding.etFname.text.toString() - var lname: String? = activityBecomeMemberBinding.etLname.text.toString() - var mname: String? = activityBecomeMemberBinding.etMname.text.toString() - var email: String? = activityBecomeMemberBinding.etEmail.text.toString() - var language: String? = activityBecomeMemberBinding.spnLang.selectedItem.toString() - var phoneNumber: String? = activityBecomeMemberBinding.etPhone.text.toString() - var birthDate: String? = dob - var level: String? = activityBecomeMemberBinding.spnLevel.selectedItem.toString() + val repassword: String = activityBecomeMemberBinding.etRePassword.text.toString() + val fname: String = activityBecomeMemberBinding.etFname.text.toString() + val lname: String = activityBecomeMemberBinding.etLname.text.toString() + val mname: String = activityBecomeMemberBinding.etMname.text.toString() + val email: String = activityBecomeMemberBinding.etEmail.text.toString() + val language: String = activityBecomeMemberBinding.spnLang.selectedItem.toString() + val phoneNumber: String = activityBecomeMemberBinding.etPhone.text.toString() + val birthDate: String = dob + val level: String = activityBecomeMemberBinding.spnLevel.selectedItem.toString() var gender: String? = null - val firstChar = if (username!!.isNotEmpty()) username[0] else null - val hasInvalidCharacters = username.any { char -> + val firstChar = if (userName.isNotEmpty()) { + userName[0] + } else { + null + } + val hasInvalidCharacters = userName.any { char -> char != '_' && char != '.' && char != '-' && !Character.isDigit(char) && !Character.isLetter(char) } @@ -150,16 +153,16 @@ class BecomeMemberActivity : BaseActivity() { val regex = ".*[ßäöüéèêæÆœøØ¿àìòùÀÈÌÒÙáíóúýÁÉÍÓÚÝâîôûÂÊÎÔÛãñõÃÑÕëïÿÄËÏÖÜŸåÅŒçÇðÐ].*" val pattern = Pattern.compile(regex) - val matcher = pattern.matcher(username) + val matcher = pattern.matcher(userName) val hasSpecialCharacters = matcher.matches() val hasDiacriticCharacters = !normalizedText.codePoints().allMatch { codePoint: Int -> Character.isLetterOrDigit(codePoint) || codePoint == '.'.code || codePoint == '-'.code || codePoint == '_'.code } - if (TextUtils.isEmpty(username)) { + if (TextUtils.isEmpty(userName)) { activityBecomeMemberBinding.etUsername.error = getString(R.string.please_enter_a_username) - } else if (username.contains(" ")) { + } else if (userName.contains(" ")) { activityBecomeMemberBinding.etUsername.error = getString(R.string.invalid_username) } else if (firstChar != null && !Character.isDigit(firstChar) && !Character.isLetter(firstChar)) { activityBecomeMemberBinding.etUsername.error = getString(R.string.must_start_with_letter_or_number) @@ -173,8 +176,6 @@ class BecomeMemberActivity : BaseActivity() { activityBecomeMemberBinding.etEmail.error = getString(R.string.invalid_email) } else if (activityBecomeMemberBinding.rbGender.checkedRadioButtonId == -1) { Utilities.toast(this, getString(R.string.please_select_gender)) - } else if (level == null) { - Utilities.toast(this, getString(R.string.level_is_required)); } else { if (activityBecomeMemberBinding.male.isChecked) { gender = "male" @@ -189,45 +190,24 @@ class BecomeMemberActivity : BaseActivity() { } checkMandatoryFieldsAndAddMember( - username, - password!!, - repassword, - fname, - lname, - mname, - email, - language, - level, - phoneNumber, - birthDate, - gender, - mRealm + userName, password, repassword, fname, lname, mname, email, language, level, + phoneNumber, birthDate, gender, mRealm ) } } } private fun checkMandatoryFieldsAndAddMember( - username: String, - password: String, - repassword: String?, - fname: String?, - lname: String?, - mname: String?, - email: String?, - language: String?, - level: String?, - phoneNumber: String?, - birthDate: String?, - gender: String?, - mRealm: Realm + username: String, password: String, repassword: String?, fname: String?, lname: String?, + mname: String?, email: String?, language: String?, level: String?, phoneNumber: String?, + birthDate: String?, gender: String?, mRealm: Realm ) { /** * Creates and adds a new member if the username and password * are not empty and password matches repassword. */ if (username.isNotEmpty() && password.isNotEmpty() && repassword == password) { - var obj = JsonObject() + val obj = JsonObject() obj.addProperty("name", username) obj.addProperty("firstName", fname) obj.addProperty("lastName", lname) @@ -250,7 +230,7 @@ class BecomeMemberActivity : BaseActivity() { obj.addProperty( "customDeviceName", NetworkUtils.getCustomDeviceName(MainApplication.context) ) - var roles = JsonArray() + val roles = JsonArray() roles.add("learner") obj.add("roles", roles) activityBecomeMemberBinding.pbar.visibility = View.VISIBLE @@ -258,10 +238,10 @@ class BecomeMemberActivity : BaseActivity() { // // } Service(this).becomeMember(mRealm, obj, object : Service.CreateUserCallback { - override fun onSuccess(res: String) { + override fun onSuccess(message: String) { runOnUiThread { activityBecomeMemberBinding.pbar.visibility = View.GONE - Utilities.toast(this@BecomeMemberActivity, res) + Utilities.toast(this@BecomeMemberActivity, message) } finish() } diff --git a/app/src/main/java/org/ole/planet/myplanet/ui/userprofile/EditAchievementFragment.kt b/app/src/main/java/org/ole/planet/myplanet/ui/userprofile/EditAchievementFragment.kt index c792c2c5aa..5edb83a4b0 100644 --- a/app/src/main/java/org/ole/planet/myplanet/ui/userprofile/EditAchievementFragment.kt +++ b/app/src/main/java/org/ole/planet/myplanet/ui/userprofile/EditAchievementFragment.kt @@ -37,7 +37,6 @@ import org.ole.planet.myplanet.utilities.CheckboxListView import org.ole.planet.myplanet.utilities.DialogUtils.getAlertDialog import org.ole.planet.myplanet.utilities.TimeUtils.getFormatedDate import org.ole.planet.myplanet.utilities.Utilities -import java.lang.Boolean import java.util.Calendar import java.util.Locale import kotlin.Array @@ -60,7 +59,7 @@ class EditAchievementFragment : BaseContainerFragment(), DatePickerDialog.OnDate private var achievementArray: JsonArray? = null private var resourceArray: JsonArray? = null - override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { fragmentEditAchievementBinding = FragmentEditAchievementBinding.inflate(inflater, container, false) aRealm = DatabaseService(requireActivity()).realmInstance user = UserProfileDbHandler(requireContext()).userModel!! @@ -267,7 +266,7 @@ class EditAchievementFragment : BaseContainerFragment(), DatePickerDialog.OnDate fragmentEditAchievementBinding.etAchievement.setText(achievement!!.achievementsHeader) fragmentEditAchievementBinding.etPurpose.setText(achievement!!.purpose) fragmentEditAchievementBinding.etGoals.setText(achievement!!.goals) - fragmentEditAchievementBinding.cbSendToNation.isChecked = Boolean.parseBoolean(achievement!!.sendToNation) + fragmentEditAchievementBinding.cbSendToNation.isChecked = achievement!!.sendToNation.toBoolean() } fragmentEditAchievementBinding.txtDob.text = if (TextUtils.isEmpty(user.dob)) getString(R.string.birth_date) else getFormatedDate(user.dob, "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") resourceArray = JsonArray() diff --git a/app/src/main/java/org/ole/planet/myplanet/utilities/DownloadUtils.kt b/app/src/main/java/org/ole/planet/myplanet/utilities/DownloadUtils.kt index 810d28400d..e6719b6a1c 100644 --- a/app/src/main/java/org/ole/planet/myplanet/utilities/DownloadUtils.kt +++ b/app/src/main/java/org/ole/planet/myplanet/utilities/DownloadUtils.kt @@ -1,11 +1,10 @@ package org.ole.planet.myplanet.utilities -import android.content.SharedPreferences import org.ole.planet.myplanet.model.RealmMyLibrary object DownloadUtils { @JvmStatic - fun downloadAllFiles(db_myLibrary: List, settings: SharedPreferences?): ArrayList { + fun downloadAllFiles(db_myLibrary: List): ArrayList { val urls = ArrayList() for (i in db_myLibrary.indices) { urls.add(Utilities.getUrl(db_myLibrary[i])) @@ -14,7 +13,7 @@ object DownloadUtils { } @JvmStatic - fun downloadFiles(db_myLibrary: List, selectedItems: ArrayList, settings: SharedPreferences?): ArrayList { + fun downloadFiles(db_myLibrary: List, selectedItems: ArrayList): ArrayList { val urls = ArrayList() for (i in selectedItems.indices) { urls.add(Utilities.getUrl(db_myLibrary[selectedItems[i]])) diff --git a/app/src/main/java/org/ole/planet/myplanet/utilities/FileUtils.kt b/app/src/main/java/org/ole/planet/myplanet/utilities/FileUtils.kt index d45281bf18..d70b75a32e 100644 --- a/app/src/main/java/org/ole/planet/myplanet/utilities/FileUtils.kt +++ b/app/src/main/java/org/ole/planet/myplanet/utilities/FileUtils.kt @@ -29,7 +29,7 @@ import java.io.OutputStream import java.util.UUID object FileUtils { - val SD_PATH = MainApplication.context.getExternalFilesDir(null).toString() + "/ole" + private val SD_PATH = MainApplication.context.getExternalFilesDir(null).toString() + "/ole" @JvmStatic @Throws(IOException::class) fun fullyReadFileToBytes(f: File): ByteArray { @@ -201,7 +201,7 @@ object FileUtils { fun convertStreamToString(`is`: InputStream?): String { val reader = BufferedReader(InputStreamReader(`is`)) val sb = StringBuilder() - var line: String? = null + var line: String? while (reader.readLine().also { line = it } != null) { sb.append(line).append("\n") } @@ -346,7 +346,7 @@ object FileUtils { suffix = "GB" size /= 1024 } - val resultBuffer = StringBuilder(java.lang.Long.toString(size)) + val resultBuffer = StringBuilder(size.toString()) var commaOffset = resultBuffer.length - 3 while (commaOffset > 0) { resultBuffer.insert(commaOffset, ',')