# anggastudio/printama architecture

> Architecture diagram of the GitHub repository anggastudio/printama, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/anggastudio/printama
- Repository: https://github.com/anggastudio/printama

## Overview

- The “printama” module: This is the core library that implements the printing logic, exposes the Printama API (with classes like Printama.java, PrinterUtil.java, etc.), and manages printer discovery, formatting (text, image, layout view conversion), and connection management.
- The “app” module: This is a sample or test application demonstrating how to use the Printama library. It contains sample activities (MainActivity.java, TestActivity.java, PrintamaPrintService.java) and example usage code (in both unit tests and instrumentation tests).

##### Main Components
a. Android Client / Application Layer:
– This represents the sample “app” module that provides UI components.
– Key elements: Activities like MainActivity, TestActivity, and any associated UI elements (layouts, resources).
– Integration points where the user triggers Bluetooth permission checks, device selection dialogs, and printing commands.
b. Printama Library Module:
– The core “printama” module that encapsulates the printing functionality.
– Main classes: Printama.java (manages connection and printing methods), PrinterUtil.java, and helper classes for formatting text/images.
– Contains services (i.e., PrintamaPrintService in the app module) that manage the lifecycle of a Bluetooth printing session.
c. Android System Services:
– External environment: Thermal Printer devices with which the library establishes a connection.

##### Relationships and Interactions
a. The Sample App (client) leverages the Printama API. Illustrate a directional flow from the activity (UI) to the Printama library when the user requests an operation (printing text, images, or layouts).
b. Within the Printama library, show the internal flow:
– API methods (e.g., with(context).connect(...)) that trigger operations.
– Text/Image formatting, permission checking, and service invocation.
c. Interactions between the app’s UI (that handles device selection and permission dialogs) and system services (Bluetooth enabler, permission dialogs).
d. Callbacks (e.g., the lambda passed to showPrinterList()) to denote asynchronous communication between the library and the application.

##### Architectural Patterns and Practices
a. The library employs callback-based asynchronous interactions (as seen with the connect() method that accepts a lambda) – this pattern should be noted.
b. Separation of concerns: The demo “app” handles UI and permission checking, while the “printama” module encapsulates all printing logic, making the library modular and reusable.
c. Usage of Android’s resource system (layouts, drawables, XMLs) is evident; highlight how these resources are integrated into the UI layer and possibly used by the library (e.g. converting vector drawables to bitmaps).

##### Technologies, Frameworks, or Libraries
a. Android SDK with Java 8+.
b. Gradle for build automation (note the separate build.gradle files and proguard rules for production builds).
c. Standard Android support libraries for UI (Activities, Fragments) and Bluetooth/permissions.
d. You may optionally represent testing frameworks based on the existence of instrumentation tests and unit tests.

– “Sample App Module” with subcomponents: UI/Activities (MainActivity, TestActivity, PrintamaPrintService), Layout/Resource files.
– “Printama Library Module” with core classes: Printama, PrinterUtil, various formatting models (PrintHeader, PrintBody, PrintFooter, etc.).
– “Android System Services” including Bluetooth service and permission management.
– Data flow from the App’s Activities to the Printama API (for instance, when a user initiates a printing command).
– The callback mechanism from the Printama library back to the calling activity (for delivering printer selection results or printing status).
– Another for library components.
d. Present interdependencies and architectural flows clearly.

– Start with the external user trigger (i.e., an Android Activity in the sample app) laying out the permissions’ check and printer selection dialog.
– Illustrate how methods in Printama (e.g., printText, printImage) connect to lower-level routines (formatting, conversion of drawables to Bitmap) before finally interfacing with the device (Bluetooth thermal printer).
– Highlight the separation of modules (with the app module essentially acting as the client/demonstrator of the API and the printama module encapsulating the core functionality).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Sample App Module
    subgraph "Sample App Module"
        A1["MainActivity"]:::app
        A2["TestActivity"]:::app
        A3["PrintamaPrintService"]:::app
        A4["UI Resources"]:::app
    end

    %% Printama Library Module
    subgraph "Printama Library Module"
        L1["Printama API (Printama.java)"]:::lib
        L2["Printer Util (PrinterUtil.java)"]:::lib
        subgraph "Supporting Classes"
            L3a["ChoosePrinterActivity"]:::lib
            L3b["DeviceListAdapter"]:::lib
            L3c["DeviceListFragment"]:::lib
            L3d["Pref"]:::lib
            L3e["StrUtil"]:::lib
        end
        L4["Library Resources"]:::lib
    end

    %% Android System Services
    subgraph "Android System Services"
        S1["Bluetooth Service"]:::sys
        S2["Permission Manager"]:::sys
    end

    %% External Device
    E1["Bluetooth Thermal Printer"]:::ext

    %% Interactions
    A1 -->|"triggersPrint"| L1
    A2 -->|"triggersPrint"| L1
    L1 -->|"invokeService"| A3
    L1 -->|"checkBluetooth"| S1
    L1 -->|"checkPermissions"| S2
    L1 -->|"formatRequest"| L2
    L2 -->|"useHelpers"| L3a
    L2 -->|"useHelpers"| L3b
    L2 -->|"useHelpers"| L3c
    L2 -->|"useHelpers"| L3d
    L2 -->|"useHelpers"| L3e
    L1 -->|"accessResources"| L4
    L1 -->|"sendToPrinter"| E1
    S1 -->|"response"| L1
    S2 -->|"response"| L1
    L1 -->|"printerCallback"| A1
    L1 -->|"printerCallback"| A2

    %% Click Events for Sample App Module
    click A1 "https://github.com/anggastudio/printama/blob/master/app/src/main/java/com/anggastudio/printama_sample/MainActivity.java"
    click A2 "https://github.com/anggastudio/printama/blob/master/app/src/main/java/com/anggastudio/printama_sample/TestActivity.java"
    click A3 "https://github.com/anggastudio/printama/blob/master/app/src/main/java/com/anggastudio/printama_sample/PrintamaPrintService.java"
    click A4 "https://github.com/anggastudio/printama/tree/master/app/src/main/res"

    %% Click Events for Printama Library Module
    click L1 "https://github.com/anggastudio/printama/blob/master/printama/src/main/java/com/anggastudio/printama/Printama.java"
    click L2 "https://github.com/anggastudio/printama/blob/master/printama/src/main/java/com/anggastudio/printama/PrinterUtil.java"
    click L3a "https://github.com/anggastudio/printama/blob/master/printama/src/main/java/com/anggastudio/printama/ChoosePrinterActivity.java"
    click L3b "https://github.com/anggastudio/printama/blob/master/printama/src/main/java/com/anggastudio/printama/DeviceListAdapter.java"
    click L3c "https://github.com/anggastudio/printama/blob/master/printama/src/main/java/com/anggastudio/printama/DeviceListFragment.java"
    click L3d "https://github.com/anggastudio/printama/blob/master/printama/src/main/java/com/anggastudio/printama/Pref.java"
    click L3e "https://github.com/anggastudio/printama/blob/master/printama/src/main/java/com/anggastudio/printama/StrUtil.java"
    click L4 "https://github.com/anggastudio/printama/tree/master/printama/src/main/res"

    %% Styles
    classDef app fill:#ADD8E6,stroke:#000,stroke-width:2px;
    classDef lib fill:#90EE90,stroke:#000,stroke-width:2px;
    classDef sys fill:#D3D3D3,stroke:#000,stroke-width:2px;
    classDef ext fill:#FFA07A,stroke:#000,stroke-width:2px;
```
