Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement/kotlin transition #195

Open
wants to merge 2 commits into
base: dev/2.4.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
debug {
applicationIdSuffix ".debug"
minifyEnabled true
shrinkResources false
}
Expand All @@ -49,21 +50,24 @@ android {
dependencies {

implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.8.0'
implementation 'com.google.android.material:material:1.9.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.navigation:navigation-fragment:2.5.3'
implementation 'androidx.navigation:navigation-ui:2.5.3'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.5.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1'
implementation 'androidx.core:core-splashscreen:1.0.0'
implementation 'androidx.navigation:navigation-fragment-ktx:2.6.0'
implementation 'androidx.navigation:navigation-ui-ktx:2.6.0'
implementation 'androidx.core:core-splashscreen:1.0.1'
implementation 'com.android.volley:volley:1.2.1'
implementation 'androidx.core:core-ktx:1.10.0'
implementation 'androidx.core:core-ktx:1.10.1'
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'

// Lifecycle Components
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.1'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.6.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.1'

/** */
implementation 'com.google.code.gson:gson:2.9.0'
Expand All @@ -73,7 +77,7 @@ dependencies {
implementation 'com.github.antonKozyriatskyi:CircularProgressIndicator:1.3.0'
implementation 'fr.bmartel:jspeedtest:1.32.1'
implementation 'io.ipinfo:ipinfo-api:2.1'
implementation 'com.squareup.okhttp3:okhttp:4.9.3'
implementation 'com.squareup.okhttp3:okhttp:4.10.0'
implementation 'com.github.bumptech.glide:glide:4.15.1'
implementation "androidx.work:work-runtime:2.8.1"
implementation "androidx.work:work-runtime-ktx:2.8.1"
Expand Down
105 changes: 105 additions & 0 deletions app/src/main/java/com/drnoob/datamonitor/adapters/UsageDataAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package com.drnoob.datamonitor.adapters

import android.content.Context
import android.content.pm.PackageManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.AsyncListDiffer
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.drnoob.datamonitor.Common
import com.drnoob.datamonitor.R
import com.drnoob.datamonitor.adapters.data.AppDataUsageModel
import com.drnoob.datamonitor.utils.NetworkStatsHelper
import com.skydoves.progressview.ProgressView

class UsageDataAdapter(private val context: Context) :
RecyclerView.Adapter<UsageDataAdapter.UsageDataViewHolder>() {

inner class UsageDataViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)

private val differCallbacks = object : DiffUtil.ItemCallback<AppDataUsageModel>() {
override fun areItemsTheSame(
oldItem: AppDataUsageModel,
newItem: AppDataUsageModel
): Boolean {
return oldItem.packageName == newItem.packageName
&& oldItem.totalDataUsage == newItem.totalDataUsage
}

override fun areContentsTheSame(
oldItem: AppDataUsageModel,
newItem: AppDataUsageModel
): Boolean {
return oldItem.totalDataUsage == newItem.totalDataUsage
&& oldItem.session == newItem.session
}
}

val differ = AsyncListDiffer(this, differCallbacks)

override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): UsageDataAdapter.UsageDataViewHolder {
return UsageDataViewHolder(
LayoutInflater.from(parent.context)
.inflate(R.layout.app_data_usage_item, parent, false)
)
}

override fun onBindViewHolder(holder: UsageDataAdapter.UsageDataViewHolder, position: Int) {

val model = differ.currentList[position]
val itemView = holder.itemView

val mAppIcon = itemView.findViewById<ImageView>(R.id.app_icon)
val mAppName = itemView.findViewById<TextView>(R.id.app_name)
val mDataUsage = itemView.findViewById<TextView>(R.id.data_usage)
val mProgress = itemView.findViewById<ProgressView>(R.id.progress)

try {
if (model.packageName == "com.android.tethering")
mAppIcon.setImageResource(R.drawable.hotspot)
else if (model.packageName == "com.android.deleted")
mAppIcon.setImageResource(R.drawable.deleted_apps)
else
if (Common.isAppInstalled(context, model.packageName))
mAppIcon.setImageDrawable(
context.packageManager.getApplicationIcon(model.packageName)
)
else mAppIcon.setImageResource(R.drawable.deleted_apps)
} catch (e: PackageManager.NameNotFoundException) {
e.printStackTrace()
}

val totalDataUsage =
NetworkStatsHelper.formatData(model.sentMobile, model.receivedMobile)[2]

if (model.progress > 0) mProgress.progress = model.progress.toFloat()
else mProgress.progress = 1F

mAppName.text = model.appName
mDataUsage.text = totalDataUsage

itemView.setOnClickListener {
onItemClickListener?.let {
if (model != null) it(model)
}
}

}

override fun getItemCount(): Int =
differ.currentList.size

private var onItemClickListener: ((AppDataUsageModel) -> Unit)? = null

fun setOnItemClickListener(listener: (AppDataUsageModel) -> Unit) {
onItemClickListener = listener
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.drnoob.datamonitor.adapters.data

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import com.drnoob.datamonitor.utils.helpers.UsageDataHelper
import kotlinx.coroutines.launch

class DataUsageViewModel(private val usageDataHelper: UsageDataHelper) : ViewModel() {

private val _userAppsList: MutableLiveData<List<AppDataUsageModel?>> = MutableLiveData()
val userAppsList: LiveData<List<AppDataUsageModel?>>
get() = _userAppsList

private val _systemAppsList: MutableLiveData<List<AppDataUsageModel?>> = MutableLiveData()
val systemAppsList: LiveData<List<AppDataUsageModel?>>
get() = _userAppsList

fun fetchApps() = viewModelScope.launch {
usageDataHelper.fetchApps()
}

fun loadUserAppsData(session: Int, type: Int) = viewModelScope.launch {
usageDataHelper.fetchApps()
_userAppsList.postValue(usageDataHelper.loadUserAppsData(session, type))
}

fun loadSystemAppsData(session: Int, type: Int) = viewModelScope.launch {
usageDataHelper.fetchApps()
_systemAppsList.postValue(usageDataHelper.loadSystemAppsData(session, type))
}

}

class DataUsageViewModelFactory(private val usageDataHelper: UsageDataHelper) :
ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(DataUsageViewModel::class.java)) {
@Suppress("UNCHECKED_CAST")
return DataUsageViewModel(usageDataHelper) as T
}
throw IllegalArgumentException("Unknown ViewModel class")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import com.drnoob.datamonitor.databinding.ActivityAppPickerBinding;
import com.drnoob.datamonitor.utils.CrashReporter;
import com.drnoob.datamonitor.utils.SharedPreferences;
import com.drnoob.datamonitor.utils.helpers.ThemeHelperKt;
import com.google.android.material.elevation.SurfaceColors;

import java.util.ArrayList;
Expand Down Expand Up @@ -80,7 +81,7 @@ public static void setData(Intent data) {

@Override
protected void onCreate(Bundle savedInstanceState) {
MainActivity.setTheme(AppPickerActivity.this);
ThemeHelperKt.setTheme(AppPickerActivity.this);
Thread.setDefaultUncaughtExceptionHandler(new CrashReporter(AppPickerActivity.this));
String languageCode = SharedPreferences.getUserPrefs(this).getString(APP_LANGUAGE_CODE, "null");
String countryCode = SharedPreferences.getUserPrefs(this).getString(APP_COUNTRY_CODE, "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
import com.drnoob.datamonitor.ui.fragments.SystemDataUsageFragment;
import com.drnoob.datamonitor.utils.CrashReporter;
import com.drnoob.datamonitor.utils.SharedPreferences;
import com.drnoob.datamonitor.utils.helpers.ThemeHelperKt;
import com.google.android.material.elevation.SurfaceColors;

import org.jetbrains.annotations.NotNull;
Expand All @@ -97,7 +98,7 @@ public class ContainerActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
MainActivity.setTheme(ContainerActivity.this);
ThemeHelperKt.setTheme(ContainerActivity.this);
Thread.setDefaultUncaughtExceptionHandler(new CrashReporter(ContainerActivity.this));
String languageCode = SharedPreferences.getUserPrefs(this).getString(APP_LANGUAGE_CODE, "null");
String countryCode = SharedPreferences.getUserPrefs(this).getString(APP_COUNTRY_CODE, "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import com.drnoob.datamonitor.core.base.Preference;
import com.drnoob.datamonitor.databinding.ActivityCrashReportBinding;
import com.drnoob.datamonitor.utils.SharedPreferences;
import com.drnoob.datamonitor.utils.helpers.ThemeHelperKt;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.elevation.SurfaceColors;
import com.google.android.material.snackbar.Snackbar;
Expand All @@ -71,7 +72,7 @@ public class CrashReportActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
MainActivity.setTheme(CrashReportActivity.this);
ThemeHelperKt.setTheme(CrashReportActivity.this);
String languageCode = SharedPreferences.getUserPrefs(this).getString(APP_LANGUAGE_CODE, "null");
String countryCode = SharedPreferences.getUserPrefs(this).getString(APP_COUNTRY_CODE, "");
if (languageCode.equals("null")) {
Expand Down
Loading