Skip to content

Commit

Permalink
Merge pull request #2298 from nextcloud/feature/branding-notes-app
Browse files Browse the repository at this point in the history
Feature Use Branding Util
  • Loading branch information
tobiasKaminsky authored Dec 13, 2024
2 parents 30a0fd8 + ff82055 commit 54bc2c7
Show file tree
Hide file tree
Showing 18 changed files with 199 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@

import android.app.Application;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.WebView;

import androidx.appcompat.app.AppCompatDelegate;

import com.nextcloud.android.sso.FilesAppTypeRegistry;
import com.nextcloud.android.sso.model.FilesAppType;

import it.niedermann.owncloud.notes.branding.BrandingUtil;
import it.niedermann.owncloud.notes.preferences.DarkModeSetting;

public class NotesApplication extends Application {
Expand All @@ -31,6 +36,7 @@ public class NotesApplication extends Application {
private static long lastInteraction = 0;
private static String PREF_KEY_THEME;
private static boolean isGridViewEnabled = false;
private static BrandingUtil brandingUtil;

@Override
public void onCreate() {
Expand All @@ -40,9 +46,26 @@ public void onCreate() {
lockedPreference = prefs.getBoolean(getString(R.string.pref_key_lock), false);
isGridViewEnabled = getDefaultSharedPreferences(this).getBoolean(getString(R.string.pref_key_gridview), false);
super.onCreate();
brandingUtil = BrandingUtil.getInstance(this);
if (BuildConfig.DEBUG) {
WebView.setWebContentsDebuggingEnabled(true);
}
registerFilesAppType();
}

private void registerFilesAppType() {
String packageId = getResources().getString(R.string.package_id);
String accountType = getResources().getString(R.string.account_type);

if (TextUtils.isEmpty(packageId) || TextUtils.isEmpty(accountType)) {
return;
}

FilesAppTypeRegistry.getInstance().init(new FilesAppType(packageId, accountType, FilesAppType.Type.PROD));
}

public static BrandingUtil brandingUtil() {
return brandingUtil;
}

public static void setAppTheme(DarkModeSetting setting) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Objects;
import java.util.stream.Collectors;

import it.niedermann.owncloud.notes.NotesApplication;
import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.branding.BrandedDialogFragment;
import it.niedermann.owncloud.notes.databinding.DialogChooseAccountBinding;
Expand Down Expand Up @@ -80,11 +81,11 @@ public void onAttach(@NonNull Context context) {
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final var dialogBuilder = new MaterialAlertDialogBuilder(requireActivity())
final MaterialAlertDialogBuilder dialogBuilder = new MaterialAlertDialogBuilder(requireActivity())
.setTitle(R.string.simple_move)
.setNegativeButton(android.R.string.cancel, null);

if (targetAccounts.size() > 0) {
if (!targetAccounts.isEmpty()) {
final var binding = DialogChooseAccountBinding.inflate(LayoutInflater.from(requireContext()));
final var adapter = new AccountChooserAdapter(targetAccounts, (account -> {
accountPickerListener.onAccountPicked(account);
Expand All @@ -96,6 +97,8 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
dialogBuilder.setMessage(getString(R.string.no_other_accounts));
}

NotesApplication.brandingUtil().dialog.colorMaterialAlertDialogBackground(requireContext(), dialogBuilder);

return dialogBuilder.create();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package it.niedermann.owncloud.notes.accountswitcher;


import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
Expand All @@ -21,6 +22,7 @@
import com.bumptech.glide.request.RequestOptions;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;

import it.niedermann.owncloud.notes.NotesApplication;
import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.branding.BrandedDialogFragment;
import it.niedermann.owncloud.notes.branding.BrandingUtil;
Expand Down Expand Up @@ -107,9 +109,12 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
dismiss();
});

return new MaterialAlertDialogBuilder(requireContext())
.setView(binding.getRoot())
.create();
final MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(requireContext())
.setView(binding.getRoot());

NotesApplication.brandingUtil().dialog.colorMaterialAlertDialogBackground(requireContext(), builder);

return builder.create();
}

public static DialogFragment newInstance(long currentAccountId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,51 @@
*/
package it.niedermann.owncloud.notes.branding;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Switch;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.preference.PreferenceViewHolder;
import androidx.preference.SwitchPreference;
import androidx.preference.SwitchPreferenceCompat;

public class BrandedSwitchPreference extends SwitchPreference implements Branded {
import com.google.android.material.materialswitch.MaterialSwitch;

import it.niedermann.owncloud.notes.R;

public class BrandedSwitchPreference extends SwitchPreferenceCompat implements Branded {

@ColorInt
private Integer mainColor = null;

@SuppressLint("UseSwitchCompatOrMaterialCode")
@Nullable
private Switch switchView;
private MaterialSwitch switchView;

public BrandedSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
setWidgetLayoutResource(R.layout.preference_switch);
}

public BrandedSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setWidgetLayoutResource(R.layout.preference_switch);
}

public BrandedSwitchPreference(Context context, AttributeSet attrs) {
super(context, attrs);
setWidgetLayoutResource(R.layout.preference_switch);
}

public BrandedSwitchPreference(Context context) {
super(context);
setWidgetLayoutResource(R.layout.preference_switch);
}

@Override
public void onBindViewHolder(PreferenceViewHolder holder) {
public void onBindViewHolder(@NonNull PreferenceViewHolder holder) {
super.onBindViewHolder(holder);

if (holder.itemView instanceof ViewGroup) {
Expand All @@ -65,7 +71,7 @@ public void applyBrand(@ColorInt int color) {
private void applyBrand() {
if (switchView != null) {
final var util = BrandingUtil.of(mainColor, getContext());
util.platform.colorSwitch(switchView);
util.material.colorMaterialSwitch(switchView);
}
}

Expand All @@ -76,19 +82,19 @@ private void applyBrand() {
* @return A Switch class or null
* @see <a href="https://gist.github.com/marchold/45e22839eb94aa14dfb5">Source</a>
*/
private Switch findSwitchWidget(View view) {
if (view instanceof Switch) {
return (Switch) view;
private MaterialSwitch findSwitchWidget(View view) {
if (view instanceof MaterialSwitch) {
return (MaterialSwitch) view;
}
if (view instanceof ViewGroup viewGroup) {
for (int i = 0; i < viewGroup.getChildCount(); i++) {
final var child = viewGroup.getChildAt(i);
if (child instanceof ViewGroup) {
@SuppressLint("UseSwitchCompatOrMaterialCode") final var result = findSwitchWidget(child);
final var result = findSwitchWidget(child);
if (result != null) return result;
}
if (child instanceof Switch) {
return (Switch) child;
if (child instanceof MaterialSwitch) {
return (MaterialSwitch) child;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ private BrandingUtil(
this.notes = new NotesViewThemeUtils(schemes);
}

public static BrandingUtil getInstance(@NonNull Context context) {
int color = BrandingUtil.readBrandMainColor(context);
return new BrandingUtil(
MaterialSchemes.Companion.fromColor(color),
new com.nextcloud.android.common.ui.color.ColorUtil(context)
);
}

public static BrandingUtil of(@ColorInt int color, @NonNull Context context) {
return CACHE.computeIfAbsent(color, c -> new BrandingUtil(
MaterialSchemes.Companion.fromColor(c),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
package it.niedermann.owncloud.notes.branding;

import android.content.Context;
import android.content.DialogInterface;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.core.content.ContextCompat;

import com.google.android.material.button.MaterialButton;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;

import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.NotesApplication;

public class DeleteAlertDialogBuilder extends MaterialAlertDialogBuilder {

Expand All @@ -35,9 +34,12 @@ public AlertDialog create() {
}

public void applyBrand() {
final var positiveButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
if (positiveButton != null) {
positiveButton.setTextColor(ContextCompat.getColor(getContext(), R.color.danger));
if (dialog.getButton(AlertDialog.BUTTON_POSITIVE) instanceof MaterialButton positiveButton) {
NotesApplication.brandingUtil().material.colorMaterialButtonPrimaryTonal(positiveButton);
}

if (dialog.getButton(AlertDialog.BUTTON_NEGATIVE) instanceof MaterialButton negativeButton) {
NotesApplication.brandingUtil().material.colorMaterialButtonPrimaryBorderless(negativeButton);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import java.util.List;

import it.niedermann.owncloud.notes.NotesApplication;
import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.branding.BrandedDialogFragment;
import it.niedermann.owncloud.notes.branding.BrandingUtil;
Expand Down Expand Up @@ -155,13 +156,16 @@ public void afterTextChanged(Editable s) {
}
});

return new MaterialAlertDialogBuilder(requireContext())
final MaterialAlertDialogBuilder alertDialogBuilder = new MaterialAlertDialogBuilder(requireContext())
.setTitle(R.string.change_category_title)
.setView(dialogView)
.setCancelable(true)
.setPositiveButton(R.string.action_edit_save, (dialog, which) -> listener.onCategoryChosen(editCategory.getText().toString()))
.setNegativeButton(R.string.simple_cancel, null)
.create();
.setNegativeButton(R.string.simple_cancel, null);

NotesApplication.brandingUtil().dialog.colorMaterialAlertDialogBackground(requireContext(), alertDialogBuilder);

return alertDialogBuilder.create();
}

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

import com.google.android.material.dialog.MaterialAlertDialogBuilder;

import it.niedermann.owncloud.notes.NotesApplication;
import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.branding.BrandedDialogFragment;
import it.niedermann.owncloud.notes.branding.BrandingUtil;
Expand Down Expand Up @@ -59,13 +60,16 @@ public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
binding.title.setText(oldTitle);
}

return new MaterialAlertDialogBuilder(requireContext())
final MaterialAlertDialogBuilder alertDialogBuilder = new MaterialAlertDialogBuilder(requireContext())
.setTitle(R.string.change_note_title)
.setView(dialogView)
.setCancelable(true)
.setPositiveButton(R.string.action_edit_save, (dialog, which) -> listener.onTitleEdited(binding.title.getText().toString()))
.setNegativeButton(R.string.simple_cancel, null)
.create();
.setNegativeButton(R.string.simple_cancel, null);

NotesApplication.brandingUtil().dialog.colorMaterialAlertDialogBackground(requireContext(), alertDialogBuilder);

return alertDialogBuilder.create();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import it.niedermann.android.util.ClipboardUtil;
import it.niedermann.nextcloud.exception.ExceptionUtil;
import it.niedermann.owncloud.notes.BuildConfig;
import it.niedermann.owncloud.notes.NotesApplication;
import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.databinding.ActivityExceptionBinding;
import it.niedermann.owncloud.notes.exception.tips.TipsAdapter;
Expand Down Expand Up @@ -53,6 +54,11 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
binding.copy.setOnClickListener((v) -> ClipboardUtil.copyToClipboard(this, getString(R.string.simple_exception), "```\n" + debugInfos + "\n```"));
binding.close.setOnClickListener((v) -> finish());

NotesApplication.brandingUtil().platform.themeStatusBar(this);
NotesApplication.brandingUtil().material.themeToolbar(binding.toolbar);
NotesApplication.brandingUtil().material.colorMaterialButtonPrimaryBorderless(binding.close);
NotesApplication.brandingUtil().material.colorMaterialButtonPrimaryFilled(binding.copy);

adapter.setThrowables(Collections.singletonList(throwable));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import it.niedermann.android.util.ClipboardUtil;
import it.niedermann.nextcloud.exception.ExceptionUtil;
import it.niedermann.owncloud.notes.BuildConfig;
import it.niedermann.owncloud.notes.NotesApplication;
import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.databinding.DialogExceptionBinding;
import it.niedermann.owncloud.notes.exception.tips.TipsAdapter;
Expand Down Expand Up @@ -69,12 +70,15 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {

adapter.setThrowables(throwables);

return new MaterialAlertDialogBuilder(requireActivity())
final MaterialAlertDialogBuilder alertDialogBuilder = new MaterialAlertDialogBuilder(requireActivity())
.setView(binding.getRoot())
.setTitle(R.string.error_dialog_title)
.setPositiveButton(android.R.string.copy, (a, b) -> ClipboardUtil.copyToClipboard(requireContext(), getString(R.string.simple_exception), "```\n" + debugInfos + "\n```"))
.setNegativeButton(R.string.simple_close, null)
.create();
.setNegativeButton(R.string.simple_close, null);

NotesApplication.brandingUtil().dialog.colorMaterialAlertDialogBackground(requireContext(), alertDialogBuilder);

return alertDialogBuilder.create();
}

public static DialogFragment newInstance(ArrayList<Throwable> exceptions) {
Expand Down
Loading

0 comments on commit 54bc2c7

Please sign in to comment.