Skip to content

Commit

Permalink
some code cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
syslogic committed Oct 11, 2024
1 parent b79071d commit 726632a
Show file tree
Hide file tree
Showing 21 changed files with 135 additions and 77 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ The JitPack repository URL would be: `maven { url 'https://jitpack.io' }`

dependencies {
// implementation "io.syslogic:androidx-github:master-SNAPSHOT"
implementation "io.syslogic:androidx-github:1.1.8"
implementation "io.syslogic:androidx-github:1.1.9"
}

[![Release](https://jitpack.io/v/syslogic/androidx-github.svg)](https://jitpack.io/#io.syslogic/androidx-github)
Expand Down
14 changes: 6 additions & 8 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@ plugins {
alias(libs.plugins.androidx.navigation.safeargs) apply false
}

/** Version Settings, loaded from file `version.properties` */
def version = new Properties()
version.load(new FileInputStream(rootProject.file('version.properties')))
project.ext.set('versionCode', new Integer(version['versionCode']))
project.ext.set('versionName', version['versionName'])

/* JitPack: use tag as versionName. */
if (System.env.JITPACK) {
project.ext.set('version_name', System.env.VERSION)
Expand All @@ -22,13 +16,15 @@ project.ext.set('archiveBuildTypes', ['debug', 'release'])
/** Keystore Settings, loaded from keystore.properties */
if (rootProject.file('keystore.properties').exists()) {
def keystore = new Properties()
keystore.load(new FileInputStream(rootProject.file('keystore.properties')))
def ins = new FileInputStream(rootProject.file('keystore.properties'))
keystore.load(ins)
project.ext.set('debugKeystorePass', keystore['debugKeystorePass'])
project.ext.set('debugKeyAlias', keystore['debugKeyAlias'])
project.ext.set('debugKeyPass', keystore['debugKeyPass'])
project.ext.set('releaseKeystorePass', keystore['releaseKeystorePass'])
project.ext.set('releaseKeyAlias', keystore['releaseKeyAlias'])
project.ext.set('releaseKeyPass', keystore['releaseKeyPass'])
ins.close()
}

/** Modules */
Expand All @@ -40,7 +36,9 @@ allprojects {
def requested = details.requested
if (requested.group == 'org.jetbrains.kotlin') {
List<String> list = ['kotlin-stdlib', 'kotlin-stdlib-jdk7', 'kotlin-stdlib-jdk8', 'kotlin-stdlib-common']
if (list.contains(requested.name)) { details.useVersion libs.versions.kotlin.get() }
if (list.contains(requested.name)) {
details.useVersion libs.versions.kotlin.get()
}
}
}
}
Expand Down
16 changes: 12 additions & 4 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
[versions]
android_gradle_plugin = '8.6.0'
app_version_code = '4'
app_version_name = '1.0.4'
android_build_tools = '35.0.0'
android_compile_sdk = '35'
android_target_sdk = '35'
android_min_sdk = '22'

# Gradle Plugins
android_gradle_plugin = '8.7.0'
kotlin = '2.0.20'
material_design = '1.12.0'
retrofit = '2.11.0'
gson = '2.10.1'
gson = '2.11.0'
junit = '4.13.2'
flexbox = '3.0.0'
# Note: DirCacheCheckout.java uses InputStream.transferTo() since jgit 6.3; no such method on Android.
jgit = '6.2.0.202206071550-r'
slf4j = '2.0.13'
androidx_appcompat = '1.7.0'
androidx_annotation = '1.8.2'
androidx_navigation = '2.8.0'
androidx_fragment = '1.8.3'
androidx_navigation = '2.8.2'
androidx_fragment = '1.8.4'
androidx_recyclerview = '1.3.2'
androidx_cardview = '1.0.0'
androidx_preference = '1.2.1'
Expand Down
15 changes: 8 additions & 7 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ plugins {
}

base {
archivesName = 'androidx_github_' + rootProject.ext.get('versionName')
String versionName = libs.versions.app.version.name.get()
archivesName = "androidx_github_$versionName"
}

android {
namespace 'io.syslogic.github.api'
buildToolsVersion = '35.0.0'
compileSdk 35
buildToolsVersion = libs.versions.android.build.tools.get()
compileSdk Integer.parseInt(libs.versions.android.compile.sdk.get())
defaultConfig {
minSdk 22
targetSdk 35
minSdk Integer.parseInt(libs.versions.android.min.sdk.get())
targetSdk Integer.parseInt(libs.versions.android.target.sdk.get())
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFile "${project.rootDir}/proguard/consumer.pro"
}
Expand Down Expand Up @@ -165,15 +166,15 @@ artifacts {
}

group = 'io.syslogic'
version = rootProject.ext.get('versionName')
version = libs.versions.app.version.name.get()

afterEvaluate {
publishing {
publications {
release(MavenPublication) {
groupId = group
artifactId = 'androidx-github'
version = rootProject.ext.get('versionName')
version = libs.versions.app.version.name.get()
from components.getByName('release')
pom {
name = 'GitHub API Client'
Expand Down
15 changes: 8 additions & 7 deletions mobile/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,22 @@ if (rootProject.file('token.properties').exists()) {
}

base {
archivesName = 'github_client_' + rootProject.ext.get('versionName')
String versionName = libs.versions.app.version.name.get()
archivesName = "github_client_$versionName"
}

android {
namespace 'io.syslogic.github'
buildToolsVersion = '35.0.0'
compileSdk 35
buildToolsVersion = libs.versions.android.build.tools.get()
compileSdk Integer.parseInt(libs.versions.android.compile.sdk.get())
defaultConfig {
minSdk 22
targetSdk 35
minSdk Integer.parseInt(libs.versions.android.min.sdk.get())
targetSdk Integer.parseInt(libs.versions.android.target.sdk.get())
versionCode Integer.parseInt(libs.versions.app.version.code.get())
versionName libs.versions.app.version.name.get()
applicationId 'io.syslogic.github'
//noinspection GroovyConstructorNamedArguments
manifestPlaceholders = [ accessToken: "" ]
versionName rootProject.ext.get('versionName')
versionCode rootProject.ext.get('versionCode')
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
testBuildType "debug"
multiDexEnabled true
Expand Down
3 changes: 3 additions & 0 deletions mobile/src/main/java/io/syslogic/github/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public final class Constants {
/** Deprecated SDK Constant */
@NonNull public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";

/** The default query-string default query-string is being used, when table `query_strings` is empty. */
@NonNull public static final String DEFAULT_QUERY_STRING = "topic:android";

/** Table Names */
@NonNull public static final String TABLE_QUERY_STRINGS = "query_strings";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.accounts.Account;
import android.app.Activity;
import android.app.Application;
import android.content.AbstractThreadedSyncAdapter;
import android.content.ContentProviderClient;
import android.content.Context;
Expand Down Expand Up @@ -45,7 +46,7 @@ public class RepositorySyncAdapter extends AbstractThreadedSyncAdapter {

private final SharedPreferences prefs;

private final String accessToken;
private String accessToken = null;

private final String username;

Expand All @@ -62,7 +63,12 @@ public RepositorySyncAdapter(@NonNull Context context, boolean autoInitialize, b
super(context, autoInitialize, allowParallelSyncs);
this.prefs = PreferenceManager.getDefaultSharedPreferences(context);
this.dao = Abstraction.getInstance(context).repositoriesDao();
this.accessToken = TokenHelper.getAccessToken((Activity) context);

if (context instanceof Activity activity) {
this.accessToken = TokenHelper.getAccessToken(activity);
} else if (context instanceof Application app) {
this.accessToken = TokenHelper.getAccessToken(app.getBaseContext());
}
this.username = TokenHelper.getUsername(context);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.DialogFragment;
import androidx.preference.PreferenceManager;

import io.syslogic.github.BuildConfig;
import io.syslogic.github.R;

/**
* Base {@link DialogFragment}
Expand Down Expand Up @@ -61,8 +59,10 @@ public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
this.dialog = new Dialog(this.requireContext());
this.dialog.setCanceledOnTouchOutside(this.cancelOnTouchOutSide);
if (this.dialog.getWindow() != null) {
this.dialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
this.dialog.getWindow().setNavigationBarColor(ContextCompat.getColor(requireContext(), R.color.colorPrimaryDark));
this.dialog.getWindow().setLayout(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
}
return this.dialog;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,17 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
}
}

/* Navigating to RepositoriesFragment */
/* Navigate to RepositoriesFragment */
this.mDataBinding.buttonUserRepositories
.setOnClickListener(view -> activity.getNavController()
.navigate(R.id.action_homeScreenFragment_to_repositoriesGraph));
.navigate(R.id.action_homeScreenFragment_to_repositoriesGraph));

/* Navigating to RepositorySearchFragment */
/* Navigate to RepositorySearchFragment */
this.mDataBinding.buttonRepositorySearch
.setOnClickListener(view -> activity.getNavController()
.navigate(R.id.action_homeScreenFragment_to_repositorySearchFragment));

/* Navigating to GitHub Sponsors */
/* Navigate to GitHub Sponsors */
this.mDataBinding.textGitHubSponsors
.setOnClickListener(view -> activity.startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(activity.getString(R.string.url_git_hub_sponsors)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,22 +107,31 @@ public void onNothingSelected(AdapterView<?> parent) {}
Abstraction.executorService.execute(() -> {
try {
assert dao != null;
String queryString = null;
List<QueryString> items = dao.getItems();
if (items.size() > 0) {
String queryString = items.get(0).toQueryString();
requireActivity().runOnUiThread(() -> {
RepositorySearchAdapter adapter = new RepositorySearchAdapter(requireContext(), queryString, showRepositoryTopics, 1);
getDataBinding().recyclerviewRepositorySearch.setAdapter(adapter);
PagerState pagerState = getDataBinding().getPagerState();
if (pagerState != null) {
pagerState.setQueryString(queryString);
getDataBinding().setPagerState(pagerState);
}
});
if (! items.isEmpty()) {
queryString = items.get(0).toQueryString();

} else {
if (mDebug) {Log.e(LOG_TAG, "table `query_strings` has no records.");}
this.getDataBinding().toolbarRepositorySearch.spinnerQueryString.setVisibility(View.INVISIBLE);
queryString = Constants.DEFAULT_QUERY_STRING;
if (mDebug) {
Log.w(LOG_TAG, "Table `query_strings` currently has no records.");
Log.w(LOG_TAG, "Using thr default value: \"" + Constants.DEFAULT_QUERY_STRING + "\".");
}
}

String finalQueryString = queryString;
requireActivity().runOnUiThread(() -> {
RepositorySearchAdapter adapter = new RepositorySearchAdapter(requireContext(), finalQueryString, showRepositoryTopics, 1);
getDataBinding().recyclerviewRepositorySearch.setAdapter(adapter);
PagerState pagerState = getDataBinding().getPagerState();
if (pagerState != null) {
pagerState.setQueryString(finalQueryString);
getDataBinding().setPagerState(pagerState);
}
});

} catch (IllegalStateException e) {
if (mDebug) {Log.e(LOG_TAG, "" + e.getMessage());}
}
Expand Down
32 changes: 17 additions & 15 deletions mobile/src/main/java/io/syslogic/github/network/TokenHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,32 +47,34 @@ public class TokenHelper {
static final boolean mDebug = BuildConfig.DEBUG;

@Nullable
public static String getAccessToken(@NonNull Activity activity) {
AccountManager accountManager = AccountManager.get(activity);
public static String getAccessToken(@NonNull Context context) {
AccountManager accountManager = AccountManager.get(context);
Account account = getAccount(accountManager, 0);
if (account != null) {
/* Default: Load the access token from AccountManager. */
return accountManager.getUserData(account, "token");
} else if (mDebug) {
/* Debug: Try to load and validate the access token. */
return loadTokenFromPackageMeta(activity, accountManager);
return loadTokenFromPackageMeta(context, accountManager);
} else {
Log.e(LOG_TAG, "Account not found: " + Constants.ACCOUNT_TYPE);
return null;
}
}

public static void setAccessToken(Activity activity, @Nullable String token) {
AccountManager accountManager = AccountManager.get(activity);
public static void setAccessToken(Context context, @Nullable String token) {
AccountManager accountManager = AccountManager.get(context);
Account account = getAccount(accountManager, 0);
if (account != null && token == null) {
accountManager.removeAccount(account, activity, accountManagerFuture -> {
Log.d(LOG_TAG, "Account removed: " + Constants.ACCOUNT_TYPE);
}, new Handler(Looper.getMainLooper()));
if (context instanceof Activity activity) {
accountManager.removeAccount(account, activity, accountManagerFuture -> {
Log.d(LOG_TAG, "Account removed: " + Constants.ACCOUNT_TYPE);
}, new Handler(Looper.getMainLooper()));
}
} else if (account == null && token == null) {
/* This maybe happen when the token loaded from package-meta has expired. */
Intent intent = new Intent(activity, AuthenticatorActivity.class);
activity.startActivity(intent);
Intent intent = new Intent(context, AuthenticatorActivity.class);
context.startActivity(intent);
}
}

Expand All @@ -91,16 +93,16 @@ public static String getUsername(@NonNull Context context) {
}

@Nullable
private static String loadTokenFromPackageMeta(@NonNull Activity activity, AccountManager accountManager) {
private static String loadTokenFromPackageMeta(@NonNull Context context, AccountManager accountManager) {
String token = null;
try {

ApplicationInfo app;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) {
PackageManager.ApplicationInfoFlags flags = PackageManager.ApplicationInfoFlags.of(PackageManager.GET_META_DATA);
app = activity.getPackageManager().getApplicationInfo(activity.getPackageName(), flags);
app = context.getPackageManager().getApplicationInfo(context.getPackageName(), flags);
} else {
app = activity.getPackageManager().getApplicationInfo(activity.getPackageName(), PackageManager.GET_META_DATA);
app = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
}

if (mDebug && app.metaData.keySet().contains("com.github.ACCESS_TOKEN")) {
Expand Down Expand Up @@ -134,9 +136,9 @@ public void onResponse(@NonNull Call<User> call, @NonNull Response<User> respons
if (message.equals("\"Bad credentials\"")) {

// Remove the token, it is invalid anyway.
TokenHelper.setAccessToken(activity, null);
TokenHelper.setAccessToken(context, null);

if (activity instanceof NavHostActivity activity2) {
if (context instanceof NavHostActivity activity2) {
if (activity2.getCurrentFragment() instanceof HomeScreenFragment) {
activity2.getNavController().navigate(
R.id.action_homeScreenFragment_to_preferencesFragment
Expand Down
13 changes: 13 additions & 0 deletions mobile/src/main/res/drawable-night/ic_baseline_bookmark_add_48.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<vector
xmlns:android="http://schemas.android.com/apk/res/android"
android:tint="#FFFFFF"
android:viewportHeight="24"
android:viewportWidth="24"
android:height="48dp"
android:width="48dp">

<path
android:fillColor="@android:color/white"
android:pathData="M21,7h-2v2h-2V7h-2V5h2V3h2v2h2V7zM19,21l-7,-3l-7,3V5c0,-1.1 0.9,-2 2,-2l7,0c-0.63,0.84 -1,1.87 -1,3c0,2.76 2.24,5 5,5c0.34,0 0.68,-0.03 1,-0.1V21z"/>

</vector>
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
<vector
xmlns:android="http://schemas.android.com/apk/res/android"
android:tint="#FFFFFF"
android:height="48dp"
android:width="48dp"
android:viewportHeight="24"
android:viewportWidth="24">
android:viewportWidth="24"
android:height="48dp"
android:width="48dp">

<path
android:fillColor="@android:color/white"
android:pathData="M19,18l2,1V3c0,-1.1 -0.9,-2 -2,-2H8.99C7.89,1 7,1.9 7,3h10c1.1,0 2,0.9 2,2v13zM15,5H5c-1.1,0 -2,0.9 -2,2v16l7,-3 7,3V7c0,-1.1 -0.9,-2 -2,-2z"/>

</vector>
Loading

0 comments on commit 726632a

Please sign in to comment.